0

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! :)

  • You have 3 arrays here, so the first loop 2 items, the second gets you id, from, link etc and the 3rd will give you name,id of from element (just make sure you check if its an array, before looping) – Peon Oct 19 '17 at 15:19
  • Thanks for your help! So there are 3 "levels"? That makes sense. But how can i get the data from the certain level if not by $something = array_column($toplevel ['2ndlevel'], 'name'); ? I don´t get how to "talk" to the sublayers, since they are not named or anything. –  Oct 19 '17 at 15:58

1 Answers1

0

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.

Nic3500
  • 8,144
  • 10
  • 29
  • 40
  • You are awesome! :) That´s pretty easy actually - i was thinking to complicated :). Thank you very, very much! It works :) –  Oct 19 '17 at 16:25