dsiimyoc804955 2014-03-24 18:41 采纳率: 100%
浏览 22
已采纳

PHP - 替换子数组中的值

I have two arrays $pq and $rs. please see them below:

$pq = array ('page-0'=>array ('line-0'=>array('item-0'=>array('name'=>"item-00",'value'=>"123"),
                                           'item-1'=>array('name'=>"item-01",'value'=>"456")
                                            ),
                            'line-1'=>array('item-0'=>array('name'=>"item-10",'value'=>"789"),
                                           'item-1'=>array('name'=>"item-11",'value'=>"012")
                                            )),

'page-1'=>array ('line-0'=>array('item-0'=>array('name'=>"item-100",'value'=>"345"),
                                           'item-1'=>array('name'=>"item-101",'value'=>"678")
                                            ),
                            'line-1'=>array('item-0'=>array('name'=>"item-110",'value'=>"901"),
                                           'item-1'=>array('name'=>"item-111",'value'=>"234")
                                            ),
                            'line-2'=>array('item-0'=>array('name'=>"item-210",'value'=>"567"),
                                           'item-1'=>array('name'=>"item-211",'value'=>"890")
                                            ))

);

 $rs = array ('1'=>array('name'=>'item-00', 'value'=>"abc"),
'2'=>array('name'=>'item-01', 'value'=>"def"),
'3'=>array('name'=>'item-10', 'value'=>"ghi"),
'4'=>array('name'=>'item-11', 'value'=>"jkl"),
'5'=>array('name'=>'item-100', 'value'=>"mno"),
'6'=>array('name'=>'item-101', 'value'=>"pqr"),
'7'=>array('name'=>'item-110', 'value'=>"stu"),
'8'=>array('name'=>'item-111', 'value'=>"vwx")
);

What I am trying to do is to replace the values in $pq for items with the values from $rs. for example item-01 in $pa to be replaced with abc from $rs. What I tried is this:

foreach($rs as &$rs1) {
echo "first count :".$firstCount."<br>";
foreach($pq as $pages) {
    foreach($pages as $lines) {
        foreach($lines as &$item) {
            if ($item['name'] == $rs1['name']) { echo "matching </p>";
                    $item['value']=$rs1['value'];
                echo '<pre>';
                print_r($item);
                echo '</pre>';
                echo "<hr>";
                }


            }
       }

    }
}

When I print the values of $item from $pq, it prints the values from $rs, but when I print the whole array $pq, the values seem to be unchanged. Can anyone please help me find out what I am missing ?

展开全部

  • 写回答

3条回答 默认 最新

  • dry9192 2014-03-24 18:47
    关注

    You're correctly looping through the items in each line by reference, but you're not doing it for the lines or pages themselves. So you're updating the value of an item in a copy of the line, instead of the line itself. It should be:

    foreach($rs as $rs1) {
        echo "first count :".$firstCount."<br>";
        foreach($pq as &$pages) {
            foreach($pages as &$lines) {
                foreach($lines as &$item) {
                    if ($item['name'] == $rs1['name']) { echo "matching </p>";
                            $item['value']=$rs1['value'];
                        echo '<pre>';
                        print_r($item);
                        echo '</pre>';
                        echo "<hr>";
                    } 
    
                }
           }
    
        }
    }
    

    Note that the & in front of &$lines and &$pages. Note also that $rs1 doesn't need to be passed by reference, since you aren't changing anything in that array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部