php 取消设置多个数组元素的更好方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7884991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Better way to unset multiple array elements
提问by Turtel
The deal here is that I have an array with 17 elements. I want to get the elements I need for a certain time and remove them permanently from the array.
这里的交易是我有一个包含 17 个元素的数组。我想获取一段时间内需要的元素并将它们从数组中永久删除。
Here's the code:
这是代码:
$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']);
Yes the code is ugly, no need to bash. How do I make this look better?
是的,代码很丑,不需要 bash。我如何让这个看起来更好?
回答by alex
It looks like the function extract()
would be a better tool for what you're trying to do (assuming it's extract all key/values from an array and assign them to variables with the same names as the keys in the local scope). After you've extracted the contents, you could then unset the entire $post
, assuming it didn't contain anything else you wanted.
看起来该函数extract()
将是您尝试执行的操作的更好工具(假设它从数组中提取所有键/值并将它们分配给与本地范围内的键具有相同名称的变量)。提取内容后,您可以取消设置整个$post
,假设它不包含您想要的任何其他内容。
However, to actually answer your question, you could create an array of the keys you want to remove and loop through, explicitly unsetting them...
但是,要实际回答您的问题,您可以创建一个要删除和循环的键数组,明确取消设置它们...
$removeKeys = array('name', 'email');
foreach($removeKeys as $key) {
unset($arr[$key]);
}
...or you could point the variable to a new array that has the keys removed...
...或者你可以将变量指向一个删除了键的新数组......
$arr = array_diff_key($arr, array_flip($removeKeys));
...or pass all of the array members to unset()
...
...或将所有数组成员传递给unset()
...
unset($arr['name'], $arr['email']);
回答by Tim
Use array_diff_key to remove
使用 array_diff_key 删除
$remove = ['telephone', 'country'];
array_diff_key($post, array_flip($remove));
You could use array_intersect_key if you wanted to supply an array of keys to keep.
如果您想提供要保留的键数组,可以使用 array_intersect_key。
回答by CodingDaily
There is another way which is better then the above examples. Source: http://php.net/manual/en/function.unset.php
还有另一种方法比上面的例子更好。来源:http: //php.net/manual/en/function.unset.php
Instead of looping thorough the entire array and unsetting all its keys, you can just unset it once like so:
您可以像这样取消设置一次,而不是遍历整个数组并取消设置其所有键:
Example Array:
示例数组:
$array = array("key1", "key2", "key3");
For the entire array:
对于整个数组:
unset($array);
For unique keys:
对于唯一键:
unset($array["key1"]);
For multiple keys in one array:
对于一个数组中的多个键:
unset($array["key1"], $array["key2"], $array["key3"] ....) and so on.
I hope this helps you in your development.
我希望这对你的发展有所帮助。
回答by Felix Imafidon
I understand this question is old, but I think an optimal way might be to do this:
我知道这个问题很老,但我认为最好的方法可能是这样做:
$vars = array('name', 'email', 'address', 'phone'); /* needed variables */
foreach ($vars as $var) {
${$var} = $_POST[$var]; /* create variable on-the-fly */
unset($_POST[$var]); /* unset variable after use */
}
Now, you can use $name, $email, ... from anywhere ;)
现在,您可以从任何地方使用 $name、$email、... ;)
NB: extract() is not safe, so it's completely out of question!
注意:extract() 不安全,所以完全没有问题!