-2

I have an array (the response I get from a service upon a POST request)

echo count($myarray);

gives 2 as result

var_dump($myarray);

gives

array(2) {
  [0]=>
  object(stdClass)#31 (12) {
    ["id"]=>
    string(1) "1"
    ["name"]=>
    string(5) "name"
    ["my_numbr"]=>
    string(18) "a:1:{i:0;s:1:"1";}"
    ["total"]=>
    string(7) "s:0:"";"
  }
  [1]=>
  object(stdClass)#30 (12) {
    ["id"]=>
    string(1) "4"
    ["name"]=>
    string(3) "john"
    ["my_numbr"]=>
    string(18) "a:1:{i:0;s:1:"2";}"
    ["total"]=>
    string(18) "a:1:{i:0;s:1:"2";}" 
  }
}

All I am trying to do is print the array using a for loop like this,

for($i=0;$i<count($myarray);$i++)
{
  echo $myarray[$i]["name"];
  echo $myarray[$i]["total"];
}

But I am getting this error.

1 Answers1

1

This is because you have an array of objects not arrays, try this:

foreach ($myarray as $item) {
    echo $item->name;
    echo $item->total;
}

Enjoy.

Dan Belden
  • 1,199
  • 1
  • 9
  • 20