在多维 Laravel 对象中显示键和值
Displaying keys and values in a multidimensional Laravel object
我想将我的键和值放在这样的集合中
$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"
}
]
]
最后,我想以某种方式推送该集合并拥有上面的键值
这样的事情可能吗?
你好 你可以这样试试
$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_array = json_decode($attributes);
$attributes = [];
foreach ($attributes_array->Attribute as $key=> $value){
$attributes[$key]['key']=$value->Attribute_name;
$attributes[$key]['value']=$value->Attribute_value;
}
dd($attributes);
想要结果
array:4 [▼
0 => array:2 [▼
"key" => "heigth"
"value" => "8"
]
1 => array:2 [▼
"key" => "color"
"value" => "red"
]
2 => array:2 [▼
"key" => "heigth"
"value" => "8"
]
3 => array:2 [▼
"key" => "heigth"
"value" => "2"
]
]
我想将我的键和值放在这样的集合中
$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"
}
]
]
最后,我想以某种方式推送该集合并拥有上面的键值 这样的事情可能吗?
你好 你可以这样试试
$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_array = json_decode($attributes);
$attributes = [];
foreach ($attributes_array->Attribute as $key=> $value){
$attributes[$key]['key']=$value->Attribute_name;
$attributes[$key]['value']=$value->Attribute_value;
}
dd($attributes);
想要结果
array:4 [▼
0 => array:2 [▼
"key" => "heigth"
"value" => "8"
]
1 => array:2 [▼
"key" => "color"
"value" => "red"
]
2 => array:2 [▼
"key" => "heigth"
"value" => "8"
]
3 => array:2 [▼
"key" => "heigth"
"value" => "2"
]
]