php 自己写的一个数组分类合并
程序员文章站
2022-03-27 22:23:42
...
$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"
}
}
*/
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"
}
}
*/