通过值删除阵列项目 [英] Removing array item by value
本文介绍了通过值删除阵列项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要与给定值去除数组项:
I need to remove array item with given value:
if (in_array($id, $items)) {
$items = array_flip($items);
unset($items[ $id ]);
$items = array_flip($items);
}
难道是在更短的(更有效)的方式完成?
Could it be done in shorter (more efficient) way?
推荐答案
它可以用一个简单的一行来完成。
It can be accomplished with a simple one-liner.
有了这个数组:
$arr = array('nice_item', 'remove_me', 'another_liked_item', 'remove_me_also');
您可以这样做:
$arr = array_diff($arr, array('remove_me', 'remove_me_also'));
和值 $改编
将是:
array('nice_item', 'another_liked_item')
希望它可以帮助写出漂亮的code。
Hope it helps write beautiful code.
这篇关于通过值删除阵列项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文