Home > Backend Development > PHP Tutorial > php 自己写的一个数组分类合并

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

WBOY
Release: 2016-06-23 13:58:16
Original
1026 people have browsed it

$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'
   ),
);
 /*
  *对两个数组进行同类合并
  *@auther 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"
  }
}
 
 */

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