i have a json that i need to get specific values and insert to array using a foreach loop. Then i convert it back to json to check if i get the same array/json format or output. But i cant make it work. Can someone help me please. Thanks!
Here's the source format:
But here's what i produced on my foreach loop:
And here is my code.
$test_json= '{ "product": { "title": "Burton Custom Freestyle 151", "body_html": "<strong>Good snowboard!</strong>", "vendor": "Burton", "product_type": "Snowboard", "variants": [ { "option1": "Blue", "option2": "155" }, { "option1": "Black", "option2": "159" } ], "options": [ { "name": "Color", "values": [ "Blue", "Black" ] }, { "name": "Size", "values": [ "155", "159" ] } ] } }';
$test_product = json_decode($test_json, true);
$attributes2 = $test_product['product']['options'];
$options_arr = array();
foreach ($attributes2 as $attribute) {
$options_arr['name'] = $attribute['name'];
foreach ($attribute['values'] as $option) {
$options_arr['values'][] = $option;
}
}
$options_json = json_encode($options_arr);
var_dump($options_json);

