php 两个数组是否相同,并且输出全面的数据,相同的加一个字段标示

 

方法一:

$date是数组,数组中有字段id,name;

$data1是数组,数组中有字段sort_id,name;

所以要通过$date[$i]['id']==$data1[$j]['sort_id']来确定是否相同;

相同的数据在添加一个status字段等于1;

for($i=0;$i<count($date);$i++){
for($j=0;$j<count($data1);$j++){
if($date[$i]['id']==$data1[$j]['sort_id']){
$date[$i]['status']=1;
}
}
}

所以输出的结果是:

{
"status": true,
"data": [
{
"id": "100028",
"name": "新闻资讯"
},
{
"id": "100029",
"name": "最新动态"
},
{
"id": "100033",
"name": "关于我们"
},
{
"id": "100034",
"name": "最近公告"
},
{
"id": "100036",
"name": "作坊文化"
},
{
"id": "100081",
"name": "财经",
"status": 1
},
{
"id": "100078",
"name": "滑客一句话"
},
{
"id": "100079",
"name": "军事"
},
{
"id": "100080",
"name": "政治"
},
{
"id": "100082",
"name": "新闻",
"status": 1
},
{
"id": "100083",
"name": "国际"
},
{
"id": "100084",
"name": "汽车"
},
{
"id": "100085",
"name": "体育"
},
{
"id": "100086",
"name": "娱乐"
},
{
"id": "100087",
"name": "长沙"
},
{
"id": "100088",
"name": "昨日"
},
{
"id": "100089",
"name": "科技"
},
{
"id": "100090",
"name": "健康"
},
{
"id": "100091",
"name": "数码"
},
{
"id": "100092",
"name": "自然"
},
{
"id": "100093",
"name": "房产"
},
{
"id": "100094",
"name": "教育"
},
{
"id": "100095",
"name": "历史"
}
]
}

方法二:

<?php
$arr1=array(
$arr11=array('name'=>'新闻','id'=>'1'),
$arr12=array('name'=>'新闻1','id'=>'2'),
$arr11=array('name'=>'新闻3','id'=>'3'),
);
$arr2=array(
$arr11=array('name'=>'新闻','id'=>'1'),
$arr11=array('name'=>'新闻3','id'=>'3'),
);
$rs = array();
foreach($arr1 as $value) {
if(in_array($value,$arr2)) {
$rs[1][] = $value;
} else {
$rs[2][] = $value;
}
}
var_dump($rs );
?>

输出的结果是:

array(2) { [1]=> array(2) { [0]=> array(2) { ["name"]=> string(6) "新闻" ["id"]=> string(1) "1" } [1]=> array(2) { ["name"]=> string(7) "新闻3" ["id"]=> string(1) "3" } } [2]=> array(1) { [0]=> array(2) { ["name"]=> string(7) "新闻1" ["id"]=> string(1) "2" } } }

posted @ 2015-01-22 15:54  唥凊-洫栤  阅读(419)  评论(0编辑  收藏  举报