1

i want array nested base of digit/character who divide by dot

i already tried any method what i know but the result not show as i want

this is example what i want (base of this 3, 3.1, 3.1.1, 3.1.2, 3.1.3) nested array base number of character.

My current array:

array (
82 => 
  array (
    'APBKID' => '83',
    'KodeRekening' => '3',
    'LevelRekening' => NULL,
    'KodeRekeningInduk' => NULL,
    'Uraian' => 'PEMBIAYAAN',
    'Anggaran' => '0',
    'Keterangan' => NULL,
    'TahunAnggaran' => '2019',
    'KampungID' => '1',
    'DanaDesa' => NULL,
    'AlokasiDanaDesa' => NULL,
    'CreatedAt' => '2019-08-10 18: 20: 36',
    'CreatedBy' => '1',
    'UpdatedAt' => '2019-09-06 12: 31: 21',
    'UpdatedBy' => '1',
    'Deleted' => '0',
    'DistrikID' => '1',
    'NamaKampung' => 'Amungun',
    'KepalaKampung' => 'Joni Niwilingame',
    'FotoKepalaKampung' => '',
    'KodeRegisterKampung' => '91.09.02',
    'GambarKampung' => 'splash.jpg',
    'ProfilKampung' => '<p><strong style="margin: 0px; padding: 0px; font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">Lorem Ipsum</strong><span style="font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">&nbsp;is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</span><br></p>',
  ),
83 => 
  array (
    'APBKID' => '84',
    'KodeRekening' => '3.1',
    'LevelRekening' => NULL,
    'KodeRekeningInduk' => NULL,
    'Uraian' => 'Penerimaan Pembiayaan ',
    'Anggaran' => '99475000',
    'Keterangan' => NULL,
    'TahunAnggaran' => '2019',
    'KampungID' => '1',
    'DanaDesa' => NULL,
    'AlokasiDanaDesa' => NULL,
    'CreatedAt' => '2019-08-10 18: 20: 36',
    'CreatedBy' => '1',
    'UpdatedAt' => '2019-09-06 12: 31: 21',
    'UpdatedBy' => '1',
    'Deleted' => '0',
    'DistrikID' => '1',
    'NamaKampung' => 'Amungun',
    'KepalaKampung' => 'Joni Niwilingame',
    'FotoKepalaKampung' => '',
    'KodeRegisterKampung' => '91.09.02',
    'GambarKampung' => 'splash.jpg',
    'ProfilKampung' => '<p><strong style="margin: 0px; padding: 0px; font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">Lorem Ipsum</strong><span style="font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">&nbsp;is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</span><br></p>',
  ),
84 => 
  array (
    'APBKID' => '85',
    'KodeRekening' => '3.1.1',
    'LevelRekening' => NULL,
    'KodeRekeningInduk' => NULL,
    'Uraian' => 'SILPA ',
    'Anggaran' => '99475000',
    'Keterangan' => NULL,
    'TahunAnggaran' => '2019',
    'KampungID' => '1',
    'DanaDesa' => NULL,
    'AlokasiDanaDesa' => NULL,
    'CreatedAt' => '2019-08-10 18: 20: 36',
    'CreatedBy' => '1',
    'UpdatedAt' => '2019-09-06 12: 31: 21',
    'UpdatedBy' => '1',
    'Deleted' => '0',
    'DistrikID' => '1',
    'NamaKampung' => 'Amungun',
    'KepalaKampung' => 'Joni Niwilingame',
    'FotoKepalaKampung' => '',
    'KodeRegisterKampung' => '91.09.02',
    'GambarKampung' => 'splash.jpg',
    'ProfilKampung' => '<p><strong style="margin: 0px; padding: 0px; font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">Lorem Ipsum</strong><span style="font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">&nbsp;is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</span><br></p>',
  ),
85 => 
  array (
    'APBKID' => '86',
    'KodeRekening' => '3.1.2',
    'LevelRekening' => NULL,
    'KodeRekeningInduk' => NULL,
    'Uraian' => 'Pencairan Dana Cadangan ',
    'Anggaran' => '0',
    'Keterangan' => NULL,
    'TahunAnggaran' => '2019',
    'KampungID' => '1',
    'DanaDesa' => NULL,
    'AlokasiDanaDesa' => NULL,
    'CreatedAt' => '2019-08-10 18: 20: 36',
    'CreatedBy' => '1',
    'UpdatedAt' => '2019-09-06 12: 31: 21',
    'UpdatedBy' => '1',
    'Deleted' => '0',
    'DistrikID' => '1',
    'NamaKampung' => 'Amungun',
    'KepalaKampung' => 'Joni Niwilingame',
    'FotoKepalaKampung' => '',
    'KodeRegisterKampung' => '91.09.02',
    'GambarKampung' => 'splash.jpg',
    'ProfilKampung' => '<p><strong style="margin: 0px; padding: 0px; font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">Lorem Ipsum</strong><span style="font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">&nbsp;is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</span><br></p>',
  ),
86 => 
  array (
    'APBKID' => '87',
    'KodeRekening' => '3.1.3',
    'LevelRekening' => NULL,
    'KodeRekeningInduk' => NULL,
    'Uraian' => 'Hasil Kekayaan Desa Yang dipisahkan ',
    'Anggaran' => '0',
    'Keterangan' => NULL,
    'TahunAnggaran' => '2019',
    'KampungID' => '1',
    'DanaDesa' => NULL,
    'AlokasiDanaDesa' => NULL,
    'CreatedAt' => '2019-08-10 18: 20: 36',
    'CreatedBy' => '1',
    'UpdatedAt' => '2019-09-06 12: 31: 21',
    'UpdatedBy' => '1',
    'Deleted' => '0',
    'DistrikID' => '1',
    'NamaKampung' => 'Amungun',
    'KepalaKampung' => 'Joni Niwilingame',
    'FotoKepalaKampung' => '',
    'KodeRegisterKampung' => '91.09.02',
    'GambarKampung' => 'splash.jpg',
    'ProfilKampung' => '<p><strong style="margin: 0px; padding: 0px; font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">Lorem Ipsum</strong><span style="font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">&nbsp;is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</span><br></p>',
  ),
87 => 
  array (
    'APBKID' => '88',
    'KodeRekening' => '3.2',
    'LevelRekening' => NULL,
    'KodeRekeningInduk' => NULL,
    'Uraian' => 'Pengeluaran   Pembiayaan ',
    'Anggaran' => '0',
    'Keterangan' => NULL,
    'TahunAnggaran' => '2019',
    'KampungID' => '1',
    'DanaDesa' => NULL,
    'AlokasiDanaDesa' => NULL,
    'CreatedAt' => '2019-08-10 18: 20: 36',
    'CreatedBy' => '1',
    'UpdatedAt' => '2019-09-06 12: 31: 21',
    'UpdatedBy' => '1',
    'Deleted' => '0',
    'DistrikID' => '1',
    'NamaKampung' => 'Amungun',
    'KepalaKampung' => 'Joni Niwilingame',
    'FotoKepalaKampung' => '',
    'KodeRegisterKampung' => '91.09.02',
    'GambarKampung' => 'splash.jpg',
    'ProfilKampung' => '<p><strong style="margin: 0px; padding: 0px; font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">Lorem Ipsum</strong><span style="font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">&nbsp;is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</span><br></p>',
  ),
88 => 
  array (
    'APBKID' => '89',
    'KodeRekening' => '3.2.1',
    'LevelRekening' => NULL,
    'KodeRekeningInduk' => NULL,
    'Uraian' => 'Pembentukan Dana Cadangan ',
    'Anggaran' => '0',
    'Keterangan' => NULL,
    'TahunAnggaran' => '2019',
    'KampungID' => '1',
    'DanaDesa' => NULL,
    'AlokasiDanaDesa' => NULL,
    'CreatedAt' => '2019-08-10 18: 20: 36',
    'CreatedBy' => '1',
    'UpdatedAt' => '2019-09-06 12: 31: 21',
    'UpdatedBy' => '1',
    'Deleted' => '0',
    'DistrikID' => '1',
    'NamaKampung' => 'Amungun',
    'KepalaKampung' => 'Joni Niwilingame',
    'FotoKepalaKampung' => '',
    'KodeRegisterKampung' => '91.09.02',
    'GambarKampung' => 'splash.jpg',
    'ProfilKampung' => '<p><strong style="margin: 0px; padding: 0px; font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">Lorem Ipsum</strong><span style="font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">&nbsp;is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</span><br></p>',
  ),
89 => 
  array (
    'APBKID' => '90',
    'KodeRekening' => '3.2.2',
    'LevelRekening' => NULL,
    'KodeRekeningInduk' => NULL,
    'Uraian' => 'Penyertaan Modal Desa ',
    'Anggaran' => '0',
    'Keterangan' => NULL,
    'TahunAnggaran' => '2019',
    'KampungID' => '1',
    'DanaDesa' => NULL,
    'AlokasiDanaDesa' => NULL,
    'CreatedAt' => '2019-08-10 18: 20: 36',
    'CreatedBy' => '1',
    'UpdatedAt' => '2019-09-06 12: 31: 21',
    'UpdatedBy' => '1',
    'Deleted' => '0',
    'DistrikID' => '1',
    'NamaKampung' => 'Amungun',
    'KepalaKampung' => 'Joni Niwilingame',
    'FotoKepalaKampung' => '',
    'KodeRegisterKampung' => '91.09.02',
    'GambarKampung' => 'splash.jpg',
    'ProfilKampung' => '<p><strong style="margin: 0px; padding: 0px; font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">Lorem Ipsum</strong><span style="font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">&nbsp;is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</span><br></p>',
  ),

Required Output:

  {
        "APBKID": "1404",
        "KodeRekening": "3",
        "LevelRekening": "",
        "KodeRekeningInduk": "",
        "Uraian": "PEMBIAYAAN",
        "Anggaran": "",
        "Keterangan": "",
        "TahunAnggaran": "2019",
        "DanaDesa": "",
        "AlokasiDanaDesa": "",
        "KampungID": "2",
        "NamaKampung": "Aramsolki",
        "Data": [
            {
                "APBKID": "1405",
                "KodeRekening": "3.1",
                "LevelRekening": "",
                "KodeRekeningInduk": "",
                "Uraian": "Penerimaan Pembiayaan ",
                "Anggaran": "99475000",
                "Keterangan": "",
                "TahunAnggaran": "2019",
                "DanaDesa": "",
                "AlokasiDanaDesa": "",
                "KampungID": "2",
                "NamaKampung": "Aramsolki",
                "Data": [
                    {
                        "APBKID": "1406",
                        "KodeRekening": "3.1.1",
                        "LevelRekening": "",
                        "KodeRekeningInduk": "",
                        "Uraian": "SILPA ",
                        "Anggaran": "99475000",
                        "Keterangan": "",
                        "TahunAnggaran": "2019",
                        "DanaDesa": "",
                        "AlokasiDanaDesa": "",
                        "KampungID": "2",
                        "NamaKampung": "Aramsolki"

                    },
                    {
                        "APBKID": "1407",
                        "KodeRekening": "3.1.2",
                        "LevelRekening": "",
                        "KodeRekeningInduk": "",
                        "Uraian": "Pencairan Dana Cadangan ",
                        "Anggaran": "",
                        "Keterangan": "",
                        "TahunAnggaran": "2019",
                        "DanaDesa": "",
                        "AlokasiDanaDesa": "",
                        "KampungID": "2",
                        "NamaKampung": "Aramsolki"
                    },
                ]
            }
        ]
    }

Does anyone know how to implement this?

nice_dev
  • 17,053
  • 2
  • 21
  • 35
Hendri Sildrian
  • 95
  • 1
  • 1
  • 9

1 Answers1

0

This is a bit tricky problem as we need to deal with references here(at least in my solution). So, we would go step by step.

Sort the data:

<?php 

// assume your array name is $arr 

usort($arr,function($array1,$array2){
    $version_1 = explode(".",$array1['KodeRekening']);
    $version_2 = explode(".",$array2['KodeRekening']);

    $size_1 = count($version_1);
    $size_2 = count($version_2);

    $min_size = min($size_1,$size_2);

    for($i=0;$i<$min_size;++$i){
        if(intval($version_1[$i]) < intval($version_2[$i])){
            return -1;
        }else if(intval($version_1[$i]) > intval($version_2[$i])){
            return 1;
        }
    }

    return $size_1 - $size_2;
});

In the above code, we sort your array data in a manner that always parent version would come before child version. For example: 3.1 would come before 3.1.1 etc.

Get last Index:

function lastIndex($arr){
    $keys = array_keys($arr);
    return end($keys);
}

We will use the above function to get last element of the array in the below code. It's better to make it a function as we would be reusing it more than once.

Actual computation:

<?php

$set = [];
$result = [];

foreach($arr as $curr){
    $version = $curr['KodeRekening'];
    $parent_version = implode(".",array_slice(explode(".",$version),0,-1));

    if(isset($set[$parent_version])){
        $parent_element = &$set[$parent_version];
        if(!isset($parent_element['data'])) $parent_element['data'] = [];
        $parent_element['data'][] = $curr;
        $set[$version] = &$parent_element['data'][lastIndex($parent_element['data'])];
    }else{
        $result[] = $curr;
        $set[$version] = &$result[lastIndex($result)];  
    }
}

In the above code:

  • We have 2 arrays $set and $result. We will use $set to get the parent array quickly and we will use $result to store the final output.

  • Now, for each version, we compute a parent version from your key KodeRekening. The way we do that is just to remove the last dot (.) and everything after it.

  • Once we get the parent version, we check to see if parent_version exists in our $set. If no, we add it to $result as is and store the current_version in $set. If yes, we get the parent_version from our $set and add it in it's data key. We also save current_version in $set accordingly.

  • We use references & in our code. This means that we edit the same copy of array present in $result. This means that any modification we do, such as $parent_element['data'][] = $curr; will automatically reflect in $result since we are passing variables by references here, so we are editing the same copy as written in lines $set[$version] = &$parent_element['data'][lastIndex($parent_element['data'])]; and $set[$version] = &$result[lastIndex($result)];.

Full Code:

<?php

$arr = array (
82 => 
  array (
    'APBKID' => '83',
    'KodeRekening' => '3',
    'LevelRekening' => NULL,
    'KodeRekeningInduk' => NULL,
    'Uraian' => 'PEMBIAYAAN',
    'Anggaran' => '0',
    'Keterangan' => NULL,
    'TahunAnggaran' => '2019',
    'KampungID' => '1',
    'DanaDesa' => NULL,
    'AlokasiDanaDesa' => NULL,
    'CreatedAt' => '2019-08-10 18: 20: 36',
    'CreatedBy' => '1',
    'UpdatedAt' => '2019-09-06 12: 31: 21',
    'UpdatedBy' => '1',
    'Deleted' => '0',
    'DistrikID' => '1',
    'NamaKampung' => 'Amungun',
    'KepalaKampung' => 'Joni Niwilingame',
    'FotoKepalaKampung' => '',
    'KodeRegisterKampung' => '91.09.02',
    'GambarKampung' => 'splash.jpg',
    'ProfilKampung' => '<p><strong style="margin: 0px; padding: 0px; font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">Lorem Ipsum</strong><span style="font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">&nbsp;is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</span><br></p>',
  ),
83 => 
  array (
    'APBKID' => '84',
    'KodeRekening' => '3.1',
    'LevelRekening' => NULL,
    'KodeRekeningInduk' => NULL,
    'Uraian' => 'Penerimaan Pembiayaan ',
    'Anggaran' => '99475000',
    'Keterangan' => NULL,
    'TahunAnggaran' => '2019',
    'KampungID' => '1',
    'DanaDesa' => NULL,
    'AlokasiDanaDesa' => NULL,
    'CreatedAt' => '2019-08-10 18: 20: 36',
    'CreatedBy' => '1',
    'UpdatedAt' => '2019-09-06 12: 31: 21',
    'UpdatedBy' => '1',
    'Deleted' => '0',
    'DistrikID' => '1',
    'NamaKampung' => 'Amungun',
    'KepalaKampung' => 'Joni Niwilingame',
    'FotoKepalaKampung' => '',
    'KodeRegisterKampung' => '91.09.02',
    'GambarKampung' => 'splash.jpg',
    'ProfilKampung' => '<p><strong style="margin: 0px; padding: 0px; font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">Lorem Ipsum</strong><span style="font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">&nbsp;is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</span><br></p>',
  ),
84 => 
  array (
    'APBKID' => '85',
    'KodeRekening' => '3.1.1',
    'LevelRekening' => NULL,
    'KodeRekeningInduk' => NULL,
    'Uraian' => 'SILPA ',
    'Anggaran' => '99475000',
    'Keterangan' => NULL,
    'TahunAnggaran' => '2019',
    'KampungID' => '1',
    'DanaDesa' => NULL,
    'AlokasiDanaDesa' => NULL,
    'CreatedAt' => '2019-08-10 18: 20: 36',
    'CreatedBy' => '1',
    'UpdatedAt' => '2019-09-06 12: 31: 21',
    'UpdatedBy' => '1',
    'Deleted' => '0',
    'DistrikID' => '1',
    'NamaKampung' => 'Amungun',
    'KepalaKampung' => 'Joni Niwilingame',
    'FotoKepalaKampung' => '',
    'KodeRegisterKampung' => '91.09.02',
    'GambarKampung' => 'splash.jpg',
    'ProfilKampung' => '<p><strong style="margin: 0px; padding: 0px; font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">Lorem Ipsum</strong><span style="font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">&nbsp;is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</span><br></p>',
  ),
85 => 
  array (
    'APBKID' => '86',
    'KodeRekening' => '3.1.2',
    'LevelRekening' => NULL,
    'KodeRekeningInduk' => NULL,
    'Uraian' => 'Pencairan Dana Cadangan ',
    'Anggaran' => '0',
    'Keterangan' => NULL,
    'TahunAnggaran' => '2019',
    'KampungID' => '1',
    'DanaDesa' => NULL,
    'AlokasiDanaDesa' => NULL,
    'CreatedAt' => '2019-08-10 18: 20: 36',
    'CreatedBy' => '1',
    'UpdatedAt' => '2019-09-06 12: 31: 21',
    'UpdatedBy' => '1',
    'Deleted' => '0',
    'DistrikID' => '1',
    'NamaKampung' => 'Amungun',
    'KepalaKampung' => 'Joni Niwilingame',
    'FotoKepalaKampung' => '',
    'KodeRegisterKampung' => '91.09.02',
    'GambarKampung' => 'splash.jpg',
    'ProfilKampung' => '<p><strong style="margin: 0px; padding: 0px; font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">Lorem Ipsum</strong><span style="font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">&nbsp;is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</span><br></p>',
  ),
86 => 
  array (
    'APBKID' => '87',
    'KodeRekening' => '3.1.3',
    'LevelRekening' => NULL,
    'KodeRekeningInduk' => NULL,
    'Uraian' => 'Hasil Kekayaan Desa Yang dipisahkan ',
    'Anggaran' => '0',
    'Keterangan' => NULL,
    'TahunAnggaran' => '2019',
    'KampungID' => '1',
    'DanaDesa' => NULL,
    'AlokasiDanaDesa' => NULL,
    'CreatedAt' => '2019-08-10 18: 20: 36',
    'CreatedBy' => '1',
    'UpdatedAt' => '2019-09-06 12: 31: 21',
    'UpdatedBy' => '1',
    'Deleted' => '0',
    'DistrikID' => '1',
    'NamaKampung' => 'Amungun',
    'KepalaKampung' => 'Joni Niwilingame',
    'FotoKepalaKampung' => '',
    'KodeRegisterKampung' => '91.09.02',
    'GambarKampung' => 'splash.jpg',
    'ProfilKampung' => '<p><strong style="margin: 0px; padding: 0px; font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">Lorem Ipsum</strong><span style="font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">&nbsp;is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</span><br></p>',
  ),
87 => 
  array (
    'APBKID' => '88',
    'KodeRekening' => '3.2',
    'LevelRekening' => NULL,
    'KodeRekeningInduk' => NULL,
    'Uraian' => 'Pengeluaran   Pembiayaan ',
    'Anggaran' => '0',
    'Keterangan' => NULL,
    'TahunAnggaran' => '2019',
    'KampungID' => '1',
    'DanaDesa' => NULL,
    'AlokasiDanaDesa' => NULL,
    'CreatedAt' => '2019-08-10 18: 20: 36',
    'CreatedBy' => '1',
    'UpdatedAt' => '2019-09-06 12: 31: 21',
    'UpdatedBy' => '1',
    'Deleted' => '0',
    'DistrikID' => '1',
    'NamaKampung' => 'Amungun',
    'KepalaKampung' => 'Joni Niwilingame',
    'FotoKepalaKampung' => '',
    'KodeRegisterKampung' => '91.09.02',
    'GambarKampung' => 'splash.jpg',
    'ProfilKampung' => '<p><strong style="margin: 0px; padding: 0px; font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">Lorem Ipsum</strong><span style="font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">&nbsp;is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</span><br></p>',
  ),
88 => 
  array (
    'APBKID' => '89',
    'KodeRekening' => '3.2.1',
    'LevelRekening' => NULL,
    'KodeRekeningInduk' => NULL,
    'Uraian' => 'Pembentukan Dana Cadangan ',
    'Anggaran' => '0',
    'Keterangan' => NULL,
    'TahunAnggaran' => '2019',
    'KampungID' => '1',
    'DanaDesa' => NULL,
    'AlokasiDanaDesa' => NULL,
    'CreatedAt' => '2019-08-10 18: 20: 36',
    'CreatedBy' => '1',
    'UpdatedAt' => '2019-09-06 12: 31: 21',
    'UpdatedBy' => '1',
    'Deleted' => '0',
    'DistrikID' => '1',
    'NamaKampung' => 'Amungun',
    'KepalaKampung' => 'Joni Niwilingame',
    'FotoKepalaKampung' => '',
    'KodeRegisterKampung' => '91.09.02',
    'GambarKampung' => 'splash.jpg',
    'ProfilKampung' => '<p><strong style="margin: 0px; padding: 0px; font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">Lorem Ipsum</strong><span style="font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">&nbsp;is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</span><br></p>',
  ),
89 => 
  array (
    'APBKID' => '90',
    'KodeRekening' => '3.2.2',
    'LevelRekening' => NULL,
    'KodeRekeningInduk' => NULL,
    'Uraian' => 'Penyertaan Modal Desa ',
    'Anggaran' => '0',
    'Keterangan' => NULL,
    'TahunAnggaran' => '2019',
    'KampungID' => '1',
    'DanaDesa' => NULL,
    'AlokasiDanaDesa' => NULL,
    'CreatedAt' => '2019-08-10 18: 20: 36',
    'CreatedBy' => '1',
    'UpdatedAt' => '2019-09-06 12: 31: 21',
    'UpdatedBy' => '1',
    'Deleted' => '0',
    'DistrikID' => '1',
    'NamaKampung' => 'Amungun',
    'KepalaKampung' => 'Joni Niwilingame',
    'FotoKepalaKampung' => '',
    'KodeRegisterKampung' => '91.09.02',
    'GambarKampung' => 'splash.jpg',
    'ProfilKampung' => '<p><strong style="margin: 0px; padding: 0px; font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">Lorem Ipsum</strong><span style="font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">&nbsp;is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</span><br></p>',
  )
);


usort($arr,function($array1,$array2){
    $version_1 = explode(".",$array1['KodeRekening']);
    $version_2 = explode(".",$array2['KodeRekening']);

    $size_1 = count($version_1);
    $size_2 = count($version_2);

    $min_size = min($size_1,$size_2);

    for($i=0;$i<$min_size;++$i){
        if(intval($version_1[$i]) < intval($version_2[$i])){
            return -1;
        }else if(intval($version_1[$i]) > intval($version_2[$i])){
            return 1;
        }
    }

    return $size_1 - $size_2;
});

$set = [];
$result = [];

foreach($arr as $curr){
    $version = $curr['KodeRekening'];
    $parent_version = implode(".",array_slice(explode(".",$version),0,-1));

    if(isset($set[$parent_version])){
        $parent_element = &$set[$parent_version];
        if(!isset($parent_element['data'])) $parent_element['data'] = [];
        $parent_element['data'][] = $curr;
        $set[$version] = &$parent_element['data'][lastIndex($parent_element['data'])];
    }else{
        $result[] = $curr;
        $set[$version] = &$result[lastIndex($result)];  
    }
}


function lastIndex($arr){
    $keys = array_keys($arr);
    return end($keys);
}

print_r($result);

Demo: https://3v4l.org/U4ssp

nice_dev
  • 17,053
  • 2
  • 21
  • 35