30

Consider the following array

$array = array('fruit'     => 'apple',
               'vegetable' => 'potato',
               'dairy'     => 'cheese');

I wanted to use array_pop to get the last key/value pair.

However, one will note that after the following

$last = array_pop($array);

var_dump($last);

It will output only the value (string(6) "cheese")

How can I "pop" the last pair from the array, preserving the key/value array structure?

7 Answers 7

23

Check out array_slice() http://php.net/manual/en/function.array-slice.php

Last argument true is to preserve keys.

When you pass the offset as negative, it starts from the end. It's a nice trick to get last elements without counting the total.

$array = [
    "a" => 1,
    "b" => 2,
    "c" => 3,
];

$lastElementWithKey = array_slice($array, -1, 1, true);

print_r($lastElementWithKey);

Outputs:

Array
(
    [c] => 3
)
3
  • 2
    Ensure you set preserve_keys = true if your keys are numeric or you may not get the pair you expected!
    – Ella Ryan
    Jan 5, 2014 at 13:10
  • 2
    This will work but doing a triple inline statement as the example may be confusing for newcomers, definitely don't recommend that part in your code.
    – sricks
    Jun 3, 2016 at 18:06
  • 1
    This answer doesn't modify the array. Yes, for getting the last key/value pair, it's efficient enough, but the question wants "popping" and getting the last key/value pair. So, you should add an array_pop() after that (which reduces performance). Jul 10, 2019 at 10:00
10

try

end($array); //pointer to end
each($array); //get pair
2
  • 4
    Note: each() is deprecated as of PHP 7.2.0 (Current minor at time of comment.)
    – martixy
    Sep 27, 2018 at 10:47
  • each() function is removed as of PHP 8.0. Nov 29, 2021 at 16:47
6

You can use end() and key() to the the key and the value, then you can pop the value.

$array = array('fruit' => 'apple', 'vegetable' => 'potato', 'dairy' => 'cheese');
$val = end($array); // 'cheese'
                    // Moves array pointer to end
$key = key($array); // 'dairy'
                    // Gets key at current array position
array_pop($array); // Removes the element
                   // Resets array pointer
0
6

Why not using new features? The following code works as of PHP 7.3:

// As simple as is!
$lastPair = [array_key_last($array) => array_pop($array)];

The code above is neat and efficient (as I tested, it's about 20% faster than array_slice() + array_pop() for an array with 10000 elements; and the reason is that array_key_last() is really fast). This way the last value will also be removed.

Tip: You can also extract key and value separately:

[$key, $value] = [array_key_last($array), array_pop($array)];
2

This should work, just don't do it inside a foreach loop (it'll mess up the loop)

end($array); // set the array pointer to the end
$keyvaluepair = each($array);  // read the key/value
reset($array); // for good measure

Edit: Briedis suggests array_slice() which is probably a better solution

1
  • each() function is deprecated as of PHP 7.2, and is removed as of PHP 8.0. Nov 29, 2021 at 16:47
0

Another option:

<?php
end($array);
list($key, $value) = each($array);
array_pop($array);
var_dump($key, $value);
?>
0

Try this:

<?php

function array_end($array)
{
    $val = end($array);
    return array(array_search($val, $array) => $val);
}

$array = array(
    'fruit' => 'apple',
    'vegetable' => 'potato',
    'dairy' => 'cheese'
);

echo "<pre>";
print_r(array_end($array));
?>

Output:

Array
(
    [dairy] => cheese
)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.