PHP two-dimensional array deduplication or statistics based on a certain field

不言
Release: 2023-03-25 08:42:01
Original
6170 people have browsed it

This article mainly introduces the tips on deduplication or statistics of PHP two-dimensional array according to a certain field. It has a certain reference value. Now I share it with you. Friends in need can refer to it.

Based on Deduplicate the field province (province), and count the number of occurrences of province (province) in Tianjin

Solution:

$arr = array(
			array('province'=>'甘肃','name'=>'甲'),
			array('province'=>'天津','name'=>'乙'),
			array('province'=>'天津','name'=>'丙')
		);
		$result = array();
		foreach ($arr as $key=>$value){
			$result[$value['province']] += 1;
		}
		dump($result);
Copy after login
$result = array(
			'甘肃'=>1,
			'天津'=>2
		);
Copy after login

Similarly, remove duplication according to the field province (province), and Count the province (province) as the total number of numbers in Tianjin

$arr = array(  
            array('province'=>'甘肃','number'=>100),  
            array('province'=>'天津','number'=>200),  
            array('province'=>'天津','number'=>300)  
); 
需要得到如下结果:

$arr = array(  
            array('province'=>'甘肃','number'=>100),  
            array('province'=>'天津','number'=>500)
);
Copy after login

Solution:

$result = array();
		foreach ($arr as $key=>$value){
			$result[$value['province']] += $value['number'];
		}
		dump($result);
Copy after login

Related recommendations:

PHP two-dimensional array deduplication based on a certain element

PHP two-dimensional array sorting array_multisort



#

The above is the detailed content of PHP two-dimensional array deduplication or statistics based on a certain field. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!