简体   繁体   English

php:获取数组内的变量

[英]php: get a variable inside an array

I have this array called $locations and this is what it produces if I use print_r : 我有一个名为$locations数组,这是我使用print_r时产生的结果:

Array
(
    [0] => stdClass Object
    (
        [term_id] => 40
        [name] => California
        [slug] => california
        [term_group] => 0
        [term_taxonomy_id] => 41
        [taxonomy] => location
        [description] =>
        [parent] => 0
        [count] => 6
     )
) 

What I need to get from this array is California only, but I can't figure out what variable produces that. 我需要从此数组中获得的只是California ,但是我无法弄清楚是什么变量导致了该变量。 I tried $locations->name and $locations[name] but none of those work. 我尝试了$locations->name$locations[name]但是这些都不起作用。

$ locations似乎是一个数组(具有1个值,即一个对象),因此:

$locations[0]->name

Just try with: 只需尝试:

$data = array( /* your data */ );

foreach ($data as $obj) {
  $name = $obj->name; // California - and others in the nest steps 
                      // if array contains more elements
}

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

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