Get last key-value pair in PHP array

前端 未结 9 686
别那么骄傲
别那么骄傲 2020-12-29 01:20

I have an array that is structured like this:

[33] => Array
    (
        [time] => 1285571561
        [user] => test0
    )

[34] => Array
    (         


        
9条回答
  •  [愿得一人]
    2020-12-29 02:11

    "SPL-way":

    $splArray = SplFixedArray::fromArray($array);
    $last_item_with_preserved_index[$splArray->getSize()-1] = $splArray->offsetGet($splArray->getSize()-1);
    

    Read more about SplFixedArray and why it's in some cases ( especially with big-index sizes array-data) more preferable than basic array here => The SplFixedArray class.

提交回复
热议问题