user4407686
user4407686

Reputation:

What I'm doing wrong in following array manipulation in foreach loop?

I've an array titled $photos as follows:

Array
(
    [0] => Array
        (
            [fileURL] => https://www.filepicker.io/api/file/UYUkZVHERGufB0enRbJo
            [filename] => IMG_0004.JPG
        )

    [1] => Array
        (
            [fileURL] => https://www.filepicker.io/api/file/WZeQAR4zRJaPyW6hDcza
            [filename] => IMG_0003.JPG
        )

)

Now I want to create a new array titled $values as follows :

 Array
(
    [vshare] => Array
        (
            [IMG_0003.JPG] => Array
                (
                    [0] => https://www.filepicker.io/api/file/RqAN2jZ7ScC8eOx6ckUE
                )

            [IMG_0004.JPG] => Array
                (
                    [0] => https://www.filepicker.io/api/file/XdwtFsu6RLaoZurZXPug
                )

        )

)

For this I tried following code :

$values = array();
        foreach($photos as $photo ) {
          $values['vshare'][$photo->filename] = array($photo->fileURL);
        }

Then I got following wrong output when I print_r($values):

Array
(
    [vshare] => Array
        (
            [] => Array
                (
                    [0] => 
                )

        )

)

Can someone please correct the mistake I'm making in my code?

Thanks.

Upvotes: 0

Views: 44

Answers (3)

neophoenix
neophoenix

Reputation: 66

you should try this code

$values = array();
foreach($photos as $photo) {
    $values['vshare'][$photo['filename']] = array(0 => $photo['fileURL']);
}

Works fine for me.

Upvotes: 0

Adrian Cid Almaguer
Adrian Cid Almaguer

Reputation: 7791

<?php

$values = array();
foreach($photos as $photo ) {
   $values['vshare'][$photo['filename']][0] = $photo['fileURL'];
}

Upvotes: 0

T.Z.
T.Z.

Reputation: 2162

-> is operator for objects, as expleined in this question.

Try:

$values = array();
foreach($photos as $photo ) {
    $values['vshare'][$photo['filename']] = array($photo['fileURL']);
}

Upvotes: 1

Related Questions