在 RecursiveIteratorIterator 中实现 RecursiveArrayIterator 时访问数组对象变量
我有一个大的 arrayObject,我正在使用以下内容循环:-
$rit = new RecursiveIteratorIterator(new RecursiveArrayIterator($hierarchy));
foreach($rit as $key=> $val) {
}
如何访问数组中的特定键?我可以通过回显 $key 和 $val 来访问它们,但我有我希望访问的特定键。如果我尝试调用 $key[''] 我会得到密钥名称的第一个字母。
编辑1
一些示例数据(也可以有许多不同的子子项):
ArrayObject::__set_state(array(
0 =>
ArrayObject::__set_state(array(
0 =>
array (
'id' => '8755',
'company_id' => '1437',
'name' => 'Name 1'
),
1 =>
ArrayObject::__set_state(array(
0 =>
ArrayObject::__set_state(array(
0 =>
array (
'id' => '8763',
'company_id' => '1437',
'name' => 'Name 2'
),
1 =>
ArrayObject::__set_state(array(
0 =>
ArrayObject::__set_state(array(
0 =>
array (
'id' => '9067',
'company_id' => '1437',
'name' => 'Name 3'
),
)),
1 =>
ArrayObject::__set_state(array(
0 =>
array (
'id' => '8765',
'company_id' => '1437',
'name' => 'Name 4'
),
)),
2 =>
ArrayObject::__set_state(array(
0 =>
array (
'id' => '9049',
'company_id' => '1437',
'name' => 'Name 5'
),
)),
3 =>
ArrayObject::__set_state(array(
0 =>
array (
'id' => '8769',
'company_id' => '1437',
'name' => 'Name 6'
),
)),
I have a large arrayObject which I'm looping over using the following:-
$rit = new RecursiveIteratorIterator(new RecursiveArrayIterator($hierarchy));
foreach($rit as $key=> $val) {
}
How can I access the a specific key within the array? I can access them by echoing $key and the $val but I have specific keys I wish to access. If I attempt to call $key[''] I get the first letter on the key name.
Edit 1
Some sample data (There can be many different sub-children too):
ArrayObject::__set_state(array(
0 =>
ArrayObject::__set_state(array(
0 =>
array (
'id' => '8755',
'company_id' => '1437',
'name' => 'Name 1'
),
1 =>
ArrayObject::__set_state(array(
0 =>
ArrayObject::__set_state(array(
0 =>
array (
'id' => '8763',
'company_id' => '1437',
'name' => 'Name 2'
),
1 =>
ArrayObject::__set_state(array(
0 =>
ArrayObject::__set_state(array(
0 =>
array (
'id' => '9067',
'company_id' => '1437',
'name' => 'Name 3'
),
)),
1 =>
ArrayObject::__set_state(array(
0 =>
array (
'id' => '8765',
'company_id' => '1437',
'name' => 'Name 4'
),
)),
2 =>
ArrayObject::__set_state(array(
0 =>
array (
'id' => '9049',
'company_id' => '1437',
'name' => 'Name 5'
),
)),
3 =>
ArrayObject::__set_state(array(
0 =>
array (
'id' => '8769',
'company_id' => '1437',
'name' => 'Name 6'
),
)),
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
默认情况下,RecursiveIteratorIterator 将仅列出叶子。尝试
获取包含元素。
By default, the RecursiveIteratorIterator will only list the leaves. Try
to get the containing elements as well.
假设
$hierarchy
是您的大型数组,您可以使用$hierarchy["foo"]
访问与foo
键关联的值。或者像这样:Assuming
$hierarchy
is your large array, you can use$hierarchy["foo"]
to access the value associated with thefoo
key. Or like this: