Search code examples
phparraysmultidimensional-arrayarray-merge

Dynamic merge on multi-dimensional array in php


I have some multi dimensional array that needs to be merged while preserving some of it's values.

Array
(
    [diagnosa_id] => 6
    [jeniskelamin] => LAKI-LAKI
    [diagnosa] => Array
    (
        [0] => Array
            (
                [diagnosa_kode] => A01.0
                [jeniskelamin] => LAKI-LAKI
                [pasienhidupmati] => HIDUP
                [diagnosa_nama] => Demam tifoid
                [jmlpasien] => 1
            )

        [4] => Array
            (
                [diagnosa_kode] => A01.0
                [jeniskelamin] => LAKI-LAKI
                [pasienhidupmati] => HIDUP
                [diagnosa_nama] => Demam tifoid
                [jmlpasien] => 1
            )

    )

)

Array
(
    [diagnosa_id] => 1
    [jeniskelamin] => LAKI-LAKI
    [diagnosa] => Array
    (
        [1] => Array
            (
                [diagnosa_kode] => A00
                [jeniskelamin] => LAKI-LAKI
                [pasienhidupmati] => HIDUP
                [diagnosa_nama] => Kolera
                [jmlpasien] => 1
            )

        [2] => Array
            (
                [diagnosa_kode] => A00
                [jeniskelamin] => LAKI-LAKI
                [pasienhidupmati] => HIDUP
                [diagnosa_nama] => Kolera
                [jmlpasien] => 1
            )

    )

)

Array
(
    [diagnosa_id] => 7
    [jeniskelamin] => LAKI-LAKI
    [diagnosa] => Array
    (
        [3] => Array
            (
                [diagnosa_kode] => A01.1
                [jeniskelamin] => LAKI-LAKI
                [pasienhidupmati] => HIDUP
                [diagnosa_nama] => Demam paratifoid A
                [jmlpasien] => 1
            )

    )

)

As you can see, the first two array have a similar data on [diagnosa] array. I need to combine the array inside the [diagnosa] array.

So if the array inside it have same value on it's [diagnosa_kode], [jeniskelamin], [pasienhidupmati], and [diagnosa_nama], then it should add the [jmlpasien] value, while preserving the other value.

For example, on the first array, the result should be like this:

Array
(
    [diagnosa_id] => 6
    [jeniskelamin] => LAKI-LAKI
    [diagnosa] => Array
    (
        [0] => Array
            (
                [diagnosa_kode] => A01.0
                [jeniskelamin] => LAKI-LAKI
                [pasienhidupmati] => HIDUP
                [diagnosa_nama] => Demam tifoid
                [jmlpasien] => 2
            )       

    )

)

Sorry if it's too complicated, but I just can't figure it out on implementing it on php.


Solution

  • Yes you can do it, you just need a temporary holder for the grouping, and then after merging you can put them back again. Example:

    foreach ($array as &$value) {
        $diagnosa = $value['diagnosa'];
        $temp = array(); // initialize the temp holder
        foreach($diagnosa as $d) {
            $name = "$d[diagnosa_kode] - $d[jeniskelamin] - $d[pasienhidupmati]"; // create a grouping index
            if(!isset($temp[$name])) {
                $temp[$name] = $d; // simple group initialization
            } else {
                $temp[$name]['jmlpasien'] += $d['jmlpasien']; // if its there already, just added them up
            }
        }
        $diagnosa = array_values($temp); // simple reindexing
        $value['diagnosa'] = $diagnosa; // overwrite the values
    }
    

    Fiddle