2

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).

Cris
  • 4,004
  • 16
  • 50
  • 74

5 Answers5

5

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.)

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • This is a good approach. Although this will duplicate the array, generating unnecessary memory clutter. It's better to catch the essence and only find out what the `key` is and call it. –  Mar 06 '13 at 22:44
1

You could also just use

$array = array_pop($array);

And then to get the name element:

$array['name']
Fabian Tamp
  • 4,416
  • 2
  • 26
  • 42
0

You can try something like this:

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

Now you should have access to the value you want.

tkestowicz
  • 332
  • 1
  • 4
  • 15
0

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>';
?>
-1

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

nap.gab
  • 451
  • 4
  • 19