更好地取消设置多个数组元素的方法

时间:2021-11-21 01:04:53

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。如何让这个看起来更好?

4 个解决方案

#1


71  

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

#2


45  

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。

#3


34  

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.

我希望这对你的发展有所帮助。

#4


2  

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()不安全,所以完全不可能!

#1


71  

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

#2


45  

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。

#3


34  

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.

我希望这对你的发展有所帮助。

#4


2  

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()不安全,所以完全不可能!