2011-12-14 56 views
2

我使用Quizlet API 2.0,和我很新的这我将如何读这个阵列(“stdClass的对象”)

我如何读的东西值(一个或多个)是这样的:

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))) 

例如,如果我想获得第一组名称,“与猫一起学习西班牙语”,我如何通过变量回显它?

这已经是JSON转换成一个数组,我认为:

$data = json_decode($json); 
+0

当您粘贴这样的转储时,请确保您包含换行符。 – 2011-12-14 20:07:16

+0

带有一个参数的`json_decode()`不会将JSON转换为数组。请参阅http://php.net/json_decode – salathe 2011-12-14 20:14:30

回答

12

你的对象不是一个数组,而是很好,一个对象。所以使用->操作符来访问它的属性:

echo $data->name; 

它包含一个属性,它本身是其他对象的数组。例如,为了获得ID 6081999的网址,你会怎么做:

echo $data->sets[0]->url; 
// http://quizlet.com/6081999/lesson-4-with-catsdogs-flash-cards/ 
1

使用功能键

eg echo key($array) 
1

我以前看过的东西,当你使用json_decode()

$data = json_decode(); 

U可以发送一些参数,他们的第一个是“$ json”,它将是json字符串

{"1":"first","2":"second"} 

但是,json解码一个参数返回一个对象,第二个参数的默认值是“false”。如果你想要返回数组,只需要使用第二个参数并发送“true”。

$data =json_decode($json,true); 

而且你可以像数组一样对它进行描述。 :)