PHP - replace values in sub array -


i have 2 arrays $pq , $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 trying replace values in $pq items values $rs. example item-01 in $pa replaced abc $rs. tried this:

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

when print values of $item $pq, prints values $rs, when print whole array $pq, values seem unchanged. can please me find out missing ?

you're correctly looping through items in each line reference, you're not doing lines or pages themselves. you're updating value of item in copy of line, instead of line itself. should be:

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

note & in front of &$lines , &$pages. note $rs1 doesn't need passed reference, since aren't changing in array.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -