我该怎么读这个数组(" stdClass对象")

时间:2011-12-14 20:04:14

标签: php arrays json variables decode

我使用的是Quizlet API 2.0,我对这个

很新

如何从以下内容中读取值:

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

例如,如果我想获得第一组的名称,“用Cats学习西班牙语”,我该如何通过变量回应它?

它已经将JSON转换为我认为的数组:

$data = json_decode($json);

5 个答案:

答案 0 :(得分:13)

您的对象不是数组,而是对象。因此,请使用->运算符访问其属性:

echo $data->name;

它包含一个属性,它本身就是一个附加对象的数组。例如,要获取ID为6081999的URL,您可以执行以下操作:

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

答案 1 :(得分:1)

使用功能键

eg echo key($array)

答案 2 :(得分:1)

这是一个简单的解决方案,用get_object_vars

转换php中数组中的 stdClass对象

请注意:http://php.net/manual/fr/function.get-object-vars.php

前:

debug($array);
$var = get_object_vars($array);
debug($var);

或者将debug替换为print_r

CDT

答案 3 :(得分:1)

当你使用json_decode()

时,我已经看过了
$data = json_decode();

你可以发送一些参数,第一个是" $ json",它将是json字符串

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

但是,带有一个参数的json解码返回一个Object,第二个参数的默认值是" false"。如果你想要回到数组中,你只需要使用第二个参数并发送" true"。

$data =json_decode($json,true);

你可以像数组一样重新调用它。 :)

答案 4 :(得分:0)

例如,如果数组中有stdClass对象,则
$user = $result1->fetch_object()然后将$user设置为变量   $val = $user->user_id(确保user_id是您的数据库列名称,及其列名称)。您将在$ val中获得一个来自数据库的值。