Home > Software engineering >  PHP flatten a Multidimensional Array with keys
PHP flatten a Multidimensional Array with keys

Time:04-01

I wrote some PHP code to line up the children of an array. I wrote a recursive function.

Everything is listed correctly one below the other, but some arrays do not have the "Name" key.

Does anyone have any idea how I can get it to show everyone's "Name" key?

My PHP Code:

$json = '[
    {
        "name": "A",
        "children": [
            {
                "name": "B",
                "children": [
                    {
                        "name": "C"
                    },
                    {
                        "name": "D",
                        "children": [
                            {
                                "name": "E"
                            },
                            {
                                "name": "F"
                            }
                        ]
                    }
                ]
            },
            {
                "name": "G"
            }
        ]
    },
    {
        "name": "H"
    }
]';


header('Content-Type: application/json; charset=utf-8');
$json = json_decode($json, true);

function array_flatten($array, $prefix = '') {
    $return = array();
    foreach ($array as $key => $value) {
        if (is_array($value) && array_key_exists("children", $value)){
            $return = array_merge($return, array_flatten($value, $key));
        }else if (is_array($value) && count($value) > 1){
            foreach ($value as $keyA => $valueA) {
                if (is_array($valueA) && array_key_exists("children", $valueA)){
                    $return = array_merge($return, array_flatten($valueA, $keyA));
                }else{
                    $return[] = $valueA;
                }
            }
        } else {
            $return[] = $value;
        }
    }

    return $return;
}

echo json_encode(array_flatten($json));

Current Result:

 ["A","B",{"name":"C"},"D",{"name":"E"},{"name":"F"},{"name":"G"},{"name":"H"}]

What I want:

[{"name":"A"},{"name":"B"},{"name":"C"},{"name":"D"},{"name":"E"},{"name":"F"},{"name":"G"},{"name":"H"}]

CodePudding user response:

Here is a working code :

<?php
$json = '[
{
    "name": "A",
    "children": [
        {
            "name": "B",
            "children": [
                {
                    "name": "C"
                },
                {
                    "name": "D",
                    "children": [
                        {
                            "name": "E"
                        },
                        {
                            "name": "F"
                        }
                    ]
                }
            ]
        },
        {
            "name": "G"
        }
    ]
},
{
    "name": "H"
}
]';

$json = json_decode($json, true);

function getArrayValuesRecursively(array $array){
    $values = [];
    foreach ($array as $value) {
        if (is_array($value)) {
            $values = array_merge(
                $values,
                getArrayValuesRecursively($value)
            );
        } else {
            $values[] = ['name' => $value];
        }
    }
    return $values;
}

echo json_encode(getArrayValuesRecursively($json));

Demo : https://3v4l.org/01Fla

Function getArrayValuesRecursively(array $array) is from Get all values from multidimensional array

CodePudding user response:

here's my take on it:

$json = json_decode($json, true);
$names = get_names_of_people($json);
print(json_encode($names));

function get_names_of_people(array $people): array
{
    $names  = [];
    foreach ($people as $person)  {
        foreach ($person as $key => $value) {
            switch ($key) {
                case 'name':
                    $names[] = [
                        'name' => $value,
                    ];
                    break;
                case 'children':
                    $names = array_merge($names, get_names_of_people($value));
            }
        }
    }
    return $names;
}

CodePudding user response:

$array = json_decode($json, true);

$result = [];

array_walk_recursive($array, function ($value, $key) use(&$result) {
  $result[][$key] = $value;
});
  • Related