pulling json category names

Viewed 147

I'm trying to get the names of the categories that come with Json. This category breaks up with added. For this reason, I need to get category names in a loop.

JSON

[
        {
            "GUID": "d8fced8f-f751-43a8-a94f-18c613869d29",
            "Code": "100101",
            "MainUnit": "12",
            "ProductCategory": {
                "ID": 263,
                "Name": "Category 5",
                "MainCategory": false,
                "ParentCategoryID": 262,
                "ParentCategory": {
                    "ID": 262,
                    "Name": "Category 4",
                    "MainCategory": false,
                    "ParentCategoryID": 153,
                    "ParentCategory": {
                        "ID": 153,
                        "Name": "Category 3",
                        "MainCategory": false,
                        "ParentCategoryID": 152,
                        "ParentCategory": {
                            "ID": 152,
                            "Name": "Category 2",
                            "MainCategory": false,
                            "ParentCategoryID": 2,
                            "ParentCategory": {
                                "ID": 2,
                                "Name": "Category 1",
                                "MainCategory": true,
                                "ParentCategory": null,
                                "ParentCategoryID": 0
                            }
                        }
                    }
                }
            },
            "Brand": ""
        }
    ]

CURL

$curl = curl_init();
      curl_setopt_array($curl, array(
      CURLOPT_URL => 'http://example.com/json/products',
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'GET',
      CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json',
        'Accept: application/json',
        'Authorization: Bearer gd6yaw7gjiLQ8VeytrpLrl9'
      ),
    ));
    $response = curl_exec($curl);
    curl_close($curl);
    $response = json_decode($response);

    foreach($response as $return){
    
        function displayRecursiveResults($arrayObject) {
            foreach($arrayObject as $key=>$data) {
                if(is_array($data)) {
                    displayRecursiveResults($data);
                } elseif(is_object($data)) {
                    displayRecursiveResults($data);
                } else {
                    echo $data.'<br />';
                }
            }
        }
        displayRecursiveResults($return->ProductCategory);
        
    }

Inside the displayRecursiveResults function I created a loop to get the categories. In this loop, not only the category names, but also other parameters.

Output:

263
Category 5
262
262
Category 4
153
153
Category 3
152
152
Category 2
2
2
Category 1
1
0

i want to print like this: Category 5->Category 4->Category 3->Category 2->Category 1

1 Answers

What you try to do is called breadcrumbs.

I wrote a short function that does what you need. Just use it in your code and breadcrumbs should work.

<?php

$json = '{"GUID": "d8fced8f-f751-43a8-a94f-18c613869d29",
            "Code": "100101",
            "MainUnit": "12",
            "ProductCategory": {
                "ID": 263,
                "Name": "Category 5",
                "MainCategory": false,
                "ParentCategoryID": 262,
                "ParentCategory": {
                    "ID": 262,
                    "Name": "Category 4",
                    "MainCategory": false,
                    "ParentCategoryID": 153,
                    "ParentCategory": {
                        "ID": 153,
                        "Name": "Category 3",
                        "MainCategory": false,
                        "ParentCategoryID": 152,
                        "ParentCategory": {
                            "ID": 152,
                            "Name": "Category 2",
                            "MainCategory": false,
                            "ParentCategoryID": 2,
                            "ParentCategory": {
                                "ID": 2,
                                "Name": "Category 1",
                                "MainCategory": true,
                                "ParentCategory": null,
                                "ParentCategoryID": 0
                            }
                        }
                    }
                }
            },
            "Brand": ""
        }';

echo createBreadcrumbs(json_decode($json, true));

function createBreadcrumbs(array $product): string
{
    // Check if the product has a category
    $categories = isset($product['ProductCategory']) ? $product['ProductCategory'] : [];

    $breadcrumbs = empty($categories) ? '' : $categories['Name'];

    while (isset($categories['ParentCategory'])) {
        $categories = $categories['ParentCategory'];
        $breadcrumbs .= '->' . $categories['Name'];
    }

    return $breadcrumbs;
}

A modified function that returns an array of categories:

// Array with all category names of the product
$productCategories = createBreadcrumbs(json_decode($json, true));
print_r($productCategories);

function createBreadcrumbs(array $product): array
{
    // Check if the product has a category
    $categories = isset($product['ProductCategory']) ? $product['ProductCategory'] : [];

    $breadcrumbs = empty($categories) ? [] : [$categories['Name']];

    while (isset($categories['ParentCategory'])) {
        $categories = $categories['ParentCategory'];
        array_push($breadcrumbs, $categories['Name']);
    }

    return $breadcrumbs;
}
Related