数组 - 如何用新添加的记录替换记录

时间:2014-12-13 04:18:29

标签: php arrays

我得到了下面的数组,我想保留最近添加的记录[2]并删除其他

Array ( 
[0] => Array ( [id] => 1 [is_sub] => 0 [product] => New [quantity] => 1 [price] => [total_item_price] => 0 [comments] => ) 
[1] => Array ( [id] => 1 [is_sub] => 0 [product] => Old [quantity] => 1 [price] => [total_item_price] => 0 [comments] => ) 
[2] => Array ( [id] => 4 [is_sub] => 0 [product] => Mix [quantity] => 1 [price] => [total_item_price] => 0 [comments] => ) )

3 个答案:

答案 0 :(得分:0)

我得到了一个答案谢谢你的帮助。

$last = array_slice($orders, -1, 1, true);

答案 1 :(得分:0)

更短的:

$last = array_pop($orders);

答案 2 :(得分:0)

$last_data = array_pop($data);
print_r($last_data);