【发布时间】:2021-11-10 07:52:26
【问题描述】:
我想将我的键和值放在这样的集合中
$attributes = [
['key' => 'color', 'value' => 'red'],
['key' => 'color', 'value' => 'blue'],
['key' => 'color', 'value' => 'red'],
['key' => 'height', 'value' => (int) 2],
['key' => 'height', 'value' => (int) 2],
['key' => 'height', 'value' => (int) 3],
['key' => 'height', 'value' => (int) 3],
['key' => 'height', 'value' => (int) 3],
['key' => 'height', 'value' => (int) 3],
['key' => 'height', 'value' => (int) 3],
['key' => 'color', 'value' => 'red'],
['key' => 'material', 'value' => 'iron'],
['key' => 'material', 'value' => 'iron'],
['key' => 'material', 'value' => 'gold'],
['key' => 'material', 'value' => 'gold'],
['key' => 'material', 'value' => 'gold'],
];
我也有我在数据库中收到的值,保存在一个 json 像这样
"attributes" => "{"Attribute":[{"Attribute_name":"heigth","Attribute_value":"8"},{"Attribute_name":"color","Attribute_value":"red"},{"Attribute_name":"heigth","Attribute_value":"8"},{"Attribute_name":"heigth","Attribute_value":"2"}]}"
我试着做这样的事情
$attributes = [];
foreach ($feeds as $feed) {
array_push($attributes, $feed->attributes);
}
$test = collect(json_decode($attributes[0]));
dd($test);
但我会以这种方式收到一些东西
Illuminate\Support\Collection {#696
#items: array:1 [
"Attribute" => array:4 [
0 => {#380
+"Attribute_name": "height"
+"Attribute_value": "8"
}
1 => {#373
+"Attribute_name": "color"
+"Attribute_value": "red"
}
2 => {#359
+"Attribute_name": "height"
+"Attribute_value": "8"
}
3 => {#1315
+"Attribute_name": "height"
+"Attribute_value": "2"
}
]
]
最后,我想以某种方式推送该集合并拥有上面的键值 有这样的可能吗?
【问题讨论】:
标签: php laravel collections