Z tej tabeli:
Z tej tabeli

Dostaję tę tablicę w PHP: Array w PHP

Do tej tablicy chcę dodać nową linię, znam kategorię_id i rodzic_id, jak zaprogramować id_path i poziom?

Edytowany1: Jak na przykład ostatnia linia: kategoria_id: 107, rodzic_id: 106, jak kodue ID_Path = 96/106 / 107 i poziom? id_path: sekwencja identyfikatorów kategorii oddzielonych cięciami

Edytowany2:

    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;
    }

Edytowany3: Próbowałem tego z tym kodem:

    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;
    }

Ale $ Newarray jest

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

Zamiast:

[
    '96/106/107/100',
    '96/106',
    '96/106/107'
]
0
Goran Strainovic 31 lipiec 2020, 18:17

1 odpowiedź

Najlepsza odpowiedź

Jestem pewien, że może być naprawdę zoptymalizowany:

<?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 sierpień 2020, 01:06