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

0

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

You could also just use

$array = array_pop($array);

And then to get the name element:

$array['name']

4 Comments

As this is, it wouldn't work. -- Even after the edit, this still wouldn't work.
@MattPsyK Yeah I realised it was a bit hacky. Changed it :)
You know you still only return the array, your answer doesn't get the answer as in the original question.
I disagree on that. But, I've edited my answer anyway.
0

You can try something like this:

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

Now you should have access to the value you want.

Comments

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

Comments

-1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.