php双重循环五角星,php 双重foreach的坑

$cardInfo 的JSON 格式如下

{

"code": 1,

"msg": "success",

"time": 1524036614,

"data": {

"list": [

{

"id": 1000,

"user_id": 1,

"live_hoster_id": 1,

"goods_id": 1,

"goods_unit": "个",

"goods_num": 1,

"goods_price_id": 2,

"create_time": 0,

"update_time": 0,

"is_delete": 0,

"delete_time": null,

"business_id": 1,

"shop": "测试商家1",

"goodsInfo": {

"id": 1,

"name": "测试商品1",

"sku": "00000000",

"category_id": 0,

"business_id": 1,

"sale_status": 1,

"sort": 0,

"price": "0.00",

"stock_num": 0,

"saled_num": 0,

"thumbnail": " ",

"create_time": 0,

"update_time": 0,

"is_delete": 0,

"delete_time": null,

"priceInfo": {

"id": 2,

"price": "80.00",

"sale_price": "60.00",

"host_per": "10.00",

"update_time": 0,

"is_delete": 0,

"delete_time": null

}

}

},

{

"id": 1001,

"user_id": 1,

"live_hoster_id": 1,

"goods_id": 2,

"goods_unit": "个",

"goods_num": 1,

"goods_price_id": 1,

"create_time": 0,

"update_time": 0,

"is_delete": 0,

"delete_time": null,

"business_id": 2,

"shop": "测试商家2",

"goodsInfo": {

"id": 2,

"name": "测试商品2",

"sku": "00000000",

"category_id": 0,

"business_id": 2,

"sale_status": 1,

"sort": 0,

"price": "0.00",

"stock_num": 0,

"saled_num": 0,

"thumbnail": " ",

"create_time": 0,

"update_time": 0,

"is_delete": 0,

"delete_time": null,

"priceInfo": {

"id": 1,

"price": "100.00",

"sale_price": "99.00",

"host_per": "20.00",

"update_time": 0,

"is_delete": 0,

"delete_time": null

}

}

},

{

"id": 1002,

"user_id": 1,

"live_hoster_id": 1,

"goods_id": 3,

"goods_unit": "个",

"goods_num": 1,

"goods_price_id": 3,

"create_time": 0,

"update_time": 0,

"is_delete": 0,

"delete_time": null,

"business_id": 2,

"shop": "测试商家2",

"goodsInfo": {

"id": 3,

"name": "测试商品3",

"sku": "00000000",

"category_id": 0,

"business_id": 2,

"sale_status": 1,

"sort": 0,

"price": "0.00",

"stock_num": 0,

"saled_num": 0,

"thumbnail": " ",

"create_time": 0,

"update_time": 0,

"is_delete": 0,

"delete_time": null,

"priceInfo": {

"id": 3,

"price": "200.00",

"sale_price": "180.00",

"host_per": "20.00",

"update_time": 0,

"is_delete": 0,

"delete_time": null

}

}

}

]

}

}

想变换数组结构为

{

"code": 1,

"msg": "success",

"time": 1524036789,

"data": {

"list": [

{

"shop_id": 1,

"shop_name": "测试商家1",

"goodsInfo":[

{goods1},{goods2}

]

},

{

"shop_id": 2,

"shop_name": "测试商家2",

"goodsInfo":[

{goods3},{goods4}

]

}

]

}

}

于是有了如下代码

$cartInfo = collection($cartItems)->toArray();

$shops =array_column($cartInfo,'shopInfo','business_id');

$shop_list = [];

$shop_lists = [];

foreach ($shops as $key => $shop){

foreach ($cartInfo as $k=>$item){

if($item['business_id'] === $key && $item['shopInfo']['id'] === $key){

$shop_list['shop_id'] = $item['business_id'];

$shop_list['shop_name'] = $item['shopInfo']['name'];

$shop_list['goodsInfo'][] = $item['goodsInfo'];

}

}

$shop_lists[] = $shop_list;

}

return $shop_lists;

但是出来的结果却是

{

"code": 1,

"msg": "success",

"time": 1524036931,

"data": {

"list": [

{

"shop_id": 1,

"shop_name": "测试商家1",

"goodsInfo": [

{

"id": 1,

"name": "测试商品1",

"sku": "00000000",

"category_id": 0,

"business_id": 1,

"sale_status": 1,

"sort": 0,

"price": "0.00",

"stock_num": 0,

"saled_num": 0,

"thumbnail": " ",

"create_time": 0,

"update_time": 0,

"is_delete": 0,

"delete_time": null,

"priceInfo": {

"id": 2,

"price": "80.00",

"sale_price": "60.00",

"host_per": "10.00",

"update_time": 0,

"is_delete": 0,

"delete_time": null

}

}

]

},

{

"shop_id": 2,

"shop_name": "测试商家2",

"goodsInfo": [

{

"id": 1,

"name": "测试商品1",

"sku": "00000000",

"category_id": 0,

"business_id": 1,

"sale_status": 1,

"sort": 0,

"price": "0.00",

"stock_num": 0,

"saled_num": 0,

"thumbnail": " ",

"create_time": 0,

"update_time": 0,

"is_delete": 0,

"delete_time": null,

"priceInfo": {

"id": 2,

"price": "80.00",

"sale_price": "60.00",

"host_per": "10.00",

"update_time": 0,

"is_delete": 0,

"delete_time": null

}

},

{

"id": 2,

"name": "测试商品2",

"sku": "00000000",

"category_id": 0,

"business_id": 2,

"sale_status": 1,

"sort": 0,

"price": "0.00",

"stock_num": 0,

"saled_num": 0,

"thumbnail": " ",

"create_time": 0,

"update_time": 0,

"is_delete": 0,

"delete_time": null,

"priceInfo": {

"id": 1,

"price": "100.00",

"sale_price": "99.00",

"host_per": "20.00",

"update_time": 0,

"is_delete": 0,

"delete_time": null

}

},

{

"id": 3,

"name": "测试商品3",

"sku": "00000000",

"category_id": 0,

"business_id": 2,

"sale_status": 1,

"sort": 0,

"price": "0.00",

"stock_num": 0,

"saled_num": 0,

"thumbnail": " ",

"create_time": 0,

"update_time": 0,

"is_delete": 0,

"delete_time": null,

"priceInfo": {

"id": 3,

"price": "200.00",

"sale_price": "180.00",

"host_per": "20.00",

"update_time": 0,

"is_delete": 0,

"delete_time": null

}

}

]

}

]

}

}

也就是 在商家2里面 所有商品都进去了。实际上商家1有一个商品 ,商家2有2个商品

求问大佬们这是个什么情况?怎么解决?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值