Search code examples
phparraysjsonarray-merge

merge all arrays with same title


i have this array in php json.

i have made it to sort array by first Characther. but now i'm stuck on how to merge the data under the same title.

my response now is.

[
{
    "title": "A",
    "data": {
        "id": "317",
        "name": "Aesethetica"
    }
},
{
    "title": "A",
    "data": {
        "id": "318",
        "name": "Astonos"
    }
},
{
    "title": "B",
    "data": {
        "id": "320",
        "name": "Bourjois"
    }
},
{
    "title": "B",
    "data": {
        "id": "321",
        "name": "Bioderma"
    }
}
]

i need to merge all data under each same title. something like this:

[
    {
        "title": "A",
        "data": [
            {
            "id": "317",
            "name": "Aesethetica"
            },
            {
                "id": "318",
                "name": "Astonos"
            }
        ]
    },
    {
        "title": "B",
        "data": [
            {
                "id": "320",
                "name": "Bourjois"
            },
            {
                "id": "321",
                "name": "Bioderma"
            }
        ]
    }
]

kindly help. Thanks


i got this now: i made this update.. but still not the needed result.

this is my php code...

$result = [];
                foreach ($data as $item) {
                    $firstLetter = substr($item['name'], 0, 1);
                    $result[] = [
                        'title' => $firstLetter = ctype_alnum($firstLetter) ? $firstLetter : '#',
                        'data' => $item
                    ];
                }

    foreach ($result as $key => $item) {
       $arr[$item['title']][$key] = $item;
    }

and this is the result.

{
    "A": [
        {
            "title": "A",
            "data": {
                "brand_id": "312",
                "brand_name": "Adidsa"
            }
        },
        {
            "title": "A",
            "data": {
                "id": "314",
                "name": "Adio"
            }
        },

still can't find make the needed response..


Solution

  • This is not perfomance optimized, but shows a simple solution.

    Collect all data grouped by title, then reformat the array to your expected result.

    $array = json_decode($json, true);
    $collect = [];
    foreach($array as $item) {
        $collect[$item['title']][] = $item['data'];
    }
    ksort($collect);
    
    $titles = [];
    foreach($collect as $title => $data) {
        $names = array_column($data, 'name');
        array_multisort($names, SORT_ASC, SORT_STRING, $data);
        $titles[] = ['title' => $title, 'data' => $data];
    }
    

    echo json_encode($titles, JSON_PRETTY_PRINT); results in

    [
        {
            "title": "A",
            "data": [
                {
                    "id": "317",
                    "name": "Aesethetica"
                },
                {
                    "id": "318",
                    "name": "Astonos"
                }
            ]
        },
        {
            "title": "B",
            "data": [
                {
                    "id": "321",
                    "name": "Bioderma"
                },
                {
                    "id": "320",
                    "name": "Bourjois"
                }
            ]
        }
    ]