trottski
trottski

Reputation: 51

updating a multidimensional array

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?

Update: Ok based on the comments and answers i realise i wasn't overly clear, and i apologise. So to clarify and give some context, these arrays are being used to construct a grid of images.

The basic process of which is: Get data -> Generate array of Images from data -> generate random positions and have image data an element of that array, the final array is shown above. The problem with this, is that it means the positions of the images change on refresh, so to prevent this i came up with the idea of updating the final array image element with new data if the database changes.

For further clarification, currently the grid html is built 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>';
    } 

Any suggestions on how to improve this would be greatly appreciated.

Thanks in advance.

Upvotes: 0

Views: 193

Answers (2)

user336242
user336242

Reputation:

$array1[0][1]['image'] = $array2[0]['image']; 

Although I don' think this what you are after. Also remember to quote your assoc. array index names unless they really are constants in which case use CAPS!

Upvotes: 3

abcde123483
abcde123483

Reputation: 3905

$firstarray[0][1] = $secondarray[0];

Upvotes: 1

Related Questions