0

I'm building a php webapp and I want to get a list of id's from an array named $array.

stdClass Object
(
  [users] => Array
    (
      [0] => stdClass Object
        (
          [id] => 90379747
          [id_str] => 90379747
        )
      [1] => stdClass Object
        (
          [id] => 30207605
          [id_str] => 30207605        
        )
    )
)

However when I try to get the value from the id key it somehow is not working.

echo "<h1>Result</h1>";

foreach ($array as $obj)
{
  echo $obj->id;
}

What am I doing wrong here. Other examples on stackoverflow seem to suggest this should work.

  • First, what you show is an object, not an array. So if `print_r($array)` indeed gives you what you show here then there is definitely no reason to expect that code to work. `$obj` will be an array inside that loop. `var_dump($obj)` to see that. – Sherif Sep 15 '16 at 00:29
  • Can you add the class creation code to make this easier to anwer – Joel Davis Sep 15 '16 at 00:33
  • Its part of twitteroauth $array = $connection->get("followers/list", ["screen_name" => twitterdev, "count" => 10]); – Ronald van der Meer Sep 15 '16 at 00:47

1 Answers1

0

You iterate an object, not an array. Try this way:

foreach ($array->users as $obj)
{
  echo $obj->id;
}
Bart
  • 1,268
  • 2
  • 12
  • 14