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
Post a Comment