从Laravel Database查询结果访问明确存在的整数类型数据时出错“无法使用stdClass类型的对象作为数组”
I got my data from Laravel database query command:
$group = DB::table('groups')->where("id", $group_id)->first();
When I var dump my data, I get:
object(stdClass)#200 (7) {
["id"]=>
int(1)
["levels_id"]=>
int(1)
["title"]=>
string(8) "Novice 1"
["description"]=>
string(11) "Lorem Ipsum"
["max_question_display"]=>
int(5)
["created_at"]=>
NULL
["updated_at"]=>
NULL
}
I want to access the max_question_display
. But when I do:
var_dump($group["max_question_display"]);
PHP returns error Cannot use object of type stdClass as array
.
When I do:
var_dump($group->max_question_display);
I get:
int(5)
But I don't want the int
. I only want the 5
. In integer form.
If I foreach
loop the $group
:
foreach ($group as $t) {
echo "<pre>";
var_dump($t);
echo "</pre>";
}
I get each of the data as a single data each loop.
int(1)
int(1)
string(8) "Novice 1"
string(11) "Lorem Ipsum"
int(5)
NULL
NULL
This is obviously also not the way the result accessed that I'm looking for.
I also tried to get the first element of array, thinking that this might be an array with 1 element, but that also raise the same error.
I get it that the general answer in this site about this error is that "stdClass is not array". I have browsed several question with similar title like mine, but nothing address object that came from Laravel DB. When I read the manual on Laravel DB, I was assured that I can access the data returned like a simple dictionary / hashmap.
EDIT: Sorry, I understand my very, very newbie mistakes. No need to answer this. Thanks.
我从Laravel数据库查询命令获取数据: p>
$ group = DB :: table('groups') - &gt; where(“id”,$ group_id) - &gt; first();
code> pre>
当我 var dump我的数据,我得到: p>
object(stdClass)#200(7){
[“id”] =&gt;
int(1)
[“levels_id”] =&gt;
int(1)
[“title”] =&gt;
string(8)“新手1”
[“description”] =&gt;
string(11) “Lorem Ipsum”
[“max_question_display”] =&gt;
int(5)
[“created_at”] =&gt;
NULL
[“updated_at”] =&gt;
NULL
} \ n code> pre>
我想访问 max_question_display code>。 但是当我这样做时: p>
var_dump($ group [“max_question_display”]);
code> pre>
PHP返回错误 不能使用stdClass类型的对象作为数组 code>。 p>
当我这样做时: p>
var_dump($ group- &gt; max_question_display);
code> pre>
我得到: p>
int(5)
code>
但我不想要 int code>。 我只想要 5 code>。 整数形式。 p>
如果我 foreach code>循环 $ group code>: p>
foreach($ group as $ t){
echo“&lt; pre&gt;”;
var_dump($ t);
echo“&lt; / pre&gt;”;
}
code> pre>
我将每个数据作为每个循环的单个数据。 p>
int(1)
int(1)
string(8) “新手1”
string(11)“Lorem Ipsum”
int(5)
NULL
NULL
code> pre>
这显然也不是结果访问的方式 我正在寻找。 p>
我也尝试获取数组的第一个元素,认为这可能是一个包含1个元素的数组,但这也会引发同样的错误。 p >
我知道这个网站关于这个错误的一般答案是“stdClass不是数组”。 我浏览了几个类似标题的问题,比如我的,但没有任何地址来自Laravel DB。 当我阅读Laravel DB上的手册时,我确信我可以像简单的字典/散列图一样访问返回的数据。 p>
编辑:对不起,我理解我非常非常新手的错误。 没必要回答这个问题。 谢谢。 p>
div>
Notice the first line of your first var_dump:
object(stdClass)#200
Because you're dealing with an object, you access its properties with ->
. When you do:
var_dump($group->max_question_display);
The reason you see (int)
in the output is that the var_dump
function shows the value type, next to the value. To access the value, do
$group->max_question_display;
If you want to see it on screen without the type, use echo
echo $group->max_question_display; // 5
stdClass is an object. You cannot use an object with array syntax to access its properties, if the class does not implement ArrayAccess interface.
As pointed out by @IbrahimLawal , var_dump outputs both the type and value. Just echoing $group->max_question_display
will provide just the value
echo $group->max_question_display; // 5
In Summary: You must use arrow syntax when interacting with stdClass.