foreach循环操作数组

需求:操作数组中coupon_log单元,如果coupon_log里数组user值等于1时,向该数组新增一个单元并赋值出现的次数

"data": [
    {
        "id": 11,
        "merchant": 2,
        "title": "100元优惠券",
        "number": 10,
        "limit_time_type": 1,
        "day": 1,
        "start_time": "",
        "end_time": "",
        "type": 1,
        "integral": "0.00",
        "balance": "0.00",
        "limit_number": 1,
        "use_condition": "0.00",
        "face_value": "100.00",
        "coupon_log": [
            {
                "user": 1,
                "coupon_id": 11
            },
            {
                "user": 3,
                "coupon_id": 11
            },
            {
                "user": 4,
                "coupon_id": 11
            },
            {
                "user": 7,
                "coupon_id": 11
            }
        ]
    },
    {
        "id": 9,
        "merchant": 1,
        "title": "限制指定用户领取10元优惠券",
        "number": 60,
        "limit_time_type": 2,
        "day": 0,
        "start_time": "2019-05-18",
        "end_time": "2019-06-21",
        "type": 1,
        "integral": "0.00",
        "balance": "0.00",
        "limit_number": 1,
        "use_condition": "0.00",
        "face_value": "6.00",
        "coupon_log": [
            {
                "user": 3,
                "coupon_id": 9
            },
            {
                "user": 1,
                "coupon_id": 9
            }
        ]
    },
    {
        "id": 8,
        "merchant": 1,
        "title": "优惠券2",
        "number": 10,
        "limit_time_type": 2,
        "day": 0,
        "start_time": "2019-05-15",
        "end_time": "2019-06-07",
        "type": 1,
        "integral": "0.00",
        "balance": "0.00",
        "limit_number": 0,
        "use_condition": "50.00",
        "face_value": "5.00",
        "coupon_log": [
            {
                "user": 3,
                "coupon_id": 8
            },
            {
                "user": 3,
                "coupon_id": 8
            },
            {
                "user": 3,
                "coupon_id": 8
            },
            {
                "user": 3,
                "coupon_id": 8
            },
            {
                "user": 3,
                "coupon_id": 8
            },
            {
                "user": 3,
                "coupon_id": 8
            },
            {
                "user": 3,
                "coupon_id": 8
            },
            {
                "user": 3,
                "coupon_id": 8
            },
            {
                "user": 4,
                "coupon_id": 8
            },
            {
                "user": 3,
                "coupon_id": 8
            }
        ]
    },
    {
        "id": 7,
        "merchant": 1,
        "title": "测试优惠券",
        "number": 100,
        "limit_time_type": 1,
        "day": 30,
        "start_time": "",
        "end_time": "",
        "type": 1,
        "integral": "0.00",
        "balance": "0.00",
        "limit_number": 0,
        "use_condition": "0.00",
        "face_value": "50.00",
        "coupon_log": [
            {
                "user": 3,
                "coupon_id": 7
            },
            {
                "user": 3,
                "coupon_id": 7
            },
            {
                "user": 3,
                "coupon_id": 7
            },
            {
                "user": 3,
                "coupon_id": 7
            },
            {
                "user": 3,
                "coupon_id": 7
            },
            {
                "user": 3,
                "coupon_id": 7
            },
            {
                "user": 3,
                "coupon_id": 7
            },
            {
                "user": 4,
                "coupon_id": 7
            },
            {
                "user": 4,
                "coupon_id": 7
            },
            {
                "user": 3,
                "coupon_id": 7
            },
            {
                "user": 1,
                "coupon_id": 7
            },
            {
                "user": 1,
                "coupon_id": 7
            },
            {
                "user": 3,
                "coupon_id": 7
            },
            {
                "user": 7,
                "coupon_id": 7
            }
        ]
    },
    {
        "id": 5,
        "merchant": 2,
        "title": "5元优惠券",
        "number": 50,
        "limit_time_type": 1,
        "day": 20,
        "start_time": "",
        "end_time": "",
        "type": 1,
        "integral": "0.00",
        "balance": "0.00",
        "limit_number": 1,
        "use_condition": "0.00",
        "face_value": "5.00",
        "coupon_log": [
            {
                "user": 3,
                "coupon_id": 5
            },
            {
                "user": 4,
                "coupon_id": 5
            },
            {
                "user": 1,
                "coupon_id": 5
            },
            {
                "user": 7,
                "coupon_id": 5
            }
        ]
    },
    {
        "id": 1,
        "merchant": 1,
        "title": "5元优惠券",
        "number": 50,
        "limit_time_type": 1,
        "day": 20,
        "start_time": "",
        "end_time": "",
        "type": 1,
        "integral": "0.00",
        "balance": "0.00",
        "limit_number": 1,
        "use_condition": "0.00",
        "face_value": "5.00",
        "coupon_log": [
            {
                "user": 3,
                "coupon_id": 1
            },
            {
                "user": 4,
                "coupon_id": 1
            },
            {
                "user": 1,
                "coupon_id": 1
            },
            {
                "user": 7,
                "coupon_id": 1
            }
        ]
    }
],

答:

private static function handleData($result,$user_id){
        foreach ($result as $k1 => $v1) {
            if (!empty($v1['coupon_log'])) {
                $num = 0;
                foreach ($v1['coupon_log'] as $k2 => $v2) {
                    if ($v2['user'] == $user_id) {
                        $num = $num + 1;
                    }
                    $result[$k1]['receive_number'] = $num; // 新增单元并赋值
                }
            } else {// 如果数组为空
                $result[$k1]['receive_number'] = 0;
            }
            // 处理其他业务逻辑...
            if($v1['limit_number']!==0){
                if($v1['receive_number'] >= $v1['limit_number']){
                    unset($result[$k1]);
                }
            }
            unset($result[$k1]['coupon_log']);// 移除
        }
        return array_values($result);// array_values() 原始数据是数组,经过unset后,多出了下标,变成了对象,让数组只返回值,不返回键名
    }
Last modification:June 6th, 2019 at 05:51 pm
如果觉得我的文章对你有用,请随意赞赏

Leave a Comment