Updating a multidimensional array

Hi everyone, I need some help on how to update a multidimensional array in PHP.
I am using the following array to display a grid of images

            
Array
(
    [0] => Array
        (
            [1] => Array
                (
                    [Image] => 'Sample image'
                )
        )

With the keys 0 and 1 representing the Column and Row the image will be placed on. What i am trying to do is update the image portion of this array only, the idea is that the other two keys need to remain the same in order to keep the images in the right place. The new image data is stored in an image array like this:

   
// A sample image array         
Array
(
    [0] => Array
        (
              [Image] => ' A new sample image'
        )

Any ideas on how to update the first array using the second?

Thanks in advance.

What’s the relation between the two? How do I know that Image 0 in your second array should be replacing the one at 0,1 instead of the one at 0,4?

I’m not sure I understand the question.

You want the first array to be replaced by the second (but just the deepest array)?

if the first array was stored in variable $images and the second array was stored in the variable $new_image

$images[0][1][‘image’] = $new_image[0][‘image’];

but this is a complicated setup of arrays which may make reading this code and understanding it more difficult. Naming these arrays, and not nesting them so deeply might really simplify things.

I’m aware its a complicated setup, but i’m not sure how else to do it. Currently the grid is displayed like this:


        for ($r = 0; $r <= $gridRows; $r++)
        {
            $html .= "<div class='wrapper'>";
            for ($c = 0; $c <= $gridCols; $c++)
            {
  
                        if(isset($grid[$r][$c]['Image']))
                        {
                            $image = $grid[$r][$c]['Image'];
                            $html .= "<div class='cell'>$image";

                        }
                        else 
                        {
                            $html .= "<div class='cell'></div>";
                        }
                     
                
            }
            $html .= '</div>';
        }

This works fine, but as i am trying to make the constant for each user i need to find a way to update the Image element of the array, so the position of the image is the same, but the image can be changed as it is voted/ modified etc.

One of the other ideas i had was like you said to make it more semantic and i had an array setup like this:


array([0] =>
         array(  [Col] => 0,
                   [Row] =>1,
                   [Image] => Sample image      
)

But i’m not sure how i would iterate over this in order to output a grid like i currently have.
Any suggestions on making this better would be greatly appreciated.

So… where does the second array come from?

@StarLion Basically to make the grid my application fetchs data from a database, generates an array of images from that, and then finally generates positions randomly and adds the image data to it. This has the flaw that it means the positions change on refresh. So to overcome this i had the idea of using an updated image array and merging it with the old position array. Does that make sense?

I have a question regarding what it is that you’re trying to accomplish here. From what I understand:
a) You have an array where the initial indice is the column where the image will be show and the value of the columns indice contains an array in which the it’s indice represents the row. ( I totally don’t agree with this implementation but to each his own. )
b) You wish to update ONLY the image indice of the secondary array, using an ( yet another ) array that contains the updated image value as a secondary indice.

Before I invest anymore grey matter into this, am I understanding this properly?
Also, why does the “updating array” not respect the format of the “to-be-updated array”?
IE: Why is the second array not formatted with the same structure that represents the row/column structure of the primary array?

P.S. I’d just like to go on record saying that each image really should be an object with row and column properties.

Yes your understanding is correct. The reason for the format is down to the way in which the grid is outputted. I am using two for loops as demonstrated above to iterate over the array and output the images. Preferably i would rather the array be more like:


array(
       [0] => array('Row' => 0,
                         'Col' => 1,
                         'Image' => 'New image')
          
       )

as this would solve my issue, but have no idea how i would iterate over this array in order to turn this into a grid.

Working with that type of an array would be much easier. For instance, assume the following array:


$array = Array(
				array( 'image_name' => 'image1_name' ,
					   'image_col' => 1 ,
					   'image_row' => 1) ,
				array( 'image_name' => 'image2_name' ,
					   'image_col' => 1 ,
					   'image_row' => 2) ,
				array( 'image_name' => 'image1_name' ,
					   'image_col' => 2 ,
					   'image_row' => 1) ,
				array( 'image_name' => 'image2_name' ,
					   'image_col' => 2 ,
					   'image_row' => 2)
				);

Also, assume the following functions:


function getByImageCol ( $col , $array )
{
	$return = array();
	foreach( $array as $image )
		if( $image['image_col'] == $col ) $return[] = $image;
	
	return $return;
}

function getByImageRow( $row , $array )
{
	$return = array();
	foreach( $array as $image )
		if( $image[ 'image_row'] == $row ) $return[] = $image;
		
	return $return;
}

With the following usage:


// Get all images that are in col 1 so you can iterate over just that column
$col1 = getByImageCol( 1 , $array );

// Get all the images from column 1 that are in row 2
$myImage = getByImageRow( 2 , $col1 );

I am unaware of what your grid implementation is and to be honest I’m not sure I want to know. But if you having the ability to iterate over this type of an array will help you…well then hopefully this points you in the right direction. Cheers and good luck :slight_smile:

Ok I was bored…is this your solution?



$array = Array(
				array( 'image_name' => 'image1_name' , 'image_col' => 1 , 'image_row' => 1) ,
				array( 'image_name' => 'image2_name' , 'image_col' => 1 , 'image_row' => 2) ,
				array( 'image_name' => 'image3_name' , 'image_col' => 1 , 'image_row' => 3) ,
				array( 'image_name' => 'image1_name' , 'image_col' => 2 , 'image_row' => 1) ,
				array( 'image_name' => 'image2_name' , 'image_col' => 2 , 'image_row' => 2) ,
				array( 'image_name' => 'image3_name' , 'image_col' => 2 , 'image_row' => 3) ,
				array( 'image_name' => 'image1_name' , 'image_col' => 3 , 'image_row' => 1) ,
				array( 'image_name' => 'image2_name' , 'image_col' => 3 , 'image_row' => 2) ,
				array( 'image_name' => 'image3_name' , 'image_col' => 3 , 'image_row' => 3)
				);

$total_rows = 3;
$total_cols = 3;
$html = '';

for( $row = 1 ; $row <= $total_rows ; $row++ ) {
	$html .= '<div class="wrapper">';
	
	$this_row = getByImageRow( $row , $array );
	for( $col = 1 ; $col <= $total_cols ; $col++ ) {
		$this_image = getByImageCol( $col , $this_row );
		$html .= '<div class="cell">' . $this_image[0][ 'image_name' ] . '</div>';
	}
	
	$html .= '</div>';
}

echo $html;


That’s perfect, thank you :).