Home  >  Article  >  Backend Development  >  How to delete array elements in php

How to delete array elements in php

墨辰丷
墨辰丷Original
2018-06-05 16:34:041559browse

This article mainly introduces the method of deleting array elements in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

Several PHP methods to delete array elements. In many cases, our arrays will be duplicated. So what should we do if we delete some duplicate content in the array? These elements must remain unique, so we need to find ways to delete them. They, below use several methods of traversing queries to remove duplicate array elements.
Method 1. Completely delete duplicate array instances-----Delete one element in the array

function array_remove_value(&$arr, $var){
foreach ($arr as $key => $value) {
if (is_array($value)) {
array_remove_value($arr[$key], $var);
} else {
$value = trim($value);
if ($value == $var) {
unset($arr[$key]);
} else {
$arr[$key] = $value;
}
}
}
}

$a is an array:

count($a); //得到4
unset($a[1]); //删除第二个元素
count($a); //得到3
echo $a[2]; //数组中仅有三个元素,本想得到最后一个元素,但却得到blue,
echo $a[1]; //无值
?>

That is to say, after deleting the elements in the array, the number of elements in the array (obtained with count()) changes, but the array subscript is not reset. Arrangement, you must also use the key before deleting the array to operate the corresponding value.
Later I adopted another method. In fact, it was not called a "method" at all. I used the ready-made function array_splice() in php4.

count ($a); //得到4
array_splice($a,1,1); //删除第二个元素
count ($a); //得到3
echo $a[2]; //得到yellow
echo $a[1]; //得到blue
?>

Method 2, Function to delete duplicate elements in an array

function delmember(&$array, $id)
{
$size = count($array);
for($i = 0; $i <$size - $id - 1; $i ++)
{
$array[$id + $i] = $array[$id + $i + 1];
}
unset($array[$size - 1]);
}

Supplementary example:

Method 1, php has a built-in function array_unique that can be used to delete duplicate values ​​in the array

  • #array_unique -- Remove duplicate values ​​from the array

  • array_uniqueDescription

  • array array_unique (array array)

  • array_unique() accepts array as input and returns a new array with no duplicate values ​​

Note that the key names remain unchanged. array_unique() sorts the values ​​first as strings, then retains only the first encountered key for each value, and then ignores all subsequent keys. This does not mean that the first occurrence of the same value in an unsorted array will be preserved.

Note: Two units are considered the same if and only if (string) $elem1 === (string) $elem2. That is, when the expressions of the strings are the same.
The first unit will be retained.
Example: array_unique()

<?php
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
?>

The above example will output:

Array
(
 [a] => green
 [0] => red
 [1] => blue
)

Method 2, array_flip to achieve the duplication effect

Another method is to use PHP's array_flip function to indirectly achieve the deduplication effect.

array_flip is a function that reverses the keys and values ​​of the array. It has a characteristic that if there are two in the array The values ​​are the same, then the last key and value will be retained after reversal. Taking advantage of this feature, we use it to indirectly implement deduplication of the array.

<?php
$arr = array("a"=>"a1","b"=>&#39;b1&#39;,"c"=>"a2","d"=>"a1");
$arr1 = array_flip($arr);
print_r($arr1);//先反转一次,去掉重复值,输出Array ( [a1] => d [b1] => b [a2] => c )
$arr2 = array_flip($arr);
print_r($arr2);//再反转回来,得到去重后的数组,输出Array ( [a1] => d [b1] => b [a2] => c )
$arr3 = array_unique($arr);
print_r($arr3);//利用php的array_unique函数去重,输出Array ( [a] => a1 [b] => b1 [c] => a2 )
?>

The difference between the two methods is to use

array_flip gets the last key and value of the repeated element, and uses array_unique to get the first key and value of the two repeated elements.

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

Related recommendations:

Sharing a simple method to achieve page staticization in PHP

Data transmission in PHP CURL example analysis

php simple example sharing of adding data to xml file

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

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