In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces php how to remove the specified value of the array, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Php array to remove the specified value of the method: 1, the use of foreach and unset () function to delete specific elements in the array; 2, the use of array_flip () function and unset () function to delete a specific value in the array; 3, the use of array_search () and unset () function to delete the array mid-value.
This article operating environment: windows7 system, PHP7.1 version, Dell G3 computer.
In some special cases, you need to delete specific values in the array, and delete them all, in fact, there are many ways, we will discuss in this article.
First, delete specific elements in the array using the foreach and unset () functions
Foreach ($array as $k = > $v) {if ($v = = 'day') {unset ($array [$k]):}}
The unset () function deletes the specified array value.
2. Delete specific values in the array by using array_flip () function and unset () function
$arr = array_flip ($arr); unset ($arr ['world']); $arr = array_flip ($arr); print_r ($arr)
Array_flip () is a reverse function that changes the original key name of the array to the key name, and the key value to the key name, so that the above operation is easy to understand.
Third, delete specific values in the array by using array_search () and unset () functions
If (($key = array_search ('day',$arr) {unset ($arr [$key]);}
The array_search () function, like in_array (), looks for a key value in the array. If the value is found, the key name of the matching element is returned. If it is not found, false is returned.
4. Array_splice () function can play the same role as unset () function.
If (($key = array_search ('day',$arr) {array_splice ($arr, $key,1);}
Array_splice () has four parameters. For usage, see here.
5. Array_unique () leaves none of this specific value in the array
Note: all the above actions only delete the first element in the array that has the same value as your given value, and the subsequent element is not deleted even if it is equal to your given value!
Before performing all the above code operations, do the array_unique ($array) operation, merge and delete the duplicate values in the array, and then delete them so that none of the duplicate values in the array are left.
6. Delete only the key values corresponding to specific values, and do not move other values
When the specific value is deleted in No. 5, other unrelated values are also merged, so what if you don't move the other values? Repeat the operations above, get the length of the array, and use a for loop. Of course, this is the stupidest way.
The function to get the length of the array is count ($arr)
For (count ($arr)) if ($key = array_search ('day',$arr)) array_splice ($arr,$key,1); Thank you for reading this article carefully. I hope the article "php how to remove the specified value from the array" shared by the editor will be helpful to you. At the same time, I also hope you will support us and follow the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.