Re: [fw-general] create tree from arrays

2010-05-15 Thread shahrzad khorrami
Thankss Aleksey Zapparov :) it works grea! merc


Re: [fw-general] create tree from arrays

2010-05-13 Thread Ralph Schindler
There are a couple of ways to go about buiding and recursing trees in 
PHP.  Utilizing functions to do it is one route.


The more object-oriented route would be to build a tree structure around 
SPL's RecursiveIterator, and RecursiveIteratorIterator.


A component in Zend Framework that does this is Zend_Navigation. It can 
have infinitely deep nodes.


http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Navigation.php
http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Navigation/

Another component to look at is Zend_Tool_Project_Profile.  This creates 
a Tree based off an xml file that can then be searched and iterated.


http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Tool/Project/

It might take some time to wrap your head around RI and RII, but in the 
end you might find its worth it to have each node as an object.  I makes 
your tree code more maintainable.


-ralph

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..


main array:

Array
(
[0] = Array
(
[nid] = 1
[parentID] = 0
[text] = Dashboard
[cls] = x-btn-icon
[icon] = lib/extjs/resources/images/default/icon/Dashboard.png
[singleClickExpand] = 1
[leaf] = 0
[id] = Dashboard
)

[1] = Array
(
[nid] = 2
[parentID] = 1
[text] = Dashboard
[cls] = firstExpanded
[icon] = lib/extjs/resources/images/default/tree/s.gif
[singleClickExpand] = 1
[leaf] = 0
[id] =
)

[2] = Array
(
[nid] = 3
[parentID] = 2
[text] = Dashboard
[cls] = x-btn-icon
[icon] = lib/extjs/resources/images/default/tree/s.gif
[singleClickExpand] = 1
[leaf] = 1
[id] = dashboard
)
...


-- The array I want to create: 

[0] = Array
(
[nid] = 1
[parentID] = 0
[text] = Dashboard
[cls] = x-btn-icon
[icon] = lib/extjs/resources/images/default/icon/Dashboard.png
[singleClickExpand] = 1
[leaf] = 0
[id] = Dashboard
[children] = Array(
 [0] = Array
(
[nid] = 2
[parentID] = 1
[text] = Dashboard
[cls] = firstExpanded
[icon] = 
lib/extjs/resources/images/default/tree/s.gif

[singleClickExpand] = 1
[leaf] = 0
[id] =
[children] = Array(

[0] = Array
(
  [nid] = 3
  [parentID] = 2
  [text] = Dashboard
  [cls] = x-btn-icon
  [icon] = 
lib/extjs/resources/images/default/tree/s.gif
  
[singleClickExpand] = 1

  [leaf] = 1
  [id] = dashboard
  )
)
)
)
)

.
wow!
it means that by nid and parentID, I'll notice where I must add children 
item to array...


Thanks,
Shahrzad





Re: [fw-general] create tree from arrays

2010-05-13 Thread Daniel Latter
To clarify more on the link I posted, in contains a view helper that takes a
flat array (with 'parentID' element), and outputs a tree. When I said
function I meant on object method.

Thanks

On 13 May 2010 14:56, Ralph Schindler ralph.schind...@zend.com wrote:

 There are a couple of ways to go about buiding and recursing trees in
 PHP.  Utilizing functions to do it is one route.

 The more object-oriented route would be to build a tree structure around
 SPL's RecursiveIterator, and RecursiveIteratorIterator.

 A component in Zend Framework that does this is Zend_Navigation. It can
 have infinitely deep nodes.


 http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Navigation.php

 http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Navigation/

 Another component to look at is Zend_Tool_Project_Profile.  This creates a
 Tree based off an xml file that can then be searched and iterated.


 http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Tool/Project/

 It might take some time to wrap your head around RI and RII, but in the end
 you might find its worth it to have each node as an object.  I makes your
 tree code more maintainable.

 -ralph


 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..

 main array:

 Array
 (
[0] = Array
(
[nid] = 1
[parentID] = 0
[text] = Dashboard
[cls] = x-btn-icon
[icon] = lib/extjs/resources/images/default/icon/Dashboard.png
[singleClickExpand] = 1
[leaf] = 0
[id] = Dashboard
)

[1] = Array
(
[nid] = 2
[parentID] = 1
[text] = Dashboard
[cls] = firstExpanded
[icon] = lib/extjs/resources/images/default/tree/s.gif
[singleClickExpand] = 1
[leaf] = 0
[id] =
)

[2] = Array
(
[nid] = 3
[parentID] = 2
[text] = Dashboard
[cls] = x-btn-icon
[icon] = lib/extjs/resources/images/default/tree/s.gif
[singleClickExpand] = 1
[leaf] = 1
[id] = dashboard
)
 ...


 -- The array I want to create: 

[0] = Array
(
[nid] = 1
[parentID] = 0
[text] = Dashboard
[cls] = x-btn-icon
[icon] = lib/extjs/resources/images/default/icon/Dashboard.png
[singleClickExpand] = 1
[leaf] = 0
[id] = Dashboard
[children] = Array(
 [0] = Array
(
[nid] = 2
[parentID] = 1
[text] = Dashboard
[cls] = firstExpanded
[icon] =
 lib/extjs/resources/images/default/tree/s.gif
[singleClickExpand] = 1
[leaf] = 0
[id] =
[children] = Array(
[0] = Array
(
  [nid] = 3
  [parentID] = 2
  [text] = Dashboard
  [cls] = x-btn-icon
  [icon] =
 lib/extjs/resources/images/default/tree/s.gif
  [singleClickExpand]
 = 1
  [leaf] = 1
  [id] = dashboard
  )
)
)
)
)

 .
 wow!
 it means that by nid and parentID, I'll notice where I must add children
 item to array...

 Thanks,
 Shahrzad






Re: [fw-general] create tree from arrays

2010-05-13 Thread Aleksey Zapparov
Hello,

Here's primitive variant of function you might looking for.
It's very simplific and non-flexible, but it gives an idea:

/**
 * @param   array $arr Array to process
 * @param   integer $id (optional) Base parent ID
 * @return  array
 */
function mktree_array($arr, $id = 0)
{
$result = array();
foreach ($arr as $a) {
if ($id == $a['parentID']) {
$a['children']  = mktree_array($arr, $a['nid']);
$result[] = $a;
}
}
return $result;
}

You can see it's example usage at gihub:
http://gist.github.com/gists/400346


2010/5/13 shahrzad khorrami shahrzad.khorr...@gmail.com:

 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..

 main array:

 Array
 (
     [0] = Array
     (
     [nid] = 1
     [parentID] = 0
     [text] = Dashboard
     [cls] = x-btn-icon
     [icon] = lib/extjs/resources/images/default/icon/Dashboard.png
     [singleClickExpand] = 1
     [leaf] = 0
     [id] = Dashboard
     )

     [1] = Array
     (
     [nid] = 2
     [parentID] = 1
     [text] = Dashboard
     [cls] = firstExpanded
     [icon] = lib/extjs/resources/images/default/tree/s.gif
     [singleClickExpand] = 1
     [leaf] = 0
     [id] =
     )

     [2] = Array
     (
     [nid] = 3
     [parentID] = 2
     [text] = Dashboard
     [cls] = x-btn-icon
     [icon] = lib/extjs/resources/images/default/tree/s.gif
     [singleClickExpand] = 1
     [leaf] = 1
     [id] = dashboard
     )
 ...


 -- The array I want to create: 

     [0] = Array
     (
     [nid] = 1
     [parentID] = 0
     [text] = Dashboard
     [cls] = x-btn-icon
     [icon] = lib/extjs/resources/images/default/icon/Dashboard.png
     [singleClickExpand] = 1
     [leaf] = 0
     [id] = Dashboard
             [children] = Array(
                          [0] = Array
                                 (
                             [nid] = 2
                         [parentID] = 1
                             [text] = Dashboard
                     [cls] = firstExpanded
                             [icon] =
 lib/extjs/resources/images/default/tree/s.gif
                             [singleClickExpand] = 1
                         [leaf] = 0
                             [id] =
                                     [children] = Array(
                                             [0] = Array
                                             (
                                                   [nid] = 3
                                               [parentID] = 2
                                                   [text] = Dashboard
                                               [cls] = x-btn-icon
                                               [icon] =
 lib/extjs/resources/images/default/tree/s.gif
                                               [singleClickExpand] =
 1
                                               [leaf] = 1
                                               [id] = dashboard
                                               )
                                             )
                                 )
                         )
     )

 .
 wow!
 it means that by nid and parentID, I'll notice where I must add children
 item to array...

 Thanks,
 Shahrzad







-- 
Sincerely yours,
Aleksey V. Zapparov A.K.A. ixti
FSF Member #7118
Mobile Phone: +34 617 179 344
Homepage: http://www.ixti.ru
JID: zappa...@jabber.ru

*Origin: Happy Hacking!