如何在PHP中动态显示最后一个数组中的元素。 例如:
Array ( [0] => Array ( [id] => 6 [user_id] => 8 [category_path] => Sport)
[1] => Array ( [id] => 8 [user_id] => 8 [category_path] => Computers))
以便从最后一个数组返回“id”
8
对于下一个例子
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))
以便返回
16
谢谢!
答案 0 :(得分:10)
试试这个:
function getLastId(&$array){
$tmp=end($array);
return $tmp['id'];
}
答案 1 :(得分:2)
如何计算元素并像这样访问它们?
$array[count($array)-1] # to get the last array item
$array[count($array)-1]['id'] # to get the id of the last entry