Home > Backend Development > PHP Tutorial > PHP difference operation function array_diff and array traversal

PHP difference operation function array_diff and array traversal

墨辰丷
Release: 2023-03-31 08:26:02
Original
2328 people have browsed it

This article mainly introduces the PHP difference operation function array_diff and array traversal. Interested friends can refer to it. I hope it will be helpful to everyone.

The example of this article describes the method of deleting elements based on the element value in PHP associative array, as follows:

<?php 
$array1 = array("a" => "green", "red", "blue", "red"); 
$array2 = array("b" => "green"); 
$result = array_diff($array1, $array2);
//这样就相当于删除$array1里的值为"green"的元素。 
print_r($result); 
?>
Copy after login

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);
}
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

php Unordered Tree Implementation Skills

php Unordered Tree Implementation Skills

php process control and mathematical operations

The above is the detailed content of PHP difference operation function array_diff and array traversal. For more information, please follow other related articles on the PHP Chinese website!

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