Get the first element of an array

后端 未结 30 2034
醉酒成梦
醉酒成梦 2020-11-22 10:59

I have an array:

array( 4 => \'apple\', 7 => \'orange\', 13 => \'plum\' )

I would like to get the first element of this array. Expect

相关标签:
30条回答
  • 2020-11-22 11:22

    I would do echo current($array) .

    0 讨论(0)
  • 2020-11-22 11:22

    This is a little late to the game, but I was presented with a problem where my array contained array elements as children inside it, and thus I couldn't just get a string representation of the first array element. By using PHP's current() function, I managed this:

    <?php
        $original = array(4 => array('one', 'two'), 7 => array('three', 'four'));
        reset($original);  // to reset the internal array pointer...
        $first_element = current($original);  // get the current element...
    ?>
    

    Thanks to all the current solutions helped me get to this answer, I hope this helps someone sometime!

    0 讨论(0)
  • 2020-11-22 11:23

    Some arrays don't work with functions like list, reset or current. Maybe they're "faux" arrays - partially implementing ArrayIterator, for example.

    If you want to pull the first value regardless of the array, you can short-circuit an iterator:

    foreach($array_with_unknown_keys as $value) break;
    

    Your value will then be available in $value and the loop will break after the first iteration. This is more efficient than copying a potentially large array to a function like array_unshift(array_values($arr)).

    You can grab the key this way too:

    foreach($array_with_unknown_keys as $key=>$value) break;
    

    If you're calling this from a function, simply return early:

    function grab_first($arr) {
        foreach($arr as $value) return $value;
    }
    
    0 讨论(0)
  • 2020-11-22 11:24

    Use:

    $first = array_slice($array, 0, 1);  
    $val= $first[0];
    

    By default, array_slice does not preserve keys, so we can safely use zero as the index.

    0 讨论(0)
  • 2020-11-22 11:25

    I think using array_values would be your best bet here. You could return the value at index zero from the result of that function to get 'apple'.

    0 讨论(0)
  • 2020-11-22 11:26

    Use array_keys() to access the keys of your associative array as a numerical indexed array, which is then again can be used as key for the array.

    When the solution is arr[0]:

    (Note, that since the array with the keys is 0-based index, the 1st element is index 0)

    You can use a variable and then subtract one, to get your logic, that 1 => 'apple'.

    $i = 1;
    $arr = array( 4 => 'apple', 7 => 'orange', 13 => 'plum' );
    echo $arr[array_keys($arr)[$i-1]];
    

    Output:

    apple
    

    Well, for simplicity- just use:

    $arr = array( 4 => 'apple', 7 => 'orange', 13 => 'plum' );
    echo $arr[array_keys($arr)[0]];
    

    Output:

    apple
    

    By the first method not just the first element, but can treat an associative array like an indexed array.

    0 讨论(0)
提交回复
热议问题