• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

Spłaszczenie wielowymiarowej tablicy z uwzględnieniem poziomu zagnieżdżenia danego elementu

Object Storage Arubacloud
0 głosów
413 wizyt
pytanie zadane 29 stycznia 2017 w PHP przez Janusz92 Bywalec (2,150 p.)

Witam, mam następujący problem

Pobierając z bazy dane dotyczące kategorii buduję wielowymiarową tablicę, w której uwzględniam rodziców i dzieci (kategorie i subkategorie). Tablica wygląda następująco:

array (size=6)
  18 => 
    array (size=3)
      'id' => string '18' (length=2)
      'parent_id' => null
      'name' => string 'autobusy' (length=8)
  15 => 
    array (size=3)
      'id' => string '15' (length=2)
      'parent_id' => null
      'name' => string 'busy' (length=4)
  2 => 
    array (size=4)
      'id' => string '2' (length=1)
      'parent_id' => null
      'name' => string 'części samochodowe' (length=20)
      'childrens' => 
        array (size=5)
          3 => 
            array (size=4)
              'id' => string '3' (length=1)
              'parent_id' => string '2' (length=1)
              'name' => string 'felgi' (length=5)
              'childrens' => 
                array (size=2)
                  12 => 
                    array (size=3)
                      'id' => string '12' (length=2)
                      'parent_id' => string '3' (length=1)
                      'name' => string 'aluminiowe' (length=10)
                  19 => 
                    array (size=3)
                      'id' => string '19' (length=2)
                      'parent_id' => string '3' (length=1)
                      'name' => string 'zwykłe' (length=7)
          5 => 
            array (size=3)
              'id' => string '5' (length=1)
              'parent_id' => string '2' (length=1)
              'name' => string 'fotele' (length=6)
          7 => 
            array (size=3)
              'id' => string '7' (length=1)
              'parent_id' => string '2' (length=1)
              'name' => string 'hamulce' (length=7)
          4 => 
            array (size=3)
              'id' => string '4' (length=1)
              'parent_id' => string '2' (length=1)
              'name' => string 'opony' (length=5)
          6 => 
            array (size=3)
              'id' => string '6' (length=1)
              'parent_id' => string '2' (length=1)
              'name' => string 'światła' (length=9)
  16 => 
    array (size=3)
      'id' => string '16' (length=2)
      'parent_id' => null
      'name' => string 'lawety' (length=6)
  13 => 
    array (size=3)
      'id' => string '13' (length=2)
      'parent_id' => null
      'name' => string 'samochody ciężarowe' (length=21)
  1 => 
    array (size=4)
      'id' => string '1' (length=1)
      'parent_id' => null
      'name' => string 'samochody osobowe' (length=17)
      'childrens' => 
        array (size=4)
          8 => 
            array (size=3)
              'id' => string '8' (length=1)
              'parent_id' => string '1' (length=1)
              'name' => string 'audi' (length=4)
          9 => 
            array (size=3)
              'id' => string '9' (length=1)
              'parent_id' => string '1' (length=1)
              'name' => string 'bmw' (length=3)
          11 => 
            array (size=3)
              'id' => string '11' (length=2)
              'parent_id' => string '1' (length=1)
              'name' => string 'ford' (length=4)
          10 => 
            array (size=3)
              'id' => string '10' (length=2)
              'parent_id' => string '1' (length=1)
              'name' => string 'nissan' (length=6)

Teraz na podstawie tej tablicy chcę zbudować kolejną tablicę, lecz bez zagnieżdżeń subkategorii, dodatkowo chcę aby przy każdym elemencie tej tablicy była podana wartość poziomu zagnieżdżenia. Np. samochody osobowe, busy, autobusy to poziom 0 (parent_id = null), a audi, bmw, ford to poziom 1.

Spłaszczona tablica wyglądałaby w ten sposób:

