Hướng dẫn dùng keep keys trong PHP
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. Show Nội dung chính
Here's the code:
Yes the code is ugly, no need to bash. How do I make this look better? alex 466k197 gold badges865 silver badges975 bronze badges asked Oct 25, 2011 at 5:19 1 Use
You could use answered Nov 19, 2015 at 10:13 TimTim 4,1914 gold badges33 silver badges39 bronze badges 3 It looks like the function However, to actually answer your question, you could create an array of the keys you want to remove and loop through, explicitly unsetting them...
...or you could point the variable to a new array that has the keys removed...
...or pass all of the array members to
answered Oct 25, 2011 at 5:20 alexalex 466k197 gold badges865 silver badges975 bronze badges 3 There is another way which is better then the above examples. Source: 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:
For the entire array:
For unique keys:
For multiple keys in one array:
I hope this helps you in your development. Rob 4,88912 gold badges49 silver badges51 bronze badges answered Aug 1, 2014 at 8:25 CodingDailyCodingDaily 5914 silver badges2 bronze badges I understand this question is old, but I think an optimal way might be to do this:
Now, you can use $name, $email, ... from anywhere ;) NB: extract() is not safe, so it's completely out of question! answered Dec 2, 2013 at 11:17 2
answered Jan 12, 2021 at 20:09 ahmedkandilahmedkandil 4023 silver badges6 bronze badges 1 In php unset function can take multiple arguments
Here is this description from documentation
answered May 29 at 14:20 How to remove multiple key from array in PHP?if you see in php documentation there are not available directly remove multiple keys from php array. But we will create our own php custom function and remove all keys by given array value. In this example i created custom function as array_except(). How to unset multiple session in PHP?A PHP session can be destroyed by session_destroy() function. This function does not need any argument and a single call can destroy all the session variables. If you want to destroy a single session variable then you can use unset() function to unset a session variable. How do you unset a variable in PHP?The unset() function in PHP resets any variable. If unset() is called inside a user-defined function, it unsets the local variables. If a user wants to unset the global variable inside the function, then he/she has to use $GLOBALS array to do so. How can I find the difference between two arrays in PHP?The array_diff() function compares the values of two (or more) arrays, and returns the differences. This function compares the values of two (or more) arrays, and return an array that contains the entries from array1 that are not present in array2 or array3, etc. |