3 回答
TA贡献1796条经验 获得超4个赞
您可以像这样组成一个新产品数组:
$products = [];
foreach($json_data as $value) {
$products[]['products'] = $value['products'];
}
file_put_contents('cleaned.json', json_encode($products));
这将产生像这样的 json 对象:
[
{
"products": ["Big-01"]
},
{
"products": ["Big-02"]
},
{
"products": ["Big-03"]
},
{
"products": ["Big-04"]
},
{
"products": ["Big-05"]
}
]
TA贡献1772条经验 获得超5个赞
从你的片段。尝试这个。
<?php
$str = '[{"username":["1"],"avatar":["yellow"],"rep":["etc"],"products":["Big"],"groups":["small"],"feedbacks":["small"],"online":["small"],"staff":["small"]}]';
$json_decoded = json_decode($str,true);
foreach($json_decoded as $value) {
$products = $value['products'];
}
print_r( $products);
?>
输出
Array
(
[0] => Big
)
TA贡献1804条经验 获得超8个赞
创建一个新数组 ( $products),遍历旧数组,并将其设置products为一个属性,而不是设置unset每个数组。
$products = [];
foreach ($json_decoded as $index) {
$products[$index] = [
'products' => $json_decoded[$index]['products']
];
}
file_put_contents('cleaned.json', json_encode($products));
- 3 回答
- 0 关注
- 223 浏览
添加回答
举报
