0

如何删除现有数组(参见下面的代码)?,我知道 CodeIgniter 是一个很好的框架,但我想了解 CI 如何管理他们的 $this->data 方法。

我的代码:

<?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
)
4

4 回答 4

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>';
   }
}
于 2012-09-01T13:02:55.547 回答
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 */
于 2012-09-02T17:12:32.480 回答
0
$testa = new testa;
$testa->yoop();

$testa1 = new testa;
$testa1->noop();
于 2012-09-01T12:56:37.187 回答
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 */
于 2012-09-01T13:00:17.653 回答