De esta tabla:
De esta tabla

Obtengo esta matriz en PHP: matriz en PHP

A esta matriz quiero agregar una nueva línea, sé category_id y parent_id, ¿cómo programo id_path y el nivel?

Edited1: Como ejemplo, la última línea: category_id: 107, parent_id: 106, ¿cómo codificar id_path = 96/106/107 y nivel? id_path: una secuencia de ID de categoría separados por barras

Editado2:

    function test() {
        $arGiven = [ 
            [
                'category_id' => 100,
                'parent_id' => 107,
                'id_path' => '',
                'level' => 0,
            ],
            [
                'category_id' => 106,
                'parent_id' => 96,
                'id_path' => '',
                'level' => 0,
            ],
            [
                'category_id' => 107,
                'parent_id' => 106,
                'id_path' => '',
                'level' => 0,
            ]

        ];

        $arExpected = [ 
            [
                'category_id' => 100,
                'parent_id' => 107,
                'id_path' => '96/106/107/100',
                'level' => 4,
            ],
            [
                'category_id' => 106,
                'parent_id' => 96,
                'id_path' => '96/106',
                'level' => 2,
            ],
            [
                'category_id' => 107,
                'parent_id' => 106,
                'id_path' => '96/106/107',
                'level' => 3,
            ]

        ];

        $result = $arGiven;

        return $result;
    }

Edited3: lo probé con este código:

    function test() {
        $arGiven = [ 
            [
                'category_id' => 100,
                'parent_id' => 107,
                'id_path' => '',
                'level' => 0,
            ],
            [
                'category_id' => 106,
                'parent_id' => 96,
                'id_path' => '',
                'level' => 0,
            ],
            [
                'category_id' => 107,
                'parent_id' => 106,
                'id_path' => '',
                'level' => 0,
            ]

        ];

        $arExpected = [ 
            [
                'category_id' => 100,
                'parent_id' => 107,
                'id_path' => '96/106/107/100',
                'level' => 4,
            ],
            [
                'category_id' => 106,
                'parent_id' => 96,
                'id_path' => '96/106',
                'level' => 2,
            ],
            [
                'category_id' => 107,
                'parent_id' => 106,
                'id_path' => '96/106/107',
                'level' => 3,
            ]

        ];

        $arrays = $arGiven;

        function getParent($arr, $parent){
            foreach($arr as $v){
                if($v['parent_id'] == $parent) return $v['parent_id'];
            }
        
          return false;
        }

        $newArray=[];
        $i = 0;
        foreach ($arrays as $array){
            $newArray[] = $newArray[$i-1] . '/'. getParent($arrays, $array['parent_id']);
            $i++;
        }

        $test=1;
        return $array;
    }

Pero $ newArray es

[ 
  "/107",
  "/107/96",
  "/107/96/106"
]

En lugar de:

[
    '96/106/107/100',
    '96/106',
    '96/106/107'
]
0
Goran Strainovic 31 jul. 2020 a las 18:17

1 respuesta

La mejor respuesta

Estoy seguro de que realmente se puede optimizar:

<?php
    $arGiven = [ 
    // add the root category
        [
            'category_id' => 96,
            'parent_id' => 0,
            'id_path' => '',
            'level' => 0,
        ],
        [
            'category_id' => 100,
            'parent_id' => 107,
            'id_path' => '',
            'level' => 0,
        ],
        [
            'category_id' => 106,
            'parent_id' => 96,
            'id_path' => '',
            'level' => 0,
        ],
        [
            'category_id' => 107,
            'parent_id' => 106,
            'id_path' => '',
            'level' => 0,
        ]
    ];

    $level = 0;
    $parentLevelIds = [0];
    do {
        $level++;
        
        $nextParentLevelIds = [];
        $arGiven = array_map(function($value) use($level, $parentLevelIds, &$nextParentLevelIds, $arGiven) {
            if (in_array($value['parent_id'], $parentLevelIds)) {
                $path = '';
                if ($value['parent_id'] !== 0) {
                    $parent = array_filter($arGiven, function($parentSearchValue) use ($value) {
                        return $parentSearchValue['category_id'] == $value['parent_id'];
                    });
                    if (!empty($parent)) {
                        $path = array_shift($parent)['id_path'];
                    }
                }
                $value['level'] = $level;
                $value['id_path'] = empty($path) ? $value['category_id'] : ( $path . '/' . $value['category_id'] );
                $nextParentLevelIds[] = $value['category_id'];
            }
            return $value;
        }, $arGiven);

        $parentLevelIds = $nextParentLevelIds;

        $hasLevel0 = !empty(array_filter($arGiven, function($value) {
            return $value['level'] == 0;
        }));
    } while ($hasLevel0);
0
Bazaim 1 ago. 2020 a las 01:06