How to get the first item from an associative PHP array?

后端 未结 15 2015
温柔的废话
温柔的废话 2021-01-31 00:33

If I had an array like:

$array[\'foo\'] = 400;
$array[\'bar\'] = \'xyz\';

And I wanted to get the first item out of that array without knowing

相关标签:
15条回答
  • 2021-01-31 01:15

    you can just use $array[0]. it will give you the first item always

    0 讨论(0)
  • 2021-01-31 01:16

    You could use array_shift

    0 讨论(0)
  • 2021-01-31 01:17

    reset() gives you the first value of the array if you have an element inside the array:

    $value = reset($array);
    

    It also gives you FALSE in case the array is empty.

    0 讨论(0)
  • 2021-01-31 01:17

    PHP < 7.3

    If you don't know enough about the array (you're not sure whether the first key is foo or bar) then the array might well also be, maybe, empty.

    So it would be best to check, especially if there is the chance that the returned value might be the boolean FALSE:

    $value = empty($arr) ? $default : reset($arr);
    

    The above code uses reset and so has side effects (it resets the internal pointer of the array), so you might prefer using array_slice to quickly access a copy of the first element of the array:

    $value = $default;
    foreach(array_slice($arr, 0, 1) as $value);
    

    Assuming you want to get both the key and the value separately, you need to add the fourth parameter to array_slice:

    foreach(array_slice($arr, 0, 1, true) as $key => $value);
    

    To get the first item as a pair (key => value):

    $item = array_slice($arr, 0, 1, true);
    

    Simple modification to get the last item, key and value separately:

    foreach(array_slice($arr, -1, 1, true) as $key => $value);
    

    performance

    If the array is not really big, you don't actually need array_slice and can rather get a copy of the whole keys array, then get the first item:

    $key = count($arr) ? array_keys($arr)[0] : null;
    

    If you have a very big array, though, the call to array_keys will require significant time and memory more than array_slice (both functions walk the array, but the latter terminates as soon as it has gathered the required number of items - which is one).

    A notable exception is when you have the first key which points to a very large and convoluted object. In that case array_slice will duplicate that first large object, while array_keys will only grab the keys.

    PHP 7.3+

    PHP 7.3 onwards implements array_key_first() as well as array_key_last(). These are explicitly provided to access first and last keys efficiently without resetting the array's internal state as a side effect.

    So since PHP 7.3 the first value of $array may be accessed with

    $array[array_key_first($array)];
    

    You still had better check that the array is not empty though, or you will get an error:

    $firstKey = array_key_first($array);
    if (null === $firstKey) {
        $value = "Array is empty"; // An error should be handled here
    } else {
        $value = $array[$firstKey];
    }
    
    0 讨论(0)
  • 2021-01-31 01:18

    I do this to get the first and last value. This works with more values too.

    $a = array(
        'foo' => 400,
        'bar' => 'xyz',
    );
    $first = current($a);   //400
    $last = end($a);    //xyz
    
    0 讨论(0)
  • 2021-01-31 01:19

    You can try this.

    To get first value of the array :-

    <?php
       $large_array = array('foo' => 'bar', 'hello' => 'world');
       var_dump(current($large_array));
    ?>
    

    To get the first key of the array

    <?php
       $large_array = array('foo' => 'bar', 'hello' => 'world');
       $large_array_keys = array_keys($large_array);
       var_dump(array_shift($large_array_keys));
    ?>
    
    0 讨论(0)
提交回复
热议问题