Home  >  Article  >  Backend Development  >  PHP, associative array, delete elements based on element value

PHP, associative array, delete elements based on element value

WBOY
WBOYOriginal
2016-07-25 09:10:52832browse
After some modifications and improvements, the effect
  1. $array1 = array("a" => "green", "red", "blue", "red");
  2. $array2 = array("b" => " green");
  3. $result = array_diff($array1, $array2);//This is equivalent to deleting the element with the value "green" in $array1.
  4. print_r($result);
  5. ?>
  6. /* There is another method, which is more complicated than the above, but the effect is the same. */
  7. removefunctionArrayElement(&$ar,$val)
  8. {
  9. $tmp = array();
  10. foreach($ar as $k => $arc)
  11. {
  12. if($arc!=$val)
  13. {
  14. $tmp[$k]=$arc;
  15. }
  16. }
  17. $ar = $tmp;
  18. unset($tmp);
  19. }
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn