首页 > 解决方案 > 在 php 的集合中添加参数

问题描述

嗨伙计们,我有这个问题,在 php 中返回这个集合

[{name: "DayOffice", pax: 3, id: 2, location: "Eur", type: "DAYOFFICE"},
{name: "HotDesking", pax: 3, id: 3, location: "Eur", type: "HOTDESKING"}]

我想要这样的东西:

[{name: "DayOffice", pax: 3, id: 2, location: "Eur", type: "DAYOFFICE","price":xxxx},
{name: "HotDesking", pax: 3, id: 3, location: "Eur", type: "HOTDESKING","price":xxxx}]

请。

谢谢大家

标签: phpobjectcollections

解决方案


首先,您的收藏/字符串无效/错误。要获得您想要的内容,您必须通过用引号将键包装起来使其有效"",然后将其解码为数组并将您预期的键推送给它,例如price使用 value xxxx

$obj = '[{"name":"DayOffice","pax":3,"id":2,"location":"Eur","type":"DAYOFFICE"},{"name":"HotDesking","pax":3,"id":3,"location":"Eur","type":"HOTDESKING"}]';

$array = json_decode($obj,1);
foreach($array as $key=> $row){
    $array[$key]['price'] = 'xxxx';
}

echo json_encode($array);

演示: https ://3v4l.org/XaNMf


推荐阅读