访问对象的数组没有一个循环 [英] Accessing an array of objects without a loop
问题描述
我有一个返回名为 $结果
对象的数组SQL查询。 SQL查询有一个LIMIT 1,所以从未有数组中的对象(无需环路)的多个实例。该阵列是如下:
阵列(1){[0] =>对象(stdClass的)#5875(1){[meta_value] =>
串(3)是}}
块引用>该meta_value对象可以是'yes或否。我想直接访问meta_value这样我就可以用它在一个状态输出,我苦苦寻找的,虽然这样做的方法。我的code是低于,这不过是无效的PHP
如果($结果> meta_value =='是'){
//做些好事
}
解决方案原因
$结果
为对象数组您应该选择先用元素首页0
,然后采取一定的属性:如果($结果[0] - > meta_value =='是'){}
I have an SQL query that returns an array of objects called
$results
. The SQL query has a 'LIMIT 1', so there are never multiple instances of an object in the array (no need to loop). The array is below:array(1) { [0]=> object(stdClass)#5875 (1) { ["meta_value"]=> string(3) "yes" } }
The meta_value object can either be 'yes or 'no'. I'd like to access 'meta_value' directly so I can use it's output in a condition, I'm struggling to find a method of doing this though. My code is below, this is not valid PHP however
if ($results->meta_value == 'yes') { //do something nice }
解决方案Cause
$results
is array of objects you should select first element with index0
and then take a certain property:if ($results[0]->meta_value == 'yes') {}
这篇关于访问对象的数组没有一个循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!