douxiuyi6529 2016-03-02 10:29
浏览 39

如何使array_walk_recursive访问每个节点?

I have a recursive $data structure that I need to modify. Each node considered an $item should get a property with the value of $value added. Things that I tried (and how they failed) are:

  • array_walk_recursive: Visits only leaf nodes.
  • Stack/queue: I failed to modify the original structure but only altered the copies on the stack/queue.
  • Loops: Without the stack/queue approach I would need to know the nesting level and write an awful lot of nested loops.
  • array_map: I failed to write a proper recursive callback given that the value of $value is not static but the result of previous code. So it must somehow get "into" the callback. Since use is only available to anonymous functions I did not manage to write a recursive one.
  • Loop and recursive function: This answer to a similar question failed for the same reason as the array_map approach.

My situation in code looks similar to this example:

<?php

$value = 'example';

$data = array(
    'foo' => 'bar'
    'items' => array(
        array(
            'foo' => 'bar',
            'items' => array(
                array('foo' => 'bar')
            )
        )
    )
);

// do this recursively to every member of an 'items' property:
$item['baz'] = $value;

Can you think of a different approach or help me straighten out one of those that I failed at so far?

Update

Some code that I tried that did not work:

// Parse error:  syntax error, unexpected 'use' (T_USE), expecting '{'

function do (&$item) use ($value) {

    $item['baz'] = $value;

    foreach ($item['items'] as $next) {

        do($next);
    }
}

// Undefined variable: value

function do (&$item) {

    $item['baz'] = $value;

    foreach ($item['items'] as $next) {

        do($next);
    }
}

foreach ($data['items'] as $item) {

    do($item);
}

Works for now (I would prefer not having to pass the $value parameter, though):

function do (&$item, $value) {

    $item['baz'] = $value;

    foreach ($item['items'] as &$next) {

        do($next, $value);
    }
}

foreach ($data['items'] as &$item) {

    do($item, $value);
}
  • 写回答

3条回答 默认 最新

  • dtjkl42086 2016-03-02 10:49
    关注

    Check this code for get each key and value:

    <?php
    
    error_reporting(0);
    
    $value = 'example';
    
    $data = array(
        'foo' => 'bar',
        'items' => array( array( 'foo' => 'bar','items' => array(array('foo' => 'bar') ) ) )
    );
    
    
    $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($data));
      foreach ($iterator as $k => $v) {
            echo $k.'=>'.$v;
            echo '</br>';
      }
    
    ?>
    

    Formatting was necessary but did not suffice for the minimum edit length. So I added this otherwise useless text.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据