Re: [PHP] create tree from arrays

2010-05-17 Thread David Harkness
On Mon, May 17, 2010 at 9:13 AM, Richard Quadling wrote: > OOI, can you take a look at my first response. Is this the sort of > thing you were talking about? > Essentially, yes. For the temporary array I would map nid => element instead of INDEX => nid. Your $Relationships array is basically the

Re: [PHP] create tree from arrays

2010-05-17 Thread Richard Quadling
On 17 May 2010 16:41, David Harkness wrote: > Shahrzad, > > While your algorithm is correct it is very inefficient. The full array is > scanned for every element in the array. If the array contains 1,000 > elements, 1,000,000 comparisons will be performed and mktree_array() will be > called 1,000

Re: [PHP] create tree from arrays

2010-05-17 Thread David Harkness
Shahrzad, While your algorithm is correct it is very inefficient. The full array is scanned for every element in the array. If the array contains 1,000 elements, 1,000,000 comparisons will be performed and mktree_array() will be called 1,000 times. This is known as order n squared: O(n^2) and is a

Re: [PHP] create tree from arrays

2010-05-15 Thread shahrzad khorrami
Thanks to all, it works now: function mktree_array(&$arr, $id = 0) { $result = array(); foreach ($arr as $a) { if ($id == $a['parentID']) { $a['children'] = $this->mktree_array($arr, $a['nid']); $result[] = $a; } }

Re: [PHP] create tree from arrays

2010-05-13 Thread Richard Quadling
On 13 May 2010 11:42, shahrzad khorrami wrote: > > Dear Richard, > we have more than one parentID  with value 0 in main array, your > code is so good but show me just the children of first parentID 0. > > :) > > Thanks, > Shahrzad > print_r($Data[0]['children']); maybe. -- - Richa

Re: [PHP] create tree from arrays

2010-05-13 Thread shahrzad khorrami
Dear Richard, we have more than one parentID with value 0 in main array, your code is so good but show me just the children of first parentID 0. :) Thanks, Shahrzad

Re: [PHP] create tree from arrays

2010-05-13 Thread shahrzad khorrami
Thanks Rechard :) I'm testing it... merccc

Re: [PHP] create tree from arrays

2010-05-13 Thread Richard Quadling
On 13 May 2010 11:09, Richard Quadling wrote: > On 13 May 2010 09:51, shahrzad khorrami wrote: >> hi all, >> >> I want to create an array from another array to create json file in my >> format and pass it to a js libraryok. >> I just know that I have to use recursive function... but how? it's

Re: [PHP] create tree from arrays

2010-05-13 Thread Richard Quadling
On 13 May 2010 09:51, shahrzad khorrami wrote: > hi all, > > I want to create an array from another array to create json file in my > format and pass it to a js libraryok. > I just know that I have to use recursive function... but how? it's hard for > me to create the new array.. Assuming tha