Home  >  Article  >  Backend Development  >  Improvements on merging arrays with the same key value of a certain field in PHP_PHP Tutorial

Improvements on merging arrays with the same key value of a certain field in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:04:09899browse

Improvements in php regarding merging arrays with the same key value of a certain field

The following is the implementation code:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

/**

**关于参数的说明

**$key键值相同的键名

**$array代表原数组

**$start代表$array[0][$key]

**$newkey代表相同键值相同的键名

**/

 

function combine_same_val($array,$start,$key,$newkey){

static $new;

foreach($array as $k=>$v){

if($v[$key]==$start){

$new[$v[$newkey]][] = $v;

unset($array[$k]);

continue;

}

}

sort($array);

if(count($array)!==0){

combine_same_val($array,$array[0][$key],$key,$newkey);

}

return $new;

}

1 2
3
4 5

6

8 9 10 11 12
13
14
15 16 17 18 19 20 21 22 23
/** **Explanation about parameters **$key Key name with the same key value **$array represents the original array **$start represents $array[0][$key] **$newkey represents the same key name with the same key value **/ function combine_same_val($array,$start,$key,$newkey){ static $new; foreach($array as $k=>$v){ if($v[$key]==$start){ $new[$v[$newkey]][] = $v; unset($array[$k]); continue; } } sort($array); if(count($array)!==0){ combine_same_val($array,$array[0][$key],$key,$newkey); } return $new; }
http://www.bkjia.com/PHPjc/966370.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/966370.htmlTechArticle Improvements in php to merge arrays with the same key value in a certain field. The following is the implementation code: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 /** **Explanation about parameters...
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