如何获取另一个数组内的数组的值[重复]

如何获取另一个数组内的数组的值[重复]

问题描述:

This question already has an answer here:

I have this:

Array
(
  [28] => Array
    (
        [name] => HTC Touch HD
    )
)

There's only one array inside the main array and I only the value of name. Problem is that I don't know the index (28).

</div>

此问题已经存在 这里有一个答案: p>

  • 获取数组的第一个元素 37 answers span> li>
  • 多维数组 3 answers span> li> ul> div >

    我有这个: p>

      Array 
    (
     [28] =&gt; Array 
    (
     [name] =&gt;  HTC Touch HD 
    )
    )
      code>  pre> 
     
     

    主阵列中只有一个数组,而我只有name的值。 问题是我不知道索引(28)。 p> div>

You could use array_values just in general to get rid of any weird keys:

$normal = array_values($arr);
$normal[0]['name']

Or in this particular case, end, which is only a little bit hacky:

end($normal)['name']

http://codepad.viper-7.com/cApBjK

(Yep, reset and first and such work too.)

You could also just use

$array = array_pop($array);

And then to get the name element:

$array['name']

If you don't know the structure of an array, you can use foreach construct.

You can try something like this:

    reset($outerArray);
    $innerArray = current($outerArray);

Now you should have access to the value you want.

Pretty self-explanatory :)

<?php
$array = array(
    28 => array(
        'name' => 'HTC Touch HD'
    )
);

$key = current(array_keys($array));

echo '<pre>';
print_r($array[$key]);
echo '</pre>';
?>