简体   繁体   English

PHP echo单个2d数组值

[英]PHP echo single 2d array value

I have a 2d array $locations that is the result of a sql query. 我有一个2d数组$位置,这是一个SQL查询的结果。 I can use the foreach function to get all of the rows, like this, and it works perfectly: 我可以使用foreach函数来获取所有行,就像这样,它完美地运行:

                 foreach($locations as $row) {
                    echo $row->NICKNAME;
                    echo $row->POC; 
                    }

I just want to grab the first row of the index NICKNAME in the array. 我只想抓住数组中索引NICKNAME的第一行。 I try 我试试

echo $locations["NICKNAME"][0];

and it says "Undefined index: NICKNAME" 它说“未定义的索引:NICKNAME”

I try: 我尝试:

echo $locations[0][0];

and it says "Cannot use object of type stdClass as array" 它说“不能使用stdClass类型的对象作为数组”

When I echo gettype($locations) it prints the word array, and the foreach function (which is only for arrays right?) works, so I really don't understand that error. 当我回显gettype($ locations)时,它打印出单词数组,而foreach函数(只适用于数组对吗?)有效,所以我真的不明白这个错误。

I know this is simple but I don't know what else to try and Googling hasn't helped. 我知道这很简单,但我不知道还有什么可以尝试,谷歌搜索没有帮助。

Try using this as $location is an array of objects and to reference each object, you have to use $location along with the key of the object you want to select. 尝试使用此作为$ location是一个对象数组并引用每个对象,您必须使用$ location以及要选择的对象的键。 Once selected, use the the Nickname from it as a regular object property. 选择后,使用其中的昵称作为常规对象属性。

echo $locations[0]->NICKNAME;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM