我正在尝试使用 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();