访问 'http://slave.bfgd.com.cn/media/event/get_list?chnlid=4200000064' 得到数据:

{
	"total": 25,
	"event_list": [{
		"event_id": 215021138,
		"str_event_id": "000101030010040215021138"
	}, {
		"event_id": 215021139,
		"str_event_id": "000101030010040215021139"
	}],
	"pf_prev_list": [],
	"chnl_modify_time": 1695089290,
	"fcc_switch": 1,
	"ret": 0,
	"request_id": "650a02adac124b1500007b80699654b4",
	"ret_msg": "success"
}

可以使用以下 PHP 代码将数据内容的 event_list 内添加一个数组 chnlid,值为请求接口的 chnlid:

$data = '{
    "total": 25,
    "event_list": [{
        "event_id": 215021138,
        "str_event_id": "000101030010040215021138"
    }, {
        "event_id": 215021139,
        "str_event_id": "000101030010040215021139"
    }],
    "pf_prev_list": [],
    "chnl_modify_time": 1695089290,
    "fcc_switch": 1,
    "ret": 0,
    "request_id": "650a02adac124b1500007b80699654b4",
    "ret_msg": "success"
}';

$chnlid = "4200000064";

$data = json_decode($data, true);
foreach ($data["event_list"] as &$event) {
    $event["chnlid"] = $chnlid;
}

$result = json_encode($data);
echo $result;

输出结果为:

{
    "total": 25,
    "event_list": [
        {
            "event_id": 215021138,
            "str_event_id": "000101030010040215021138",
            "chnlid": "4200000064"
        },
        {
            "event_id": 215021139,
            "str_event_id": "000101030010040215021139",
            "chnlid": "4200000064"
        }
    ],
    "pf_prev_list": [],
    "chnl_modify_time": 1695089290,
    "fcc_switch": 1,
    "ret": 0,
    "request_id": "650a02adac124b1500007b80699654b4",
    "ret_msg": "success"
}

以上代码将原始数据解析为数组,然后使用 foreach 循环遍历 event_list 数组,并在每个 event 中添加一个键值对 "chnlid",值为请求接口的 chnlid。最后将修改后的数组转换为 JSON 格式并输出。

PHP 添加 chnlid 到 event_list 数组

原文地址: https://www.cveoy.top/t/topic/fkGg 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录