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


Array - how to replace record with newly added

>我得到了下面的数组,我想保留最近添加的记录[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] => ) )

我得到了答案,感谢您的帮助。

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

更短:

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