Autor Zpráva
Tomas Balvan
Profil *
Dobry den,

chcel by som sa spytat, je mozne vytvorit strom, z tohto pola objektov? Pripadne by ste ma mohli nejakym sposobom naviest? Uz si s tym neviem rady. Snazim sa cez rekurzivnu funkciu, ale nedari sa mi :/

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [parent_id] => 0
            [text] => text1
        )
    [1] => stdClass Object
        (
            [id] => 2
            [parent_id] => 0
            [text] => text2
        )
    [2] => stdClass Object
        (
            [id] => 3
            [parent_id] => 1
            [text] => text3
        )
    [3] => stdClass Object
        (
            [id] => 4
            [parent_id] => 1
            [text] => text4
        )
    [4] => stdClass Object
        (
            [id] => 5
            [parent_id] => 3
            [text] => text5
        )
)


Dakujem za kazdu radu.
ShiraNai7
Profil
Objekt s indexem 0 je svým vlastním rodičem nebo co? :)
Tomas Balvan
Profil *
Ahoj,
tam kde je [parent_id] => 0, tak to je bez rodica (v mojom priklade teda 2 objekty nemaju rodica), a napriklad objekt [parent_id] => 3 (cize objekt s indexom 4) je dieta rodica objektu s indexom 2 ( [id] => 3)

neviem ci som to napisal zrozumitelne, ale strom by mal byt takto :
[id] => 1 
    [id] => 3
        [id] => 5
    [id] => 4
[id] => 2


cize vazba rodic - dieta je [parent_id] - [id]
ShiraNai7
Profil
Zde předpokládám, že ono pole s objekty se nachází v proměnné s názvem $arr. Podle potřeby si to uprav.
Výsledná hierarchie je dosažena tím, že jsou jednotlivé objekty vloženy do pole children u daného rodiče.

Funkce má ošetřen případ, kdy je v hierarchii nějaký fyzicky nemožný vztah - tj. jeden objekt je potomkem dalšího a zároveň jeho rodičem - aby nedošlo k nekonečnému zacyklení.

edit: funkce je má práce, nenechte se zmást angličtinou - jsem prostě zvyklý takhle psát :)

// definice funkce
function maketree($for, &$map)
{

	// static vars
	static $level = 0, $path = array();
	++$level;

	// prevent infinite recursion
	if(isset($path[$for])) throw new RuntimeException('Object #'.$for.' is involved in impossible relation!');
	$path[$for] = true;

	// process
	if(!isset($map[$for])) return;
	if(!isset($map[$for]->children)) $map[$for]->children = array();
	foreach($map as $it) {
		if(!isset($it) || $it->parent_id !== $for) continue;
		maketree($it->id, $map);
		$map[$for]->children[] = $it;
		unset($map[$it->id]);
	}

	// decrease level and cleanup
	--$level;
	if($level === 0) $path = array();

}

// prevest pole na pole, kde klicem je ID
$map = array();
for($i = 0; isset($arr[$i]); ++$i) $map[$arr[$i]->id] = $arr[$i];

// hlavni cyklus
foreach($map as $it)
	if($it->parent_id !== 0) maketree($it->parent_id, $map);

// vypis vysledku
print_r($map);


A výsledek je:

Array
(
    [1] => stdClass Object
        (
            [id] => 1
            [parent_id] => 0
            [text] => text1
            [children] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 3
                            [parent_id] => 1
                            [text] => text3
                            [children] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [id] => 5
                                            [parent_id] => 3
                                            [text] => text5
                                            [children] => Array
                                                (
                                                )

                                        )

                                )

                        )

                    [1] => stdClass Object
                        (
                            [id] => 4
                            [parent_id] => 1
                            [text] => text4
                            [children] => Array
                                (
                                )

                        )

                )

        )

    [2] => stdClass Object
        (
            [id] => 2
            [parent_id] => 0
            [text] => text2
        )

)
Tomas Balvan
Profil *
Super, skusim. Velmi pekne dakujem!

Vaše odpověď

Mohlo by se hodit


Prosím používejte diakritiku a interpunkci.

Ochrana proti spamu. Napište prosím číslo dvě-sta čtyřicet-sedm:

0