php 我将如何读取这个数组(“stdClass 对象”)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8510917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:53:08  来源:igfitidea点击:

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

phparraysjsonvariablesdecode

提问by Gray Adams

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

我正在使用 Quizlet API 2.0,我对此很陌生

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:

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

$data = json_decode($json);

回答by Michael Berkowski

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:

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

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

回答by Breith

Here is a simple solution to convert a stdClass Objectin array in php with get_object_vars

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

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

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

Example :

例子 :

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

Or replace dump()function by print_r()

或替换dump()功能print_r()

回答by Ramssés Gómez

I have looked something before, when you use the json_decode()

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

$data = json_decode();

U can send some parameters, the first of them is "$json", it will be the json string

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

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

But that json decode with one parameter return an Object and the default value of the second parameter is "false". If you want that back in array you only need to use the second parameter and send "true".

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

$data =json_decode($json,true);

And you can recibe it like an array. :)

你可以把它当作一个数组。:)

回答by Shashank Agarwal

Use function key

使用功能键

eg echo key($array)

回答by Rohit Singh

In case you got stdClass Object in the array, for example
$user = $result1->fetch_object()then set $userinto a variable $val = $user->user_id(make sure user_id is your database column name, its the column name). You will get a single value in $val that comes from database.

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