欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2024-01-12 09:20:58
...
想用foreach来获取gid, 得到以下的数据:gid 相同,
有什么办法排除同样的gid,一样的gid,只留一组.
[
  {
    "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"
  }
]

回复内容:

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

[
  {
    "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"
  }
]

$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 '
';print_r(array_values($res));

自己寫個函數去除重複的數據在遍歷啊

相关标签: php