dpca31461 2015-07-23 13:43
浏览 5

如何递归存储项目?

I want to collect& store data into an array while drilling into some structured data, so that when I am done, I have an array like this for ex::

[{nodes:[{nodes:[], id:2}, {nodes:[{nodes:[], id:4}], id:3} ], id:1}]

Notice how I store deeper nodes into parent nodes,Here is my attempt simplified, as I tried to pass node by reference to be able to collect deeper nodes into their parent nodes:

$parentNode= array('nodes'=>array());
scan ($parentNode, $excel, 0, 0);

function scan (& $parentNode, $excel, $row, $column, $maxColumn)
{
    $childNode = array('nodes' => array(), 'id' => $excel[$row]['cr'.$column]);

    // to make sure I am drilling as expected, I used echo:
    echo ($newNode['id'].',');
    // now, push the child node into the passed by reference parent node:
    array_push($parentNode['nodes'], $childNode);
    // keep drilling, pass child node as parent:
    $this->scan($childNode, $excel, $row, $column + 1, $maxColumn);
}

But, what I get is:

{nodes:[{nodes:[],id:1}]}

Why wouldn't I get what I expect? As long as I pass childNode by reference, I should be able to build my pyramid of nodes based on first passed node, right?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程