在 RecursiveIteratorIterator 中实现 RecursiveArrayIterator 时访问数组对象变量

发布于 2024-09-29 02:45:39 字数 1814 浏览 5 评论 0原文

我有一个大的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自由范儿 2024-10-06 02:45:39

默认情况下,RecursiveIteratorIterator 将仅列出叶子。尝试

$rit = new RecursiveIteratorIterator(
           new RecursiveArrayIterator($hierarchy),
           RecursiveIteratorIterator::SELF_FIRST);

获取包含元素。

By default, the RecursiveIteratorIterator will only list the leaves. Try

$rit = new RecursiveIteratorIterator(
           new RecursiveArrayIterator($hierarchy),
           RecursiveIteratorIterator::SELF_FIRST);

to get the containing elements as well.

梦初启 2024-10-06 02:45:39

假设 $hierarchy 是您的大型数组,您可以使用 $hierarchy["foo"] 访问与 foo 键关联的值。或者像这样:

$my_key = "foo"
echo $hierarchy[$my_key]
// Same as
echo $hierarchy["foo"]

Assuming $hierarchy is your large array, you can use $hierarchy["foo"] to access the value associated with the foo key. Or like this:

$my_key = "foo"
echo $hierarchy[$my_key]
// Same as
echo $hierarchy["foo"]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文