【发布时间】:2021-08-30 20:09:52
【问题描述】:
我在向关联数组添加新项目时遇到了一些问题,
这就是我创建结构的方式:
$cpdata[$count] = array(
'value' => $value->value,
'images' => array(
'color' => $value->color,
'image' => $value->image,
),
);
这就是我输出 json 时的样子:
{
"value": "BOX",
"images": {
"color": "white",
"image": "white.png"
}
}
但我想向images 添加更多项目,如下所示:
{
"value": "BOX",
"images": [
{
"color": "white",
"image": "white.png"
},
{
"color": "black",
"image": "black.png"
},
{
"color": "gray",
"image": "gray.png"
}
]
}
我尝试过使用array_push 和array_merge,但我无法得到它
我试过array_push($cpdata['images'][$count]['images'], 'color'=>'red', image' => 'red.png')
你能帮帮我吗? 问候 马里奥
【问题讨论】:
标签: php arrays associative-array array-push