php - 在 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}]
请。
谢谢大家
解决方案
首先,您的收藏/字符串无效/错误。要获得您想要的内容,您必须通过用引号将键包装起来使其有效""
,然后将其解码为数组并将您预期的键推送给它,例如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);
推荐阅读
- r - 为什么 R 的方差不同?
- c# - “获取此版本”在项目的查看历史记录中显示为灰色
- java - Java中的接口继承
- maven - liquibase:changelogSync 给出 NullPointerException
- scala - 如何将结果保存到 spark scala 中的文本文件
- suitescript2.0 - SuiteScript 2.0 估计未更新
- c# - 除了外键整数属性之外添加导航属性时,Entity Framework 正在我的表中创建两个 Id 字段
- ios - 如何让移动应用程序在中国安装和运行?应用程序可以在不使用 VPN 的情况下运行吗?
- dbus - 通过 Bluez DBus API 的设备连接/断开通知
- webrtc - 需要帮助我的公共 IP 地址的 Coturn 配置