Search code examples
phpforeacharray-column

Unexpected results php foreach loop with array_column


thanks in advance for any help! I´m an absolute newbie to php but trying to get along.

I have an array $result which contains something like:

Array
(
    [0] => Array
        (
            [id] => xyz
            [from] => Array
                (
                    [name] => xyz
                    [id] => xyz
                )

            [link] => xyz
            [name] => xyz
            [picture] => xyz
            [created_time] => xyz
        )
[1] => Array
        (
            [id] => xyz
            [from] => Array
                (
                    [name] => xyz
                    [id] => xyz
                )

            [link] => xyz
            [name] => xyz
            [picture] => xyz
            [created_time] => xyz
        )

And so on... .

What I want to achieve is a foreach loop that works with some of those information. For now I changed the code to simply output the info, so I can see the problems...I think. My Code is:

foreach($result AS $buildresult) {
  $resobjid = array_column($buildresult, 'id');
  $cardpicurl = "https://graph.facebook.com/$resobjid/picture";
  $heading = array_column($buildresult ['from'], 'name');
  $para = array_column($buildresult, 'name');
  $link = array_column($buildresult, 'link');
  echo '<pre>' . print_r( $resobjid, 1 ) . '</pre><br>';
  echo '<pre>' . print_r( $cardpicurl, 1 ) . '</pre><br>';
  echo '<pre>' . print_r( $heading, 1 ) . '</pre><br>';
  echo '<pre>' . print_r( $para, 1 ) . '</pre><br>';
  echo '<pre>' . print_r( $link, 1 ) . '</pre><br>';
  echo '<pre>' . print_r( $cardpicurl, 1 ) . '</pre><br>';
  echo '<pre>' . print_r( $buildresult, 1 ) . '</pre><br>';
}

Im expecting it to do something like:

The ID

https://graph.facebook.com/someidfromthearray/picture

Array
(
    [0] => Some Name from the Array
)
Array
(
    [0] => Some Name from the above array
)

But what I get is:

Array
(
    [0] => 387639951329150
)

https://graph.facebook.com/Array/picture

Array
(
)

Array
(
    [0] => Some.Name
)

Array
(
)

https://graph.facebook.com/Array/picture

So I do get the ID and the name from the "from" array. The rest seems empty. As well the https://graph.facebook.com/$resobjid/picture shows "Array" despite showing up correctly within echo '<pre>' . print_r( $resobjid, 1 ) . '</pre><br>';

Any help is highly appreciated!

Thanks a lot! :)


Solution

  • you do not need array_column to address arrays, you can do it directly. Consider this code:

        $result = array(
                        array('id' => 'id0',
                              'from' => array('name' => 'from_name0','id' => 'from_id0'),
                              'link' => 'link0',
                              'name' => 'name0',
                              'picture' => 'picture0',
                              'created_time' => 'created_time0'
                             ),
                        array('id' => 'id1',
                              'from' => array('name' => 'from_name1','id' => 'from_id1'),
                              'link' => 'link1',
                              'name' => 'name1',
                              'picture' => 'picture1',
                              'created_time' => 'created_time1'
                             )
                       );
        $resobjid = 'SOMETHING';
        foreach($result AS $buildresult)
        {
            $cardpicurl = "https://graph.facebook.com/$resobjid/picture";
    
            echo "<pre>\n";
            echo 'The ' . $buildresult['id'] . "\n";
            echo $cardpicurl . "\n";
            echo $buildresult['from']['name'] . "\n";
            echo $buildresult['name'] . "\n";
            echo $buildresult['link'] . "\n";
            echo $cardpicurl . "\n";
            echo "</pre>\n<hr>\n";
        }
    

    I changed the array values to make the output easier to read and debug, but I did not change the structure.

    I get this:

    enter image description here

    you did not specify a value for $resobjid, so I set something to avoid errors. Also, the second parameter to print_r is only used if you want to assign the result to some variable, like this:

    $output = print_r($array,1);
    

    You do not need it here. I hope this help, Nic.