dsiimyoc804955 2014-03-25 02: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-25 02: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条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