简体   繁体   中英

Array - how to replace record with newly added

I got the array below and I would like to keep recent added record which is [2] and delete others

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM