在PHP中从数组中获取值

时间:2021-02-27 13:39:38

I'm trying to get the value out of an array in PHP.

我试图从PHP中的数组中获取值。

The value of $v when displayed with print_r($v) is as follows:

使用print_r($ v)显示的$ v值如下:

Array ( [0] => Array ([name] => BLARGH ) 
        [1] => Array ( [name] => TEMP CATEGORY ) 
       )

I'm trying to iterate over this and pull out the value of the name key as follows:

我正在尝试迭代这个并提取名称键的值,如下所示:

foreach($v as $category) {
 echo  $category->name; 
}

The echo returns no value. Further, if I add a print_r($category) to the loop I get a return of

echo不返回任何值。此外,如果我在循环中添加print_r($ category),我会得到一个返回

Array ( [name] => TEMP CATEGORY )

How do I get the name value out of the array?

如何从数组中获取名称值?

5 个解决方案

#1


4  

inside your foreach loop do $category['name']

在你的foreach循环中做$ category ['name']

#2


2  

This is an array, not an object. Use array notation:

这是一个数组,而不是一个对象。使用数组表示法:

echo $category['name'];

See here: http://3v4l.org/gPL27

见这里:http://3v4l.org/gPL27

#3


2  

foreach($v as $category) {
   echo  $category['name']; 
}

#4


2  

foreach($v as $category) {

   echo  $category['name']; 

}

what you did wrong:

你做错了什么:

in this case:

在这种情况下:

$catagory->name

$category would need to be an object, not an array

$ category需要是一个对象,而不是一个数组

#5


-2  

<?php
foreach($v as $key => $value){
    echo $key;
    echo $value;
}
?>

#1


4  

inside your foreach loop do $category['name']

在你的foreach循环中做$ category ['name']

#2


2  

This is an array, not an object. Use array notation:

这是一个数组,而不是一个对象。使用数组表示法:

echo $category['name'];

See here: http://3v4l.org/gPL27

见这里:http://3v4l.org/gPL27

#3


2  

foreach($v as $category) {
   echo  $category['name']; 
}

#4


2  

foreach($v as $category) {

   echo  $category['name']; 

}

what you did wrong:

你做错了什么:

in this case:

在这种情况下:

$catagory->name

$category would need to be an object, not an array

$ category需要是一个对象,而不是一个数组

#5


-2  

<?php
foreach($v as $key => $value){
    echo $key;
    echo $value;
}
?>