-1

I have an array I call with an api connection.

["data"]=>
  object(stdClass)#212 (3) {
    ["status_code"]=>
    int(200)
    ["data"]=>
    object(stdClass)#211 (3) {
      ["days"]=>
      int(30)
      ["total_clicks"]=>
      int(6)
      ["clicks"]=>
      array(30) {
        [0]=>
        object(stdClass)#215 (2) {
          ["clicks"]=>
          int(0)
          ["day_start"]=>
          int(1466395200)
        }
        [1]=>
        object(stdClass)#216 (2) {
          ["clicks"]=>
          int(0)
          ["day_start"]=>
          int(1466308800)
        }

I try to get the first

["data"]=>
  object(stdClass)#212 (3) {
    ["status_code"]=>
    int(200)
    ["data"]=>
    object(stdClass)#211 (3) {
      ["days"]=>
      int(30)
      ["total_clicks"]=>
      int(6)
      ["clicks"]=>
      array(30) {
        [0]=>
        object(stdClass)#215 (2) {
          ["clicks"]=>
          int(0)

I try to do so with the following function (the array itself is stored in the "$data" variable):

function currentClicks($data)
        {
            $path = $data->data->data->clicks;
            foreach ($path as $key => $item) {
                $array[] = [$item->clicks[0]];
            }
            return json_encode($array);
        }

And I echo it in a <h1> tag: <h1><?php echo currentClicks($data); ?></h1> The problem is that instead of displaying the value (which is the number 0) I get an error in console displaying

[[null],[null],[null],[null],[null],[null],[null],[null],[null],[null],[null],[null],[null],[null],[null],[null],[null],[null],[null],[null],[null],[null],[null],[null],[null],[null],[null],[null],[null],[null]]
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Alphonse
  • 661
  • 1
  • 12
  • 29
  • You are asking an off-topic question. Please read [On-Topic](http://superuser.com/help/on-topic), [How do I ask a good question?](http://superuser.com/help/how-to-ask) and [What types of questions should I avoid asking?](http://superuser.com/help/dont-ask). – DavidPostill Jun 21 '16 at 10:41
  • How is this off topic? He clearly has a bug in traversing the object. – Tibor Szasz Jul 12 '16 at 07:23

1 Answers1

-2

Clicks is still an object, you still haven't reached the "bottom" of the object to display a primitive.

So for example it should be:

 $array[] = [$item->clicks[0]['clicks']];
Tibor Szasz
  • 3,028
  • 2
  • 18
  • 23