0

Like in Java when you iterate a list, it's real easy, it's like: while(BLAH.hasNext()) { }, so how do I do that in PHP when I have an array within an stdObject that I want to iterate through each and every item?

I keep getting Catchable fatal error: Object of class stdClass could not be converted to string in /Applications/XAMPP/xamppfiles/htdocs/index.php on line 29

<?php   
    $apiUrl = 'https://api.quizlet.com/2.0/groups/44825?client_id=***BLOCKED FROM PUBLIC***&whitespace=1';

    $curl = curl_init($apiUrl);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $json = curl_exec($curl);
    if ($json) {
        $data = json_decode($json);

        echo "<h1>Sets from \"{$data->name}\"</h1>";

        foreach ($data->sets as $key => $val) {
         echo "$key: $val\n";
        }
        echo "</ul>";
        var_dump($data);
    }
    ?>
3
  • Can you also provide the actual var_dump of $data? If that's still not working, the "sets" attribute must not be an array. Commented Dec 14, 2011 at 21:22
  • @RyanLaBarre pastebin.com/qgWtFgxp Commented Dec 14, 2011 at 21:28
  • Aha! Sets is an array of nested objects, not an array of strings. So You cannot echo "val" since "val" is a sub-object. Editing my answer to reflect this for you, and then it'll work great! Commented Dec 15, 2011 at 0:49

3 Answers 3

2

You can/should use foreach to iterate over every element of an array.

$foo = new stdClass;
$foo->arr = array('1','7','heaven','eleven');

foreach ($foo->arr as $val)
{
    if (is_object($val)) var_dump($val);
    else echo $val;
}

Note the line I added to var_dump sub-objects. The error you were initially getting was that the elements of your sets array were also objects, not strings as expected. If you only need to access certain elements of the set objects, you can access them using $val->property.

Sign up to request clarification or add additional context in comments.

2 Comments

I get Catchable fatal error: Object of class stdClass could not be converted to string in /Applications/XAMPP/xamppfiles/htdocs/index.php on line 29
That error is not directly related to this code as it sits. What's on line 29 of that index file? Can you provide all the code, and an example of some actual data?
0

For example you have an object like

    $obj = new stdClass;
    $obj->foo = 'bar';
    $obj->arr = array('key' => 'val', ...);

    $array = (array) $obj;

now you can use foreach to iterate over array.

  foreach($array as $prop) {
    //now if you are not sure if it's an array or not
    if(is_array($prop)) {
        foreach($prop as $val)
        //do stuff
    }
    else {
        //do something else
    }
  }

Comments

0

The $val variable holds another object (of type stdClass) which contains the details for an individual "set". As you can see, since it generates an error, you cannot echo a stdClass object.

You can access the values inside each object using the object->property notation that you seems to be getting familiar with. For example.

foreach ($data->sets as $set) {
    echo $set->title . " by " . $set->created_by . "<br>";
}

/*

An example of the JSON object for a single $set
Access these like $set->title and $set->term_count

{
  "id": 8694763,
  "url": "http:\/\/quizlet.com\/8694763\/wh-test-1-2-flash-cards\/",
  "title": "WH Test 1 & 2",
  "created_by": "GrayA",
  "term_count": 42,
  "created_date": 1323821510,
  "modified_date": 1323821510,
  "has_images": false,
  "subjects": [
    "history"
  ],
  "visibility": "public",
  "editable": "groups",
}
*/

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.