Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use array in php to delete by value

Shulou Source: shulou.com Published: 2022-06-03 16:13:29 09月18日 Update

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!

Tags: Methods tests arrays results runs searches middle fingers elements content drawbacks more problems loops feasible appropriate helpless for this two reasons multiple Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei OPPO Reno Apple NVidia vpn