添加/修改php n级关联数组

发布于 2024-11-01 06:51:01 字数 1266 浏览 2 评论 0原文

$arrResult=array(
0=>array('categoryid'=>112,'catname'=>'apperal','subcategory'=>array(
412=>array('categoryid'=>428,'catname'=>'rainwear','subcategory'=>array(
428=>array('categoryid'=>413,'catname'=>'summer','subcategory'=>array()))))));

print_r($arrResult);


$iterator = new RecursiveArrayIterator($arrResult); 
iterator_apply($iterator, 'traverseStructure', array($iterator)); 

function traverseStructure($iterator) {
   $arrAddResult=array('categoryid'=>416,'catname'=>'winter','subcategory'=>array());

    while ( $iterator -> valid() ) {

        if ( $iterator -> hasChildren() ) {

            traverseStructure($iterator -> getChildren());

        }
        else {

            if($iterator -> current() == 413)
            {
                $arr=&$iterator;
                $a='arr';                           
                            ${$a}['subcategory']=$arrAddResult;
                break;
            }
        }
        $iterator -> next();
    }
} 

预期输出是将“arrAddResult”追加到 $arrResult 中。但由于某种原因,迭代器得到修改,但它没有反映 arrResult 数组中的修改。

我尝试在函数“traverseStructure”中通过 ref 传递数组,但仍然难以获得正确的输出。

我首先尝试迭代器。我必须构造一个 N 级关联数组作为 arrResult 因此选择使用迭代器。

$arrResult=array(
0=>array('categoryid'=>112,'catname'=>'apperal','subcategory'=>array(
412=>array('categoryid'=>428,'catname'=>'rainwear','subcategory'=>array(
428=>array('categoryid'=>413,'catname'=>'summer','subcategory'=>array()))))));

print_r($arrResult);


$iterator = new RecursiveArrayIterator($arrResult); 
iterator_apply($iterator, 'traverseStructure', array($iterator)); 

function traverseStructure($iterator) {
   $arrAddResult=array('categoryid'=>416,'catname'=>'winter','subcategory'=>array());

    while ( $iterator -> valid() ) {

        if ( $iterator -> hasChildren() ) {

            traverseStructure($iterator -> getChildren());

        }
        else {

            if($iterator -> current() == 413)
            {
                $arr=&$iterator;
                $a='arr';                           
                            ${$a}['subcategory']=$arrAddResult;
                break;
            }
        }
        $iterator -> next();
    }
} 

the expected output is to append the 'arrAddResult' appenedn in $arrResult. But with some reason the iterator get modify but it doesn't reflect the modification in arrResult array.

I tried passing the array by ref in function 'traverseStructure' but still struggling to get the correct output.

I am trying iterator first. I have to constructor a N-Level associative array as arrResult hence opt to use the iterator.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

未央 2024-11-08 06:51:01

下面是一个使用一个数组执行此操作的方法的示例。

<?php
$arrResult=array(
    1=>array('categoryid'=>112,'catname'=>'apperal','subcategory'=>array()),
    0=>array('categoryid'=>112,'catname'=>'apperal','subcategory'=>array(
        1=>array('categoryid'=>112,'catname'=>'rainwear','subcategory'=>array(
            1=>array('categoryid'=>112,'catname'=>'apperal','subcategory'=>array()),
            428=>array('categoryid'=>413,'catname'=>'summer','subcategory'=>array()))
            ),
        412=>array('categoryid'=>428,'catname'=>'rainwear','subcategory'=>array(
            1=>array('categoryid'=>112,'catname'=>'apperal','subcategory'=>array()),
            428=>array('categoryid'=>413,'catname'=>'summer','subcategory'=>array()))
            )
        )
    )
);

function append(&$ar,$who,$what){
    // just a simple check, you can remove it
    if(!is_array($ar))return false;
    // loop through all keys
    foreach($ar as $k=>$v){
        // found node, i'm assuming you don't have the node multiple times
            // if you want this to go forever, remove the returns and the if on the add()
        if($v['categoryid']==$who){
            $ar[$k]['subcategory'][]=$what;
            return true;
        }
        // recursion !
        if(add($ar[$k]['subcategory'],$who,$what))return true;// if found stop
    }
    // key not found here in this node or subnodes
    return false;
}
append($arrResult,413,array('categoryid'=>416,'catname'=>'winter','subcategory'=>array()));
echo'<pre>';
var_dump($arrResult);

这在大型阵列上可能效率低下。我建议创建一个类来缓存 $who 和 $what,这样它就不会被复制到遍历的所有级别。其余的应该是相同的。

Here's an example on a way of doing this with one array.

<?php
$arrResult=array(
    1=>array('categoryid'=>112,'catname'=>'apperal','subcategory'=>array()),
    0=>array('categoryid'=>112,'catname'=>'apperal','subcategory'=>array(
        1=>array('categoryid'=>112,'catname'=>'rainwear','subcategory'=>array(
            1=>array('categoryid'=>112,'catname'=>'apperal','subcategory'=>array()),
            428=>array('categoryid'=>413,'catname'=>'summer','subcategory'=>array()))
            ),
        412=>array('categoryid'=>428,'catname'=>'rainwear','subcategory'=>array(
            1=>array('categoryid'=>112,'catname'=>'apperal','subcategory'=>array()),
            428=>array('categoryid'=>413,'catname'=>'summer','subcategory'=>array()))
            )
        )
    )
);

function append(&$ar,$who,$what){
    // just a simple check, you can remove it
    if(!is_array($ar))return false;
    // loop through all keys
    foreach($ar as $k=>$v){
        // found node, i'm assuming you don't have the node multiple times
            // if you want this to go forever, remove the returns and the if on the add()
        if($v['categoryid']==$who){
            $ar[$k]['subcategory'][]=$what;
            return true;
        }
        // recursion !
        if(add($ar[$k]['subcategory'],$who,$what))return true;// if found stop
    }
    // key not found here in this node or subnodes
    return false;
}
append($arrResult,413,array('categoryid'=>416,'catname'=>'winter','subcategory'=>array()));
echo'<pre>';
var_dump($arrResult);

This might be inefficient on large arrays. I'd recommend making a class that caches the $who and $what so it doesn't get copied to all the levels of the traversal. The rest should be identical.

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