0

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:

enter image description here

But here's what i produced on my foreach loop:

enter image description here

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);
2
  • what is your expected output? Commented May 23, 2020 at 14:11
  • i want the format same like the source json Commented May 24, 2020 at 7:31

3 Answers 3

1

I think this is what you are looking for... This is also less complicated as your code.

<?php
$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); 

$options = $test_product->product->options;

// Check whatever you like in this for each
foreach ($options as $option) {

    // Example
    switch ($option->name) {
        case 'Color':
            echo 'this is the color array';
        break;
    }
}

$options_json = json_encode($options);
var_dump($options_json);

?>
Sign up to request clarification or add additional context in comments.

Comments

0

you're overriding the name key in your loop, you need to do something like this:

foreach ($attributes2 as $attribute) {

    $data = [
        "name" => $attribute["name"],
        "values" => []
    ];


    foreach ($attribute['values'] as $option) {
        $data['values'][] = $option;
    }         

    $options_arr[] = $data;
}

1 Comment

no luck.I get this result using your code: { "name": "Size", "values": [ "Blue", "Black", "155", "159" ] }
0

If I don't misunderstand your requirements then you just need to do this way to get what you want,

<?php
$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'];
$expected = ['options'=>$attributes2];
echo json_encode($expected);
?>

DEMO:https://3v4l.org/5r6WI

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.