RecursiveIteratorIterator クラスを使用して、すべての子オブジェクトを反復処理して、特定のキーが特定の列のリストと一致するかどうかを確認しようとしています。キー/列が一致したら、テキストを変更します。ただし、各要素にオブジェクトがある多次元配列にアクセスできるようにしたいと考えています。
配列とオブジェクトをトラバースし、下位の子レベルを反復処理してキー名を確認する方法を探しています。キー名が一致する場合、コールバック関数またはある種の正規表現/置換関数を実行したい場合があります。
データは次のようになります。
[0] => Array
(
[01__BLAH_A] => 1
[01__BLAH_B] => 0
[01__BLAH_C] => 1
[01__BLAH_D] => 1
[01__BLAH_E] => 1
[01__BLAH_F] => 1
[01__BLAH_G] => 0
[01__BLAH_H] => 3
[01__BLAH_I] => 0
[01__BLAH_J] => 1
[01__BLAH_K] => 1
[01__BLAH_L] => 1
[01__BLAH_M] => 3
[SOME_OBJECT] => some_object Object
(
[variable_1:some_type:private] =>
[variable_2:some_type:private] =>
[my_data:protected] => Array
(
[BLAH_1_A] => nAME
[BLAH_1_B] => blahblah
[BLAH_1_C] => other_dude
[BLAH_1_D] => 1
[BLAH_1_E] => 55
[BLAH_1_F] => 1
[BLAH_1_G] => null
[BLAH_1_H] => 1234567989
)
)
[SOME_OTHER_OBJECT] => some_other_object Object
(
[variable_1:some_type:private] =>
[variable_2:some_type:private] =>
[my_data:protected] => Array
(
[BLAH_2_A] => nAME of another
[BLAH_2_B] => fofofofo
[BLAH_2_C] => right_dude
[BLAH_2_D] => 1
[BLAH_2_E] => 33
[BLAH_2_F] => 2
[BLAH_2_G] => 0
[BLAH_2_H] => 987654321
)
)
)
[1] => Array
(
[02__BLAH_A] => 1
[02__BLAH_B] => 0
[02__BLAH_C] => 1
[02__BLAH_D] => 1
[02__BLAH_E] => 1
[02__BLAH_F] => 1
[02__BLAH_G] => 0
[02__BLAH_H] => 3
[02__BLAH_I] => 0
[02__BLAH_J] => 1
[02__BLAH_K] => 1
[02__BLAH_L] => 1
[02__BLAH_M] => 3
[SOME_OTHER_OBJECT] => some_other_object Object
(
[variable_1:some_type:private] =>
[variable_2:some_type:private] =>
[my_data:protected] => Array
(
[BLAH_2_A] => nAME of another
[BLAH_2_B] => fofofofo
[BLAH_2_C] => right_dude
[BLAH_2_D] => 1
[BLAH_2_E] => 33
[BLAH_2_F] => 2
[BLAH_2_G] => 0
[BLAH_2_H] => 987654321
)
)
)
データには、オブジェクトの基本的な配列と、いくつかの要素とネストされたオブジェクトがあることに注意してください。各配列要素に異なるオブジェクト型が存在する場合があります。これらすべての要素にとらわれない反復子が必要です。
RecursiveIteratorIterator で正しい道を進んでいると思っていましたが、それらのオブジェクトに到達したときに障害物にぶつかりました。
class Modifiable_Iterator
extends RecursiveIteratorIterator
{
private $char_set;
private $columns_to_check = array();
static function make($mixed_array_data)
{
return new self($mixed_array_data);
}
function __construct($data)
{
parent::__construct(
new RecursiveArrayIterator($data),
RecursiveIteratorIterator::SELF_FIRST,
null);
$this->columns_to_check = array('BLAH_2_A', 'BLAH_1_A');
}
final public function current($parent_key_name = null)
{
// Retrieves the current value
$current = parent::current();
if (in_array($this->key(), $this->columns_to_check))
{
// If the column name matches the list of columns in the private
// variable, then edit the column value
return _some_function_that_edits_this_value($current);
}
return $current;
}
final public function exec()
{
$this->_loop_check($this);
return $this;
}
final private function _loop_check($iterator)
{
while ($iterator->valid())
{
if ($iterator->hasChildren())
{
$this->_loop_check($iterator->getChildren());
}
$this->offsetSet($iterator->key(), $this->current());
$iterator->next();
}
}
final private function _some_function_that_edits_this_value($value)
{
// Do something to the value and return it.
return $value;
}
}
混合データ オブジェクトを取得して、このコードを次のように実行できるようにしたいと考えています。
$new_text = Modifiable_Iterator::make($mixed_bag_of_data)->exec();