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

PHP, associative array, delete elements based on element value

WBOY
Release: 2016-07-25 09:10:52
Original
856 people have browsed it
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


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template