This article describes the example of PHP associative array implementing the method of deleting elements based on the element value. Share it with everyone for your reference. The details are as follows:
<?php $array1 = array("a" => "green", "red", "blue", "red"); $array2 = array("b" => "green"); $result = array_diff($array1, $array2); //这样就相当于删除$array1里的值为"green"的元素。 print_r($result); ?>
There is another method, which is more complicated than the above, but the effect is the same:
function removeArrayElement(&$ar,$val) { $tmp = array(); foreach($ar as $k => $arc) { if($arc!=$val) { $tmp[$k]=$arc; } } $ar = $tmp; unset($tmp); }
I hope this article will be helpful to everyone’s PHP programming design.