您现在的位置是:网站首页 > 心得笔记

把一个大的数组对象或者二维数组,按照某字段分类并重组

盛悦2019-10-2557人围观
简介这个问题在开发过程中经常遇到,比如我现在查询数据库,查询出来商品下五大分类下的课程,但是直接查询结果一定是一个大的数组对象,里面分别展示着每一个课程信息,现在我想汇总下,把所有课程按照他的分类分别归属到对应的数组下

口述的场景也许不是很清楚,这里直接展示前后的数据

{
  "goods": [
    {
      "id": 6,
      "admin_id": 2,
      "cat_id": 5,
      "goods_name": "栏目商品",
      "thumbnail": "http://oms-oss.oss-cn-beijing.aliyuncs.com/marketing/20190923/yF8iKGVeWXRI9qu83vbUYe7IYQWLjjesoTH9ckGB.jpeg",
      "brief": "<p>223232</p>",
      "content": "<p>223232</p>",
      "is_copy": 0,
      "sell_type": 0,
      "sell_status": 0,
      "price": "0.00",
      "line_price": "0.00",
      "group_ids": "2,10",
      "correlation_sell": null,
      "is_alone_sale": 0,
      "is_correlation_sale": 0,
      "correlation_ids": "3,4",
      "column_ids": "",
      "status": 0,
      "is_putaway": 0,
      "timing_date": null,
      "is_show": 1,
      "subscribe_number": 0,
      "is_recommended": 0,
      "created_at": null,
      "updated_at": null,
      "deleted_at": null
    },
    {
      "id": 5,
      "admin_id": 2,
      "cat_id": 1,
      "goods_name": "商品3",
      "thumbnail": "http://oms-oss.oss-cn-beijing.aliyuncs.com/marketing/20190923/yF8iKGVeWXRI9qu83vbUYe7IYQWLjjesoTH9ckGB.jpeg",
      "brief": "<p>2222222222</p>",
      "content": "<p>123123123</p>",
      "is_copy": 1,
      "sell_type": 0,
      "sell_status": 1,
      "price": "120.00",
      "line_price": "4.00",
      "group_ids": "1,3",
      "correlation_sell": null,
      "is_alone_sale": 1,
      "is_correlation_sale": 1,
      "correlation_ids": "2,4",
      "column_ids": "",
      "status": 0,
      "is_putaway": 1,
      "timing_date": null,
      "is_show": 1,
      "subscribe_number": 0,
      "is_recommended": 0,
      "created_at": "2019-09-23 19:23:25",
      "updated_at": "2019-09-23 19:23:25",
      "deleted_at": null
    },
    {
      "id": 3,
      "admin_id": 2,
      "cat_id": 2,
      "goods_name": "商品1",
      "thumbnail": "http://oms-oss.oss-cn-beijing.aliyuncs.com/marketing/20190919/gSUKATsCIyLTgLz5mitnJCoJXoajXRCDqUTDzt4c.jpeg",
      "brief": "<p>23213213213</p>",
      "content": "<p>123123</p>",
      "is_copy": 1,
      "sell_type": 0,
      "sell_status": 1,
      "price": "12.00",
      "line_price": "1.00",
      "group_ids": "2",
      "correlation_sell": null,
      "is_alone_sale": 1,
      "is_correlation_sale": 1,
      "correlation_ids": "3,4",
      "column_ids": "",
      "status": 0,
      "is_putaway": 1,
      "timing_date": "2019-09-19 00:00:00",
      "is_show": 1,
      "subscribe_number": 0,
      "is_recommended": 0,
      "created_at": "2019-09-19 10:07:28",
      "updated_at": "2019-09-19 10:07:28",
      "deleted_at": null
    }
  ]
}



根据cat_id(分类id)汇总

{
  "1": [
    {
      "id": 5,
      "admin_id": 2,
      "cat_id": 1,
      "goods_name": "商品3",
      "thumbnail": "http://oms-oss.oss-cn-beijing.aliyuncs.com/marketing/20190923/yF8iKGVeWXRI9qu83vbUYe7IYQWLjjesoTH9ckGB.jpeg",
      "brief": "<p>2222222222</p>",
      "content": "<p>123123123</p>",
      "is_copy": 1,
      "sell_type": 0,
      "sell_status": 1,
      "price": "120.00",
      "line_price": "4.00",
      "group_ids": "1,3",
      "correlation_sell": null,
      "is_alone_sale": 1,
      "is_correlation_sale": 1,
      "correlation_ids": "2,4",
      "column_ids": "",
      "status": 0,
      "is_putaway": 1,
      "timing_date": null,
      "is_show": 1,
      "subscribe_number": 0,
      "is_recommended": 0,
      "created_at": "2019-09-23 19:23:25",
      "updated_at": "2019-09-23 19:23:25",
      "deleted_at": null
    },
  ],
  "2": [
    {
      "id": 3,
      "admin_id": 2,
      "cat_id": 2,
      "goods_name": "商品1",
      "thumbnail": "http://oms-oss.oss-cn-beijing.aliyuncs.com/marketing/20190919/gSUKATsCIyLTgLz5mitnJCoJXoajXRCDqUTDzt4c.jpeg",
      "brief": "<p>23213213213</p>",
      "content": "<p>123123</p>",
      "is_copy": 1,
      "sell_type": 0,
      "sell_status": 1,
      "price": "12.00",
      "line_price": "1.00",
      "group_ids": "2",
      "correlation_sell": null,
      "is_alone_sale": 1,
      "is_correlation_sale": 1,
      "correlation_ids": "3,4",
      "column_ids": "",
      "status": 0,
      "is_putaway": 1,
      "timing_date": "2019-09-19 00:00:00",
      "is_show": 1,
      "subscribe_number": 0,
      "is_recommended": 0,
      "created_at": "2019-09-19 10:07:28",
      "updated_at": "2019-09-19 10:07:28",
      "deleted_at": null
    }
  ],
  "5": [
    {
      "id": 6,
      "admin_id": 2,
      "cat_id": 5,
      "goods_name": "栏目商品",
      "thumbnail": "http://oms-oss.oss-cn-beijing.aliyuncs.com/marketing/20190923/yF8iKGVeWXRI9qu83vbUYe7IYQWLjjesoTH9ckGB.jpeg",
      "brief": "<p>223232</p>",
      "content": "<p>223232</p>",
      "is_copy": 0,
      "sell_type": 0,
      "sell_status": 0,
      "price": "0.00",
      "line_price": "0.00",
      "group_ids": "2,10",
      "correlation_sell": null,
      "is_alone_sale": 0,
      "is_correlation_sale": 0,
      "correlation_ids": "3,4",
      "column_ids": "",
      "status": 0,
      "is_putaway": 0,
      "timing_date": null,
      "is_show": 1,
      "subscribe_number": 0,
      "is_recommended": 0,
      "created_at": null,
      "updated_at": null,
      "deleted_at": null
    }
  ]
}