php - How to reassign an array value in a loop? -


i have following piece of code:-

$returnarr = $this->master_model->fetch_all_data($data, $selectstring,$limit, $offset); foreach($returnarr $row)     {         if (array_key_exists($data.'_image', $row))          {             $img = base_url()."uploads/$data/". $row[$data.'_image'];             $row[$data.'_image'] = $img;         }     } print_r($returnarr); 

the $return in following format:

array ( [0] =>           array ( [sticker_image] => post_1462515402.jpg                   [sticker_code] => :* )          [1] => array ( [sticker_image] => post_1462515510.jpg                   [sticker_code] => ^=^ )          [2] => array ( [sticker_image] => post_1462515532.jpg                   [sticker_code] => >_<* )          [3] => array ( [sticker_image] => post_1462515539.jpg                   [sticker_code] => :(( ) )  

now, in following line of code, changing [sticker_image] link:

if (array_key_exists($data.'_image', $row))  {     $img = base_url()."uploads/$data/". $row[$data.'_image'];     $row[$data.'_image'] = $img; } 

still, changes doesn't take place. it's still coming

[sticker_image] => post_1462515402.jpg 

what doing wrong?

$row[$data.'_image'] = $img; change local copy of array element.

to change actual array element must loop reference:

$returnarr = ['a' => 'b'];  foreach ($returnarr &$row) {     $row = 'cc'; }  var_dump($returnarr); // ['a' => 'cc']; 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -