Help with Recursive array loop

Posted 2 months ago by TuffRivers

I need to get product options out of each product in a product array. Some products might have no product options, some products might have N product options.

How can i recursively loop the product array to get the display_value and display_name for each product_options array from the product array?

I tried this solution to build a tree and return the "branch" but i get a blank array when i execute.

Any ideas?

//convert json to array
//take the product (elements) array, set the parentId to the "id" field for each product
//then use the product options [id] field as the child 
// print out the tree

    $elements = json_decode($product, true);
    
    function buildTree(array $elements, $parentId = 0) {
    $branch = array();

    foreach ($elements as $element) {
        if ($element['id'] == $parentId) {
            $children = buildTree($elements, $element['id']);
            if ($children) {
                $element['product_options']["id"] = $children;
            }
            $branch[] = $element;
        }
    }

    return $branch;
}

$tree = buildTree($elements);

print_r($tree);


//data

$product = '[
    {
        "id": 72,
        "order_id": 64,
        "product_id": 55,
        "order_address_id": 75,
        "name": "Product XXXX",
        "sku": "XXX123",
        "type": "physical",
        "product_options": [],
        "configurable_fields": []
    },
    {
        "id": 74,
        "order_id": 64,
        "product_id": 112,
        "order_address_id": 75,
        "name": "Iron-On Letters",
        "sku": "",
        "product_options": [
            {
                "id": 26,
                "option_id": 34,
                "order_product_id": 74,
                "product_option_id": 15,
                "display_name": "Color",
                "display_value": "Pink",
                "value": "99",
                "type": "Swatch",
                "name": "Color",
                "display_style": ""
            },
            {
                "id": 27,
                "option_id": 38,
                "order_product_id": 74,
                "product_option_id": 22,
                "display_name": "Initials",
                "display_value": "TT",
                "value": "TT",
                "type": "Text field",
                "name": "Initials",
                "display_style": ""
            },
            {
                "id": 28,
                "option_id": 40,
                "order_product_id": 74,
                "product_option_id": 26,
                "display_name": "Style",
                "display_value": "Golf",
                "value": "113",
                "type": "Multiple choice",
                "name": "Style",
                "display_style": "Dropdown"
            }
        ],
        "configurable_fields": []
    },
    {
        "id": 75,
        "order_id": 64,
        "product_id": 12,
        "order_address_id": 75,
        "name": "Iron-On Letters",
        "sku": "",
        "product_options": [
            {
                "id": 29,
                "option_id": 34,
                "order_product_id": 75,
                "product_option_id": 15,
                "display_name": "Color",
                "display_value": "Pink",
                "value": "99",
                "type": "Swatch",
                "name": "Color",
                "display_style": ""
            },
            {
                "id": 30,
                "option_id": 38,
                "order_product_id": 75,
                "product_option_id": 22,
                "display_name": "Initials",
                "display_value": "RR",
                "value": "RR",
                "type": "Text field",
                "name": "Initials",
                "display_style": ""
            },
            {
                "id": 31,
                "option_id": 40,
                "order_product_id": 75,
                "product_option_id": 26,
                "display_name": "Style",
                "display_value": "Golf",
                "value": "113",
                "type": "Multiple choice",
                "name": "Style",
                "display_style": "Dropdown"
            }
        ],
        "configurable_fields": []
    }
]';

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.