Home  >  Article  >  Backend Development  >  php 自己写的一个数组归类合并

php 自己写的一个数组归类合并

WBOY
WBOYOriginal
2016-06-13 11:55:471003browse

php 自己写的一个数组分类合并
$arr=array(
   array(
     'id'=>1,
'sex'=>'yafei'
   ),array(
     'id'=>2,
'sex'=>'jiyu'
   ),array(
     'id'=>3,
'sex'=>'yun'
   ),


);
$arr2=array(
   array(
     'id'=>1,
'name'=>'qwe'
   ),array(
     'id'=>2,
'name'=>'ewq'
   ),array(
     'id'=>3,
'name'=>'qwew'
   ),
);
 /*
  *对两个数组进行同类合并
  [email protected] yalong sun
  *@
  */
function array_Category_merge($arr,$arr2){
foreach($arr as $k=>$v){
  foreach($arr2 as $q=>$s){
 if($v['id']==$s['id']) 
 $a[]=array_merge($arr[$k],array("content"=>$s['name']));
  }
}
return $a;
}
var_dump(array_Category_merge($arr,$arr2));


 /*结果为:
 array(3) {
  [0]=>
  array(3) {
    ["id"]=>
    int(1)
    ["sex"]=>
    string(5) "yafei"
    ["content"]=>
    string(3) "qwe"
  }
  [1]=>
  array(3) {
    ["id"]=>
    int(2)
    ["sex"]=>
    string(4) "jiyu"
    ["content"]=>
    string(3) "ewq"
  }
  [2]=>
  array(3) {
    ["id"]=>
    int(3)
    ["sex"]=>
    string(3) "yun"
    ["content"]=>
    string(4) "qwew"
  }
}
 
 */

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