如何在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
谢谢!
试试这个:
function getLastId(&$array){
$tmp=end($array);
return $tmp['id'];
}
如何计算元素并像这样访问它们?
$array[count($array)-1] # to get the last array item
$array[count($array)-1]['id'] # to get the id of the last entry