Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return an element from the last array in a multidimensional array in PHP

How to display an element from the last array dynamically in PHP. For example:

Array ( [0] => Array ( [id] => 6 [user_id] => 8 [category_path] => Sport)
        [1] => Array ( [id] => 8 [user_id] => 8 [category_path] => Computers))

in order to return the "id" from the last array

8

And for the next example

Array ( [0] => Array ( [id] => 6 [user_id] => 8 [category_path] => Sport)
        [1] => Array ( [id] => 8 [user_id] => 5 [category_path] => Computers)
        [2] => Array ( [id] => 16 [user_id] => 45 [category_path] => Soft))

in order to return

   16

Thank you!

like image 585
Alfonso Fernandez-Ocampo Avatar asked Dec 13 '22 09:12

Alfonso Fernandez-Ocampo


1 Answers

Try this:

function getLastId(&$array){
    $tmp=end($array);
    return $tmp['id'];
}
like image 77
stewe Avatar answered Jan 18 '23 22:01

stewe