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

PHP, associative array, delete elements based on element value

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-25 09:10:52
Original
874 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:
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template