Home  >  Article  >  Backend Development  >  PHP two-dimensional array deduplication or statistics based on a certain field

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

不言
不言Original
2018-05-18 15:03:496193browse

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);
$result = array(
			'甘肃'=>1,
			'天津'=>2
		);

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)
);

Solution:

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

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!

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