FaredCesar
FaredCesar

Reputation: 21

convert simple array to multidimensional

[
  {
    "id": "209",
    "weight": "1.00",
    "Parent": "True",
    "idParent": "209",
    "replace": "False",
    "idreplace": "209",
    "status": "1",
    "idType": "7",
    "grade": "14462",
    "section": "16370",
    "created_at": "01/04/2022 12:46:06 p.m.",
    "updated_at": "20/10/2022 12:04:49 p.m.",
    "user_register": "698",
    "user_updated": "698",
    "number": "1",
    "Type": "NOTA FINAL",
    "Initials": "NF",
    "Enabled": "False",
    "Silabo_at": "13/06/2022 12:00:00 a.m.",
    "Limit_at": "08/08/2022 11:26:00 a.m."
  },
  {
    "id": "210",
    "weight": "1.00",
    "Parent": "True",
    "idParent": "209",
    "replace": "False",
    "idReemplazo": "210",
    "status": "1",
    "idType": "8",
    "grade": "14462",
    "section": "16370",
    "created_at": "01/04/2022 12:46:06 p.m.",
    "updated_at": "20/10/2022 12:04:49 p.m.",
    "user_register": "698",
    "user_updated": "698",
    "number": "2",
    "Type": "CAPACIDAD 1",
    "Initials": "C1",
    "Enabled": "True",
    "Silabo_at": "13/06/2022 12:00:00 a.m.",
    "Limit_at": "08/08/2022 11:26:00 a.m."
  },
  {
    "id": "212",
    "weight": "1.00",
    "Parent": "True",
    "idParent": "209",
    "replace": "False",
    "idReemplazo": "212",
    "status": "1",
    "idType": "14",
    "grade": "14462",
    "section": "16370",
    "created_at": "01/04/2022 12:46:06 p.m.",
    "updated_at": "20/10/2022 12:04:49 p.m.",
    "user_register": "698",
    "user_updated": "698",
    "number": "4",
    "Type": "NOTA DE RESULTADO",
    "Initials": "NR",
    "Enabled": "False",
    "Silabo_at": "13/06/2022 12:00:00 a.m.",
    "Limit_at": "08/08/2022 11:26:00 a.m."
  },
  {
    "id": "1019",
    "weight": "1.00",
    "Parent": "True",
    "idParent": "209",
    "replace": "False",
    "idReemplazo": "1019",
    "status": "1",
    "idType": "9",
    "grade": "14462",
    "section": "16370",
    "created_at": "28/09/2022 10:30:57 a.m.",
    "updated_at": "20/10/2022 12:04:49 p.m.",
    "user_register": "698",
    "user_updated": "698",
    "number": "13",
    "Type": "CAPACIDAD 2",
    "Initials": "C2",
    "Enabled": "True",
    "Silabo_at": "13/06/2022 12:00:00 a.m.",
    "Limit_at": "08/08/2022 11:26:00 a.m."
  },
  {
    "id": "215",
    "weight": "1.00",
    "Parent": "False",
    "idParent": "210",
    "replace": "False",
    "idReemplazo": "215",
    "status": "1",
    "idType": "10",
    "grade": "14462",
    "section": "16370",
    "created_at": "01/04/2022 12:46:06 p.m.",
    "updated_at": "18/10/2022 03:23:06 p.m.",
    "user_register": "698",
    "user_updated": "698",
    "number": "7",
    "Type": "INDICADOR DE LOGRO 1",
    "Initials": "IL1-C1",
    "Enabled": "True",
    "Silabo_at": "13/06/2022 12:00:00 a.m.",
    "Limit_at": "08/08/2022 11:26:00 a.m."
  },
  {
    "id": "216",
    "weight": "1.00",
    "Parent": "False",
    "idParent": "210",
    "replace": "False",
    "idReemplazo": "216",
    "status": "1",
    "idType": "11",
    "grade": "14462",
    "section": "16370",
    "created_at": "01/04/2022 12:46:06 p.m.",
    "updated_at": "18/10/2022 03:23:06 p.m.",
    "user_register": "698",
    "user_updated": "698",
    "number": "8",
    "Type": "INDICADOR DE LOGRO 2",
    "Initials": "IL2-C1",
    "Enabled": "True",
    "Silabo_at": "13/06/2022 12:00:00 a.m.",
    "Limit_at": "08/08/2022 11:26:00 a.m."
  },
  {
    "id": "1122",
    "weight": "1.00",
    "Parent": "True",
    "idParent": "210",
    "replace": "False",
    "idReemplazo": "1122",
    "status": "1",
    "idType": "8",
    "grade": "14462",
    "section": "16370",
    "created_at": "18/10/2022 03:35:53 p.m.",
    "updated_at": "",
    "user_register": "1051",
    "user_updated": "",
    "number": "16",
    "Type": "CAPACIDAD 1",
    "Initials": "C1",
    "Enabled": "True",
    "Silabo_at": "13/06/2022 12:00:00 a.m.",
    "Limit_at": "08/08/2022 11:26:00 a.m."
  },
  {
    "id": "213",
    "weight": "1.00",
    "Parent": "False",
    "idParent": "212",
    "replace": "False",
    "idReemplazo": "213",
    "status": "1",
    "idType": "21",
    "grade": "14462",
    "section": "16370",
    "created_at": "01/04/2022 12:46:06 p.m.",
    "updated_at": "20/10/2022 12:04:49 p.m.",
    "user_register": "698",
    "user_updated": "698",
    "number": "5",
    "Type": "NOTA RESULTADO H",
    "Initials": "NRH",
    "Enabled": "True",
    "Silabo_at": "13/06/2022 12:00:00 a.m.",
    "Limit_at": "08/08/2022 11:26:00 a.m."
  },
  {
    "id": "214",
    "weight": "1.00",
    "Parent": "False",
    "idParent": "213",
    "replace": "False",
    "idReemplazo": "213",
    "status": "1",
    "idType": "22",
    "grade": "14462",
    "section": "16370",
    "created_at": "01/04/2022 12:46:06 p.m.",
    "updated_at": "",
    "user_register": "698",
    "user_updated": "",
    "number": "6",
    "Type": "NOTA RESULTADO N",
    "Initials": "NRN",
    "Enabled": "True",
    "Silabo_at": "13/06/2022 12:00:00 a.m.",
    "Limit_at": ""
  },
  {
    "id": "217",
    "weight": "1.00",
    "Parent": "False",
    "idParent": "215",
    "replace": "False",
    "idReemplazo": "217",
    "status": "1",
    "idType": "17",
    "grade": "14462",
    "section": "16370",
    "created_at": "01/04/2022 12:46:32 p.m.",
    "updated_at": "28/09/2022 10:32:00 a.m.",
    "user_register": "698",
    "user_updated": "698",
    "number": "9",
    "Type": "TRABAJOS",
    "Initials": "TR",
    "Enabled": "True",
    "Silabo_at": "13/06/2022 12:00:00 a.m.",
    "Limit_at": ""
  },
  {
    "id": "218",
    "weight": "1.00",
    "Parent": "False",
    "idParent": "215",
    "replace": "False",
    "idReemplazo": "218",
    "status": "1",
    "idType": "18",
    "grade": "14462",
    "section": "16370",
    "created_at": "01/04/2022 12:47:13 p.m.",
    "updated_at": "28/09/2022 03:52:39 p.m.",
    "user_register": "698",
    "user_updated": "698",
    "number": "10",
    "Type": "PARTICIPACION",
    "Initials": "PR",
    "Enabled": "True",
    "Silabo_at": "13/06/2022 12:00:00 a.m.",
    "Limit_at": ""
  },
  {
    "id": "219",
    "weight": "1.00",
    "Parent": "False",
    "idParent": "215",
    "replace": "False",
    "idReemplazo": "219",
    "status": "1",
    "idType": "15",
    "grade": "14462",
    "section": "16370",
    "created_at": "01/04/2022 12:48:01 p.m.",
    "updated_at": "",
    "user_register": "698",
    "user_updated": "",
    "number": "11",
    "Type": "EXAMEN PARCIAL",
    "Initials": "EP",
    "Enabled": "True",
    "Silabo_at": "13/06/2022 12:00:00 a.m.",
    "Limit_at": ""
  },
  {
    "id": "220",
    "weight": "1.00",
    "Parent": "False",
    "idParent": "216",
    "replace": "False",
    "idReemplazo": "220",
    "status": "1",
    "idType": "17",
    "grade": "14462",
    "section": "16370",
    "created_at": "01/04/2022 12:51:33 p.m.",
    "updated_at": "",
    "user_register": "698",
    "user_updated": "",
    "number": "12",
    "Type": "TRABAJOS",
    "Initials": "TR",
    "Enabled": "True",
    "Silabo_at": "13/06/2022 12:00:00 a.m.",
    "Limit_at": ""
  },
  {
    "id": "221",
    "weight": "1.00",
    "Parent": "False",
    "idParent": "216",
    "replace": "False",
    "idReemplazo": "221",
    "status": "1",
    "idType": "18",
    "grade": "14462",
    "section": "16370",
    "created_at": "01/04/2022 12:51:41 p.m.",
    "updated_at": "01/04/2022 12:52:03 p.m.",
    "user_register": "698",
    "user_updated": "698",
    "number": "13",
    "Type": "PARTICIPACION",
    "Initials": "PR",
    "Enabled": "True",
    "Silabo_at": "13/06/2022 12:00:00 a.m.",
    "Limit_at": ""
  },
  {
    "id": "1120",
    "weight": "1.00",
    "Parent": "False",
    "idParent": "1019",
    "replace": "False",
    "idReemplazo": "1120",
    "status": "1",
    "idType": "12",
    "grade": "14462",
    "section": "16370",
    "created_at": "18/10/2022 10:47:40 a.m.",
    "updated_at": "20/10/2022 12:04:49 p.m.",
    "user_register": "698",
    "user_updated": "698",
    "number": "14",
    "Type": "INDICADOR DE LOGRO 1",
    "Initials": "IL1-C2",
    "Enabled": "True",
    "Silabo_at": "13/06/2022 12:00:00 a.m.",
    "Limit_at": "08/08/2022 11:26:00 a.m."
  },
  {
    "id": "1121",
    "weight": "1.00",
    "Parent": "False",
    "idParent": "1019",
    "replace": "False",
    "idReemplazo": "1121",
    "status": "1",
    "idType": "13",
    "grade": "14462",
    "section": "16370",
    "created_at": "18/10/2022 10:47:40 a.m.",
    "updated_at": "20/10/2022 12:04:49 p.m.",
    "user_register": "698",
    "user_updated": "698",
    "number": "15",
    "Type": "INDICADOR DE LOGRO 2",
    "Initials": "IL2-C2",
    "Enabled": "True",
    "Silabo_at": "13/06/2022 12:00:00 a.m.",
    "Limit_at": "08/08/2022 11:26:00 a.m."
  },
  {
    "id": "1123",
    "weight": "1.00",
    "Parent": "True",
    "idParent": "1122",
    "replace": "False",
    "idReemplazo": "1123",
    "status": "1",
    "idType": "8",
    "grade": "14462",
    "section": "16370",
    "created_at": "18/10/2022 03:37:41 p.m.",
    "updated_at": "18/10/2022 03:58:36 p.m.",
    "user_register": "1051",
    "user_updated": "1051",
    "number": "17",
    "Type": "CAPACIDAD 1",
    "Initials": "C1",
    "Enabled": "True",
    "Silabo_at": "13/06/2022 12:00:00 a.m.",
    "Limit_at": "04/04/2022 11:26:00 a.m."
  },
  {
    "id": "1182",
    "weight": "1.00",
    "Parent": "False",
    "idParent": "1122",
    "replace": "False",
    "idReemplazo": "1182",
    "status": "1",
    "idType": "10",
    "grade": "14462",
    "section": "16370",
    "created_at": "19/10/2022 12:56:22 p.m.",
    "updated_at": "20/10/2022 12:04:49 p.m.",
    "user_register": "698",
    "user_updated": "698",
    "number": "18",
    "Type": "INDICADOR DE LOGRO 1",
    "Initials": "IL1-C1",
    "Enabled": "True",
    "Silabo_at": "13/06/2022 12:00:00 a.m.",
    "Limit_at": "08/08/2022 11:26:00 a.m."
  },
  {
    "id": "1183",
    "weight": "1.00",
    "Parent": "False",
    "idParent": "1122",
    "replace": "False",
    "idReemplazo": "1183",
    "status": "1",
    "idType": "11",
    "grade": "14462",
    "section": "16370",
    "created_at": "19/10/2022 12:56:22 p.m.",
    "updated_at": "20/10/2022 12:04:49 p.m.",
    "user_register": "698",
    "user_updated": "698",
    "number": "19",
    "Type": "INDICADOR DE LOGRO 2",
    "Initials": "IL2-C1",
    "Enabled": "True",
    "Silabo_at": "13/06/2022 12:00:00 a.m.",
    "Limit_at": "08/08/2022 11:26:00 a.m."
  }
]

