如何删除现有数组(请参阅下面的代码)?我知道CodeIgniter是一个很好的框架,但我想了解CI如何管理他们的$ this->数据方法。
我的代码:
<?php
class simple {
public $data;
public function set($key, $value)
{
$this->data[$key] = $value;
}
}
class testa extends simple {
function yoop()
{
$this->set('name', 'excelent');
echo '<pre>'.print_r($this->data, TRUE).'</pre>';
}
function noop()
{
$this->set('level', 'normal');
$this->set('power', 'high');
echo '<pre>'.print_r($this->data, TRUE).'</pre>';
}
}
$testa = new testa;
$testa->yoop();
$testa->noop();
/* EOF */
当前结果:
Array
(
[name] => excelent
)
Array
(
[name] => excelent
[level] => normal
[power] => high
)
我想删除现有的数组,我想要的最终结果是:
Array
(
[name] => excelent
)
Array
(
[level] => normal
[power] => high
)
答案 0 :(得分:2)
我真的不明白你的目标是什么。为什么不简单:
class testa {
public $data;
function yoop()
{
$this->data = array('name' => 'excellent');
echo '<pre>'.print_r($this->data, TRUE).'</pre>';
}
function noop()
{
$this->data = array(
'level' => 'normal',
'power' => 'high'
);
echo '<pre>'.print_r($this->data, TRUE).'</pre>';
}
}
答案 1 :(得分:0)
$testa = new testa;
$testa->yoop();
$testa1 = new testa;
$testa1->noop();
答案 2 :(得分:0)
你做不到,
您正在尝试访问相同的阵列,但期望得到不同的结果? 如果你真的想这样做(不知道为什么),你可以再次设置数组,或者可以使用两个实例
只是
<?php
class simple {
public $data;
public function set($key, $value)
{
$this->data[$key] = $value;
}
public function reset(){
this->data = array();
}
}
class testa extends simple {
function yoop()
{
$this->reset();
$this->set('name', 'excelent');
echo '<pre>'.print_r($this->data, TRUE).'</pre>';
}
function noop()
{
$this->reset();
$this->set('level', 'normal');
$this->set('power', 'high');
echo '<pre>'.print_r($this->data, TRUE).'</pre>';
}
}
$testa = new testa;
$testa->yoop();
$testa->noop();
/* EOF */
答案 3 :(得分:0)
尝试为testa
课程创建另一个功能:
function qoop(){
unset($this->data['name']); // this function remove $data key by 'name'
$this->set('level', 'normal');
$this->set('power', 'high');
echo '<pre>'.print_r($this->data, TRUE).'</pre>';
}
结果将是:
Array
(
[name] => excelent
)
Array
(
[level] => normal
[power] => high
)
原始代码:
<?php
class simple {
public $data;
public function set($key, $value)
{
$this->data[$key] = $value;
}
}
class testa extends simple {
function yoop()
{
$this->set('name', 'excelent');
echo '<pre>'.print_r($this->data, TRUE).'</pre>';
}
function noop()
{
$this->set('level', 'normal');
$this->set('power', 'high');
echo '<pre>'.print_r($this->data, TRUE).'</pre>';
}
function qoop()
{
unset($this->data['name']); // this function remove $data key by 'name'
$this->set('level', 'normal');
$this->set('power', 'high');
echo '<pre>'.print_r($this->data, TRUE).'</pre>';
}
}
$testa = new testa;
$testa->yoop();
$testa->qoop();
/* EOF */