View Single Post
Old 09-03-2010, 03:17 PM   #6 (permalink)
Marious Barrier
Guest
 
Posts: n/a
Default Re: Multidimensional nested array from mysql table

On 09/03/2010 03:41 AM, Erwin Moller wrote:
> On 9/1/2010 5:25 PM, Marious Barrier wrote:
>> On 09/01/2010 11:15 AM, Erwin Moller wrote:
>>> On 9/1/2010 4:16 PM, Marious Barrier wrote:
>>>> On 09/01/2010 04:50 AM, Atomic wrote:
>>>>> i try to figure out myself but only what i get is first 2 levels
>>>>>
>>>>> is it possible to create array with parent/children with all "leafs"
>>>>> since i
>>>>> plan to use such array to build UL/LI list
>>>>
>>>> Do this.-
>>>>
>>>> // you can copy and paste this code as-is and test it.
>>>>
>>>> // A single query to get all the entries in a single array. $allitems
>>>> // Would end up being something like this
>>>>
>>>> $allitems = array(
>>>> 1 => array( 'id' => 1, 'name' => 'item1', 'par' => 0),
>>>> 2 => array( 'id' => 2, 'name' => 'item2', 'par' => 1),
>>>> 3 => array( 'id' => 3, 'name' => 'item3', 'par' => 0),
>>>> 4 => array( 'id' => 4, 'name' => 'item4', 'par' => 1),
>>>> 5 => array( 'id' => 5, 'name' => 'item5', 'par' => 2),
>>>> 6 => array( 'id' => 6, 'name' => 'item6', 'par' => 1),
>>>> );
>>>>
>>>> // A “while”, to link them using references.
>>>>
>>>> while (list($z, $o) = each($allitems))
>>>> if ($o['par'] && isset($allitems[$o['par']]))
>>>> $allitems[$o['par']]['child'][] =& $allitems[$z];
>>>> reset($allitems);
>>>>
>>>> // And then unset all the childs. leaving the items with par = 0
>>>>
>>>> while (list($z, $o) = each($allitems))
>>>> if ($o['par']) unset($allitems[$z]);
>>>>
>>>> // see what it results and if it is what you want.
>>>>
>>>> print_r($allitems);
>>>
>>>
>>> Nice approach Marious, but there is one small hick-up.
>>> The success of your routine depends on the order of feeded source (the
>>> $allitems array in your example).
>>>
>>> If it encounters parentid's that weren't used earlier, it will fail.
>>>
>>> Feed it this to test:
>>>
>>> $allitems = array(
>>> 1 => array( 'id' => 1, 'name' => 'item1', 'par' => 0),
>>> 2 => array( 'id' => 2, 'name' => 'item2', 'par' => 1),
>>> 3 => array( 'id' => 3, 'name' => 'item3', 'par' => 0),
>>> 4 => array( 'id' => 4, 'name' => 'item4', 'par' => 1),
>>> 5 => array( 'id' => 5, 'name' => 'item5', 'par' => 2),
>>> 6 => array( 'id' => 6, 'name' => 'item6', 'par' => 7),
>>> 7 => array( 'id' => 10, 'name' => 'item10', 'par' => 5),
>>> 8 => array( 'id' => 15, 'name' => 'item15', 'par' => 7),
>>> 9 => array( 'id' => 44, 'name' => 'item44', 'par' => 2)
>>> );

>>
>>
>> Nop nop, you did a mistake there... you just changed the 'id' key which
>> is actually used by nothing.
>>
>> 7 for instance.
>> 7 => array('id' => 10 ...
>> should be
>> 10 => array('id' => 10 ...

>
> Hi Marious,
>
> I also changed the 'par' key to 7 in that line.
> (Since 7 doesn't exist at that time.)
>
> But on second inspection I see that your script handles that just right.
> Not sure what I was thinking/doing yesterday when I tested your code,
> but it surely wasn't something intelligent. I stand corrected.
>
> I expect I was confused by the fact you used the array index instead of
> the 'id' field as coming from the database, which is a more realistic
> field to use, so I blindly assumed that you did.
>
> Regards,
> Erwin Moller
>
>
>>
>>>
>>> And that cannot always be solved with a simple ORDER BY clause in the
>>> underlying query.
>>>
>>> I had that problem once (complex nested navigation structure), and
>>> approached it in a different way.
>>> (I'll try to dig my code up, but I am not 100% sure where I left it.)

>
>


Don’t worry, the array will be made dynamically so that won’t happen for
sure.
  Reply With Quote