Jump to content

changing values of a multidimensional array?


zander1983

Recommended Posts

Hi

I have an array which looks like this when i do a print_r():

 

Array ( [0] => Array ( [path] => 1 [order] => 2 ) [1] => Array ( [path] => 2 [order] => 1 ) [2] => Array ( [path] => 3 [order] => 2 ) )

 

In this instance, I want to change the values of the order key. So I do something like this so that the last array has a path of 3 and an order of 1, and the others have orders of 2

 

foreach ($old_images as $old_image){

if($old_image['path']==3){

$old_image['order']="1";

}

else{

$old_image['order']="2";

}

}

 

When i step through, i see that the order is changed, but when i do a print_r(), i get the same result. So even though im resetting the order value of the inside arrays, the multidimensional array does not change....

any ideas?

 

 

You are changing values in $old_image. $old_image is not the original array. It is a copy of each element of the original array that the foreach() statement sets.

 

You would need to set the values in the $old_images array.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.