开发者

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!


Try this:

function getLastId(&$array){
    $tmp=end($array);
    return $tmp['id'];
}


How about counting the elements and accessing them like this?

$array[count($array)-1] # to get the last array item
$array[count($array)-1]['id'] # to get the id of the last entry
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