I want to sort this array from a rest api, in tree format where type NF is the initial one and has children "NR", "C1", "C2", "C3" (as far as it can be extended) then each one except NR must have children and these children have their own (maximum 4 levels counted from the initial Father - Children - Children - Children)

I have tried using foreach but the process is very long and it has already failed me several times, I don't know what to do or what I am doing wrong, the system is in Laravel.

sorry my english, I'm using a translator.

P.S. I have also wanted to change the date to Carbon format since it comes from SQL Server.

use this code:

$Datos = [];
        foreach($ArrayGeneral as $Grade){
            if($Grade['Padre'] == 'True'){
                $IdPadre = $Grade['id'];
                $Datos[$IdPadre] = [
                    'Hijos' => []
                ];
                $Datos[$IdPadre] = array_merge($Datos[$IdPadre], $Grade);
                foreach($ArrayGeneral['Datos'] as $GradeHijo){
                    $IdHijo = $GradeHijo['id'];
                    if($Grade['id'] == $GradeHijo['idParent'] AND $GradeHijo['idParent'] != $GradeHijo['id']){
                        $Datos[$IdPadre]['Hijos'][$IdHijo] = [
                            'Nietos' =>[]
                        ];
                        $Datos[$IdPadre]['Hijos'][$IdHijo] = array_merge($Datos[$IdPadre]['Hijos'][$IdHijo], $GradeHijo);
                        foreach($ArrayGeneral['Datos'] as $GradeNieto){
                            $IdNieto = $GradeNieto['id'];
                            if($GradeHijo['id'] == $GradeNieto['idParent']){
                                $Datos[$IdPadre]['Hijos'][$IdHijo]['Nietos'][$IdNieto] = $GradeNieto;
                                $Datos[$IdPadre]['Hijos'][$IdHijo]['Nietos'][$IdNieto] = [
                                    'BisNietos' => []
                                ];
                                $Datos[$IdPadre]['Hijos'][$IdHijo]['Nietos'][$IdNieto] = array_merge($Datos[$IdPadre]['Hijos'][$IdHijo]['Nietos'][$IdNieto], $GradeNieto);
                                foreach($ArrayGeneral['Datos'] as $GradeBisNieto){
                                    if($GradeNieto['id'] == $GradeBisNieto['idParent']){
                                        $Datos[$IdPadre]['Hijos'][$IdHijo]['Nietos'][$IdNieto]['BisNietos'][] = $GradeBisNieto;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        foreach($Datos as $data){
            $Final[] = count($data, COUNT_RECURSIVE);
        }
        $ArrayFinal = array_search(max($Final), $Final);
        $Datos = GeneralApi::fix_keys($Datos);
        $Final = $Datos[$ArrayFinal];

Upvotes: 0

Views: 39

Answers (1)

Tim Roberts
Tim Roberts

Reputation: 54649

You don't need to do things nested like that. A tree is a much more general structure.

Consider the following code. First, I create another array from your simple list, where the array key is the numeric ID. Now I can do lookups based strictly on the ID.

Next, for each element in the array, I just add that element to a new array called "children".

// First, convert the list into a keyed-array.

$tree = array();
foreach($ArrayGeneral as $Grade) 
{
    $tree[$Grade['id']] = $Grade;
    $tree[$Grade['id']]['children'] = array();
}


// Now, build a tree.

foreach($ArrayGeneral as $Grade)
{
    // Find the parent node.
    $parent = $tree[$Grade['idParent']];
    // Add this child to its "children" list.
    $parent['children'][] = $Grade;
    // Remember the root node, if we found it.
    if( $Grade['idParent'] == $Grade['id'] )
        $root = $Grade;
}

Now, you can iterate through the tree starting with the $root node. Every node now has a $children array, which is a list of its direct children.

Upvotes: 1

Related Questions