我将如何读取这个数组(“stdClass Object”)

Posted

技术标签:

【中文标题】我将如何读取这个数组(“stdClass Object”)【英文标题】:How would I read this array ("stdClass Object") 【发布时间】:2012-01-20 14:34:45 【问题描述】:

我正在使用 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 ) ) )

例如,如果我想获得第一个集合的名称“Learn Spanish with Cats”,如何通过变量回显它?

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

$data = json_decode($json);

【问题讨论】:

当您像这样粘贴转储文件时,请确保包含换行符。 json_decode() 带有一个参数不会将 JSON 转换为数组。见php.net/json_decode 如果添加第二个参数json_decode($json,true); TRUE 时,返回的对象将被转换为关联数组。 【参考方案1】:

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

echo $data->name;

它包含一个属性,该属性本身就是一个附加对象的数组。例如,要获取 id 6081999 的 URL,您可以:

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

【讨论】:

如果有多个以“name”为标识符的集合怎么办?会是$data->name[1]吗? @GrayAdams 我刚刚在编辑中解决了一个类似的问题。对于作为数组的集合,您可以使用 [] 数组索引,然后通过 -> 使用所需的对象属性【参考方案2】:

这是一个简单的解决方案,用get_object_vars在php中转换数组中的stdClass Object

看:https://www.php.net/manual/en/function.get-object-vars.php

例子:

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

或者用print_r()替换dump()函数

【讨论】:

【参考方案3】:

使用功能键

eg echo key($array)

【讨论】:

【参考方案4】:

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

$data = json_decode();

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

"1":"first","2":"second"

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

$data =json_decode($json,true);

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

【讨论】:

【参考方案5】:

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

【讨论】:

以上是关于我将如何读取这个数组(“stdClass Object”)的主要内容,如果未能解决你的问题,请参考以下文章

我将数组的旧元素保存在新数组中。我该如何解决这个问题?

php如何序列化对象数组?

如何从 MATLAB 的 audioread 等 libsndfile 库中读取数组格式的音频文件

我将如何减少这个数组,以便每个对象中的对象被合并 javascript 但不知道键值名称

我如何在闭包之外访问数组?

将 CSV 文件读取到 numpy 数组,第一行为字符串,其余为浮点数