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?

6
  • var_dump($list), you'll discover that it is 1 at some point… Commented Feb 28, 2018 at 7:45
  • 1
    Can you clarify? Not sure how this helps. Commented Feb 28, 2018 at 7:46
  • 1
    If you did foreach ($lists as $key => $list), then $key will be 0, result_code, result_message, etc. Does that help? Commented Feb 28, 2018 at 7:49
  • How would that be written as code? Commented Feb 28, 2018 at 7: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. Commented Feb 28, 2018 at 7:56

4 Answers 4

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

try this

foreach($lists as $list) {
       $list = (array)$list;
       if(isset($list['id'])){
           echo $list['id'];
       }
}

2 Comments

You don't need to cast to an array in order to use isset.
I think you had better handle results in 'list/list_' api first check $lists has result or counter...
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;
}

Comments

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.