0

What im trying to do is create a function that convert this array

{
    "data": {
        "churnStructured": [
            {
                "id_region": "4",
                "iso": "be",
                "mcc": "206",
                "country": "Belgium",
                "carriers": [
                    {
                        "carrier": "Triton PCS",
                        "id_carrier": 68,
                        "brands": []
                    },
                    {
                        "carrier": "Beeline",
                        "id_carrier": 80,
                        "brands": []
                    }
                ]
            },
            {
                "id_region": "4",
                "iso": "bg",
                "mcc": "284",
                "country": "Bulgaria",
                "carriers": [
                    {
                        "carrier": "Comium",
                        "id_carrier": 75,
                        "brands": []
                    }
                ]
            }
        ]
    }
}

into smaller arrays, like this:

{[
    "first_array" : [{
        "id_region": "4",
            "iso": "be",
            "mcc": "206",
            "country": "Belgium",
            "carriers": [
                {
                    "carrier": "Triton PCS",
                    "id_carrier": 68,
                    "brands": []
                },
                {
                    "carrier": "Beeline",
                    "id_carrier": 80,
                    "brands": []
                }
            ]
    ]},
    "second_array": [{
        "id_region": "4",
        "iso": "bg",
        "mcc": "284",
        "country": "Bulgaria",
        "carriers": [{
                "carrier": "Comium",
                "id_carrier": 75,
                "brands": []
                }]
    }],
    "third_array": [{}],
    "n_array": [{}]
]}

as you can see the new arrays has as the first branch, their country, and the structure is the same for their children.

I was thinking into do a recursive function, that get each value and with that validate with the keys, however, i don't know how to do the proper iteration and get each country as a new array with their respective children.

and also the array is dynamic, it can receive an array of n-objects

1 Answer 1

1

Use a numerical array, not first_array etc. This should do the trick:

$data = json_decode($json, true);
$data = $data['data']['churnStructured'];

$results = [];
foreach ($data as $row) {
    $results[] = $y;
}

var_dump($results);

Which you can see in action here! https://3v4l.org/V6VoO

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

2 Comments

im gonna try, your solution :)
glad i could help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.