首页 > 后端开发 > php教程 > php排除有某同一特征的数据

php排除有某同一特征的数据

WBOY
发布: 2016-06-06 20:09:49
原创
860 人浏览过

想用foreach来获取gid, 得到以下的数据:gid 相同,
有什么办法排除同样的gid,一样的gid,只留一组.

<code>[
  {
    "id": "151",
    "uid": "101497",
    "gid": "399",
    "create_time": "1462445116",
    "msg": null,
    "number": "148",
    "order_id": "2016050518451705537",
    "type": "0",
    "prepay_id": null,
    "code": null,
    "recharge": "0",
    "wid": "0",
    "end_time": "1462445416",
    "status": "1"
  },
  {
    "id": "132",
    "uid": "101497",
    "gid": "399",
    "create_time": "1462420981",
    "msg": null,
    "number": "97",
    "order_id": "2016050512030120339",
    "type": "0",
    "prepay_id": null,
    "code": null,
    "recharge": "0",
    "wid": "0",
    "end_time": "1462421281",
    "status": "1"
  },
  {
    "id": "133",
    "uid": "101497",
    "gid": "399",
    "create_time": "1462426436",
    "msg": null,
    "number": "97",
    "order_id": "2016050513335645342",
    "type": "0",
    "prepay_id": null,
    "code": null,
    "recharge": "0",
    "wid": "0",
    "end_time": "1462426736",
    "status": "1"
  }
]</code>
登录后复制
登录后复制

回复内容:

想用foreach来获取gid, 得到以下的数据:gid 相同,
有什么办法排除同样的gid,一样的gid,只留一组.

<code>[
  {
    "id": "151",
    "uid": "101497",
    "gid": "399",
    "create_time": "1462445116",
    "msg": null,
    "number": "148",
    "order_id": "2016050518451705537",
    "type": "0",
    "prepay_id": null,
    "code": null,
    "recharge": "0",
    "wid": "0",
    "end_time": "1462445416",
    "status": "1"
  },
  {
    "id": "132",
    "uid": "101497",
    "gid": "399",
    "create_time": "1462420981",
    "msg": null,
    "number": "97",
    "order_id": "2016050512030120339",
    "type": "0",
    "prepay_id": null,
    "code": null,
    "recharge": "0",
    "wid": "0",
    "end_time": "1462421281",
    "status": "1"
  },
  {
    "id": "133",
    "uid": "101497",
    "gid": "399",
    "create_time": "1462426436",
    "msg": null,
    "number": "97",
    "order_id": "2016050513335645342",
    "type": "0",
    "prepay_id": null,
    "code": null,
    "recharge": "0",
    "wid": "0",
    "end_time": "1462426736",
    "status": "1"
  }
]</code>
登录后复制
登录后复制

<code class="php">$str='[
  {
    "id": "151",
    "uid": "101497",
    "gid": "399",
    "create_time": "1462445116",
    "msg": null,
    "number": "148",
    "order_id": "2016050518451705537",
    "type": "0",
    "prepay_id": null,
    "code": null,
    "recharge": "0",
    "wid": "0",
    "end_time": "1462445416",
    "status": "1"
  },
  {
    "id": "132",
    "uid": "101497",
    "gid": "399",
    "create_time": "1462420981",
    "msg": null,
    "number": "97",
    "order_id": "2016050512030120339",
    "type": "0",
    "prepay_id": null,
    "code": null,
    "recharge": "0",
    "wid": "0",
    "end_time": "1462421281",
    "status": "1"
  },
  {
    "id": "133",
    "uid": "101497",
    "gid": "398",
    "create_time": "1462426436",
    "msg": null,
    "number": "97",
    "order_id": "2016050513335645342",
    "type": "0",
    "prepay_id": null,
    "code": null,
    "recharge": "0",
    "wid": "0",
    "end_time": "1462426736",
    "status": "1"
  }
]';

$arr = json_decode($str,1);
$res = [];
foreach($arr as $v){
      if (!isset($res[$v['gid']])){
           $res[$v['gid']] = $v;
      }
}
echo '<pre class="brush:php;toolbar:false">';print_r(array_values($res));
</code>
登录后复制

自己写个函数去除重复的数据在遍历啊

相关标签:
php
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板