Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何读取此数组(“stdClass对象”)_Php_Arrays_Json_Variables_Decode - Fatal编程技术网

Php 如何读取此数组(“stdClass对象”)

Php 如何读取此数组(“stdClass对象”),php,arrays,json,variables,decode,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] =>

我使用的是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 ) ) )
例如,如果我想得到第一组的名称,“用猫学西班牙语”,我如何通过变量来响应它

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

$data = json_decode($json);

你的对象不是数组,而是一个对象。因此,请使用
->
操作符访问其属性:

echo $data->name;
它包含一个属性,该属性本身是附加对象的数组。例如,要获取id为6081999的URL,您可以执行以下操作:

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

eg echo key($array)

下面是一个简单的解决方案,可以在php中使用
get\u Object\u vars

看看:

例如:

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

或者将
dump()
函数替换为
print\r()

当您使用json解码()时,我以前看过一些东西

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

{"1":"first","2":"second"}
但是,带有一个参数的json解码返回一个对象,第二个参数的默认值为“false”。如果您想将其返回到数组中,只需使用第二个参数并发送“true”


你可以把它当作一个数组

以防数组中有stdClass对象,例如
$user=$result1->fetch_object()
然后将
$user
设置为一个变量
$val=$user->user\u id
(确保user\u id是您的数据库列名,它是列名)
。您将获得一个来自数据库的$val值。

当您粘贴这样的转储时,请确保包含换行符。
json\u decode()
带有一个参数不会将json转换为数组。查看是否添加第二个参数
json\u decode($json,true)如果为TRUE,返回的对象将转换为关联数组。如果有多个集合的标识符为“name”,该怎么办?是不是
$data->name[1]
?@grayams我刚刚在编辑中提到了一个类似的问题。对于作为数组的集合,可以使用
[]
数组索引,然后通过
->
{"1":"first","2":"second"}
$data =json_decode($json,true);