2

I'm trying to integrate with an API. Assuming the following code works.

$lists = $ac->api("list/list_", array("ids" => "all"));

            echo"<pre>"; print_r( $lists); echo"</pre>";

Outputs the following stdclass object

stdClass Object
(
    [0] => stdClass Object
        (
            [id] => 1
            [name] => List 3
            [cdate] => 2018-02-27 08:19:39
            [private] => 0
            [userid] => 1
            [subscriber_count] => 0
        )

    [result_code] => 1
    [result_message] => Success: Something is returned
    [result_output] => json
    [http_code] => 200
    [success] => 1
)

The following code

            foreach($lists as $list) {

                   echo $list->id;

               }

Shows me an error

Trying to get property of non-object

The line $list->id is wrong. How can I fix this?

kevinkt
  • 735
  • 12
  • 24
  • `var_dump($list)`, you'll discover that it is `1` at some point… – deceze Feb 28 '18 at 07:45
  • 1
    Can you clarify? Not sure how this helps. – kevinkt Feb 28 '18 at 07:46
  • 1
    If you did `foreach ($lists as $key => $list)`, then `$key` will be `0`, `result_code`, `result_message`, etc. Does that help? – deceze Feb 28 '18 at 07:49
  • How would that be written as code? – kevinkt Feb 28 '18 at 07:54
  • You don't need to write it as code. The problem is that the object which has an `->id` property is at the same level as other values such as `1`, `"Success: Something is returned"` etc, and you're trying to access `->id` on all of those diverse values. – deceze Feb 28 '18 at 07:56
  • You should really be changing your API so it returns something like `[results] => Array ( [0] => ... ), [result_code] => ...`, that's much easier to work with. – deceze Feb 28 '18 at 08:01

4 Answers4

1

It's an odd structure, whats happening is its looping over result_code, result_message, result_output, success which the values are not objects.

Either fix that, or do a check in the loop of sorts.

foreach ($lists as $key => $list) {
    if (!is_numeric($key) || !is_object($list)) {
        continue;
    }
    echo $list->id;
}
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
0

try this

foreach($lists as $list) {
       $list = (array)$list;
       if(isset($list['id'])){
           echo $list['id'];
       }
}
Bai Nguyen
  • 814
  • 7
  • 16
0

PHP Objects are clean and easy to write.

// Echoing a PHP Array value

echo $array['value'];

// Echoing a PHP Object value

echo $object->value;

Now to the conversion (casting) of a PHP Array into a PHP Object. This is very simple. I just type cast the Array as an Object when returning it.

function array_to_object($array) {
    return (object) $array;
}

The above is just an example. You do not need a PHP function to convert an Array into an Object. The (object) function will do that to any PHP Array. If you ever need to change an Object into an Array, then use the (array) type casting function.

function object_to_array($object) {
    return (array) $object;
}
Vrushal Raut
  • 1,050
  • 2
  • 15
  • 20
0

When return data from json where [ ] is not present, do a array before parsing your data. Like the sample:

foreach(array($YourData) as $row){
    echo $row->ValueOfYourData;
}
Tiago Martins Peres
  • 14,289
  • 18
  • 86
  • 145