Better way to unset multiple array elements

后端 未结 4 2102
说谎
说谎 2020-12-07 13:29

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.

Here\'s the

4条回答
  •  醉话见心
    2020-12-07 13:37

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

    NB: extract() is not safe, so it's completely out of question!

提交回复
热议问题