带有一个元素但索引未知的PHP数组。 如何获得?

A function is returning arrays which look like

( [4] => SomeObject )

or

( [2] => SomeObject )

If I know the array will always have only one child how can I access it without knowing the index?

I'm sure PHP has an equivalent to Javascript's getOwnPropertynames, but I would think there must be a more direct way.

Thanks!

You can use current:

$value = current($arr);

current returns the value of the element currently pointed to by the array's internal pointer. In this case, that will be the first (and only) element of the array.

Testing it out:

function f() {
    return array(4 => 'foo');
}

$arr = f();
echo current($arr);

Output:

foo