Home > Backend Development > PHP Problem > How to delete associative array elements in php

How to delete associative array elements in php

coldplay.xixi
Release: 2023-03-06 15:48:01
Original
2751 people have browsed it

php method to delete associative array elements: first create a PHP sample file; then define a "removeArrayElement" method; then delete elements in the associative array through loop traversal.

How to delete associative array elements in php

php method to delete associative array elements:

$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

If you want to know more about programming learning, please pay attention to the php training column!

The above is the detailed content of How to delete associative array elements in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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