In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
How to use array in php to delete according to the value, I believe many inexperienced people are at a loss about this. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem. The method of deleting php array according to the value: 1, delete through foreach traversal with unset; 2, delete with unset after passing array_flip; 3, delete through array_search; 4, use array_keys to search for the specified value and then cycle to delete.
This article operating environment: windows7 system, PHP7.1 version, DELL G3 computer
How does php array delete based on value?
Common methods for PHP to delete elements with specified values in an array
This article example shows that PHP deletes the elements of the specified value in the array. Share with you for your reference, the details are as follows:
The test array is as follows:
/ / this is a test array $testArr = array ('t'= > 'qq',' q' = > 'qq',' b' = > 'baidu', 'a' = > 'ali', 'm' = >' xiaomi')
Method 1:
/ / unset is deleted after foreach traversal. This method is also the easiest way to think of function delByValue ($arr, $value) {if (! is_array ($arr)) {return $arr;} foreach ($arr as $k = > $v) {if ($v = $value) {unset ($arr [$k]);} return $arr;}
Test:
$testArr = delByValue ($testArr,'baidu'); print_r ($testArr)
Running result:
Array
(
[t] = > qq
[Q] = > qq
[a] = > ali
[M] = > xiaomi
)
Method 2:
Unset after / / array_flip. One drawback of this method is that after inversion, since both keys are qq, one data will be lost, so please be careful when using) function delByValue ($arr, $value) {$tempArr = array_flip ($arr); unset ($tempArr [$value]); return array_flip ($tempArr);}
Test:
$testArr = delByValue ($testArr,'baidu'); print_r ($testArr)
Running result:
Array
(
[Q] = > qq
[a] = > ali
[M] = > xiaomi
)
Method 3:
/ / array_search, this method also has its drawbacks. Array_search returns when it finds an appropriate value, so this method is not applicable when there are multiple related values in the array) function delByValue ($arr, $value) {$key = array_search ($value,$arr); if (isset ($key)) {unset ($arr [$key]);} return $arr;}
Test:
$testArr = delByValue ($testArr,'baidu'); print_r ($testArr)
Running result:
Array
(
[t] = > qq
[Q] = > qq
[a] = > ali
[M] = > xiaomi
)
Method 4:
/ use array_keys search to recycle unset) function delByValue ($arr, $value) {$keys = array_keys ($arr, $value); var_dump ($keys); if (! empty ($keys)) {foreach ($keys as $key) {unset ($arr [$key]);} return $arr;}
Test:
$testArr = delByValue ($testArr,'baidu'); print_r ($testArr)
Running result:
Array (1) {
[0] = >
String (1) "b"
}
Array
(
[t] = > qq
[Q] = > qq
[a] = > ali
[M] = > xiaomi
)
Summed up by the four methods, the really feasible ones are method 1 and method 4. As for which of these two methods is better, it should be a specific analysis of the specific situation.
After reading the above, do you know how to delete by value using array in php? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.