<?
array (size=17)
  0 => 
    array (size=4)
      'id' => string '18' (length=2)
      'parent_id' => null
      'name' => string 'autobusy' (length=8)
      'depth' => int 0
  1 => 
    array (size=4)
      'id' => string '15' (length=2)
      'parent_id' => null
      'name' => string 'busy' (length=4)
      'depth' => int 0
  2 => 
    array (size=4)
      'id' => string '2' (length=1)
      'parent_id' => null
      'name' => string 'części samochodowe' (length=20)
      'depth' => int 0
  3 => 
    array (size=4)
      'id' => string '3' (length=1)
      'parent_id' => string '2' (length=1)
      'name' => string 'felgi' (length=5)
      'depth' => int 1
  4 => 
    array (size=4)
      'id' => string '12' (length=2)
      'parent_id' => string '3' (length=1)
      'name' => string 'aluminiowe' (length=10)
      'depth' => int 2
  5 => 
    array (size=4)
      'id' => string '19' (length=2)
      'parent_id' => string '3' (length=1)
      'name' => string 'zwykłe' (length=7)
      'depth' => int 2
  6 => 
    array (size=4)
      'id' => string '5' (length=1)
      'parent_id' => string '2' (length=1)
      'name' => string 'fotele' (length=6)
      'depth' => int 1
  7 => 
    array (size=4)
      'id' => string '7' (length=1)
      'parent_id' => string '2' (length=1)
      'name' => string 'hamulce' (length=7)
      'depth' => int 1
  8 => 
    array (size=4)
      'id' => string '4' (length=1)
      'parent_id' => string '2' (length=1)
      'name' => string 'opony' (length=5)
      'depth' => int 1
  9 => 
    array (size=4)
      'id' => string '6' (length=1)
      'parent_id' => string '2' (length=1)
      'name' => string 'światła' (length=9)
      'depth' => int 1
  10 => 
    array (size=4)
      'id' => string '16' (length=2)
      'parent_id' => null
      'name' => string 'lawety' (length=6)
      'depth' => int 0
  11 => 
    array (size=4)
      'id' => string '13' (length=2)
      'parent_id' => null
      'name' => string 'samochody ciężarowe' (length=21)
      'depth' => int 0
  12 => 
    array (size=4)
      'id' => string '1' (length=1)
      'parent_id' => null
      'name' => string 'samochody osobowe' (length=17)
      'depth' => int 0
  13 => 
    array (size=4)
      'id' => string '8' (length=1)
      'parent_id' => string '1' (length=1)
      'name' => string 'audi' (length=4)
      'depth' => int 1
  14 => 
    array (size=4)
      'id' => string '9' (length=1)
      'parent_id' => string '1' (length=1)
      'name' => string 'bmw' (length=3)
      'depth' => int 1
  15 => 
    array (size=4)
      'id' => string '11' (length=2)
      'parent_id' => string '1' (length=1)
      'name' => string 'ford' (length=4)
      'depth' => int 1
  16 => 
    array (size=4)
      'id' => string '10' (length=2)
      'parent_id' => string '1' (length=1)
      'name' => string 'nissan' (length=6)
      'depth' => int 1

Doszła wartość "depth" i zachowana jest kolejność z tablicy bazowej.

Nie wiem w jaki sposób odpowiednio przyporządkować wartość depth dla odpowiedniego poziomu zagnieżdżenia. Proszę o wskazówki, dziękuję.

komentarz 29 stycznia 2017 przez Janusz92 Bywalec (2,150 p.)

Myślę, że sobie poradziłem :)

function flatArray($array, $depth = 0) {
	foreach ($array as $value) {
		$currentDepth = $depth;

		if($value['parent_id'])
			$currentDepth++;

		if(is_array($value) && !array_key_exists('childrens', $value)) {
			echo '<li>' . $value['name'] . '[' . $currentDepth . ']' . '</li>';
		} elseif(is_array($value)) {
			echo '<li>' . '[PARENT] ' . $value['name'] . '[' . $currentDepth . ']' . '</li>';
			echo '<ul>';
			flatArray($value['childrens'], $currentDepth);
			echo '</ul>';
		}
	}
}

echo '<ul>';
flatArray($categoriesTree);
echo '</ul>';

Tutaj akurat jest przedstawiane roboczo w postaci listy, ale do pożądanej przeze mnie tablicy wystarczy tylko lekko przerobić.

Zaloguj lub zarejestruj się, aby odpowiedzieć na to pytanie.

Podobne pytania

0 głosów
2 odpowiedzi 264 wizyt
0 głosów
1 odpowiedź 775 wizyt
0 głosów
1 odpowiedź 653 wizyt

92,556 zapytań

141,404 odpowiedzi

319,560 komentarzy

61,942 pasjonatów

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj.

Akademia Sekuraka

Kolejna edycja największej imprezy hakerskiej w Polsce, czyli Mega Sekurak Hacking Party odbędzie się już 20 maja 2024r. Z tej okazji mamy dla Was kod: pasjamshp - jeżeli wpiszecie go w koszyku, to wówczas otrzymacie 40% zniżki na bilet w wersji standard!

Więcej informacji na temat imprezy znajdziecie tutaj. Dziękujemy ekipie Sekuraka za taką fajną zniżkę dla wszystkich Pasjonatów!

Akademia Sekuraka

Niedawno wystartował dodruk tej świetnej, rozchwytywanej książki (około 940 stron). Mamy dla Was kod: pasja (wpiszcie go w koszyku), dzięki któremu otrzymujemy 10% zniżki - dziękujemy zaprzyjaźnionej ekipie Sekuraka za taki bonus dla Pasjonatów! Książka to pierwszy tom z serii o ITsec, który łagodnie wprowadzi w świat bezpieczeństwa IT każdą osobę - warto, polecamy!

...