这里的交易是我有一个包含 17 个元素的数组。我想获取一段时间内需要的元素并将它们从数组中永久删除。
代码如下:
$name = $post['name'];
$email = $post['email'];
$address = $post['address'];
$telephone = $post['telephone'];
$country = $post['country'];
unset($post['name']);
unset($post['email']);
unset($post['address']);
unset($post['telephone']);
unset($post['country']);
是的,代码很丑,不需要 bash。如何让这个看起来更好?
最佳答案
使用 array_diff_key
删除
$remove = ['telephone', 'country'];
$removed = array_diff_key($post, array_flip($remove));
您可以使用 array_intersect_key
如果您想提供要保留的键数组。
关于php - 取消设置多个数组元素的更好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7884991/