Home > Backend Development > PHP Problem > How to remove multiple arrays and keep one in php

How to remove multiple arrays and keep one in php

藏色散人
Release: 2023-03-17 07:52:01
Original
1568 people have browsed it

How to remove multiple PHP arrays and keep one: 1. Create a PHP sample file; 2. Create the "function formatArray($array) {...}" method; 3. Pass "for($ i=0;$i

How to remove multiple arrays and keep one in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

php Remove multiple arrays and keep one?

php deletes the same elements in the array and keeps only one same element

We have introduced N methods for deleting duplicate arrays. Today’s example is a little different, which is to delete the array. Among the same elements, only one same element is retained. Let’s take an example below. The code is as follows:

<?php 
// 删除数组中相同元素,只保留一个相同元素 
function formatArray($array) 
{ 
sort($array); 
$tem = ”; 
$temarray = array(); 
$j = 0; 
for($i=0;$i<count($array);$i++) 
{ 
if($array[$i]!=$tem) 
{ 
$temarray[$j] = $array[$i]; 
$j++;//开源代码phpfensi.com 
} 
$tem = $array[$i]; 
} 
return $temarray; 
} 
//测试 调用函数 
$array = array(‘aa’,&#39;bb’,&#39;aa’,3,4,5,5,5,5,’bc’); 
$arr = formatArray($array); 
print_r($arr); 
?>
Copy after login

Related introduction:

sort() function sorts the numerical array in ascending order.

Tip: Please use the rsort() function to sort the numerical array in descending order.

Grammar

sort(array,sortingtype);
Copy after login

Recommended study: "PHP Video Tutorial"

The above is the detailed content of How to remove multiple arrays and keep one in php. 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