Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I read this array ("stdClass Object")

I am using the Quizlet API 2.0, and I am pretty new to this

How do I read a value(s) from something like this:

stdClass Object ( [id] => 102269 [name] => Learn Spanish with Cats! [set_count] => 3 [user_count] => 10 [created_date] => 1308035691 [is_public] => 1 [has_password] => [has_access] => 1 [has_discussion] => 1 [member_add_sets] => 1 [description] => This set is exclusively for Spanish flashcard sets with relevant cat images as the set definitions. [sets] => Array ( [0] => stdClass Object ( [id] => 6081999 [url] => http://quizlet.com/6081999/lesson-4-with-catsdogs-flash-cards/ [title] => Lesson 4 (with cats+dogs) [created_by] => wsvincent [term_count] => 33 [created_date] => 1311984796 [modified_date] => 1312490710 [has_images] => 1 [subjects] => Array ( [0] => spanish cats dogs ) [visibility] => public [editable] => groups [has_access] => 1 ) [1] => stdClass Object ( [id] => 5855751 [url] => http://quizlet.com/5855751/paso-a-paso-book-1-chapter-4-flash-cards/ [title] => Paso a Paso Book 1 Chapter 4 [created_by] => catdawg426 [term_count] => 30 [created_date] => 1307761267 [modified_date] => 1307819129 [has_images] => 1 [subjects] => Array ( [0] => spanish ) [visibility] => public [editable] => only_me [has_access] => 1 ) [2] => stdClass Object ( [id] => 5873819 [url] => http://quizlet.com/5873819/los-gatos-de-viaje-flash-cards/ [title] => Los Gatos de Viaje! [created_by] => tiffreja [term_count] => 21 [created_date] => 1307996657 [modified_date] => 1307996796 [has_images] => 1 [subjects] => Array ( [0] => spanish [1] => language [2] => foreign ) [visibility] => public [editable] => only_me [has_access] => 1 ) ) [members] => Array ( [0] => stdClass Object ( [username] => philfreo [role] => creator [email_notification] => 1 ) [1] => stdClass Object ( [username] => milkncookies [role] => member [email_notification] => 1 ) [2] => stdClass Object ( [username] => Icypaw [role] => member [email_notification] => ) [3] => stdClass Object ( [username] => luckycat10 [role] => member [email_notification] => ) [4] => stdClass Object ( [username] => jeffchan [role] => member [email_notification] => ) [5] => stdClass Object ( [username] => catchdave [role] => member [email_notification] => 1 ) [6] => stdClass Object ( [username] => tiffreja [role] => member [email_notification] => 1 ) [7] => stdClass Object ( [username] => catdawg426 [role] => member [email_notification] => 1 ) [8] => stdClass Object ( [username] => ihaque [role] => member [email_notification] => 1 ) [9] => stdClass Object ( [username] => jalenack [role] => member [email_notification] => 1 ) ) )

For instance, if I want to get the name of that first set, "Learn Spanish with Cats", how do I echo it via variable?

It already converts the JSON to an array I think:

$data = json_decode($json);
like image 899
Gray Adams Avatar asked Dec 14 '11 20:12

Gray Adams


People also ask

How do you access the stdClass object?

Below are two examples of how to access a StdClass object one will expose the entire object, and the other will grab one property of that object. $myNameIs $data->{'name'}; Accessing the whole object called $data. The for-each loop adds it to $array, and then by using the print_r function, it will display everything.

What is a stdClass object?

The stdClass is the empty class in PHP which is used to cast other types to object. It is similar to Java or Python object. The stdClass is not the base class of the objects. If an object is converted to object, it is not modified.

How do you convert a std class object to an array?

Convert stdClass objects to array Suppose we have the following stdClass objects. #define stdObjects to store employee details $obj = new stdClass; $obj->name = "Priska"; $obj->position = "Data Administrator" $obj->age = 26; $obj->experience = 3; The following PHP code converts the above defined objects into an array.


1 Answers

Your object is not an array, but rather, well, an Object. So use the -> operator to access its properties:

echo $data->name;

It contains a property which itself is an array of additional objects. For example, to get the URL of id 6081999, you would do:

echo $data->sets[0]->url;
// http://quizlet.com/6081999/lesson-4-with-catsdogs-flash-cards/
like image 70
Michael Berkowski Avatar answered Sep 28 '22 06:09

Michael Berkowski