0

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.

1
  • $your_array[0][1]['Image'] = $img_array[0]['Image'];? Commented Dec 4, 2011 at 14:34

2 Answers 2

3
$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!

Sign up to request clarification or add additional context in comments.

3 Comments

Regarding the keys: I think we are looking at a dump in stead of actual code.
What was wrong with my simples :-p .Although i do agree, I hope my answer my draw out a bit more information from the OP
Nothing really wrong. Just didn't see what it would add. So I thought I would remove it while I'm fixing your code block. :-)
1
$firstarray[0][1] = $secondarray[0];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.