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 = new testa;
$testa->yoop();

$testa1 = new testa;
$testa1->noop();
于 2012-09-01T12:56:37.187 に答える
0

それをしてはいけない、

同じ配列にアクセスしようとしていますが、異なる結果が予想されますか? あなたが本当にそれをしたいのであれば(理由がわからない)、配列を再度設定するか、2つのインスタンスを使用することができます

ただ

<?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 に答える
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 に答える