I have a problem with my logic in the function.I'm trying to nest array X times so that it can look like a tree with children.I nested it with the first child but when i go deep i cant find solution to remove them from the Main array and add them deep.The "parent" key is the ID of the parent element that needs to have child.Any help appreciate.
function array_search_multidim($array, $column, $key){
return (array_search($key, array_column($array, $column)));
}
public function get_all() {
$full_menu = $this->Site_model->get_all_menu();
usort($full_menu, function($a, $b){
return strcmp($a->menu_order, $b->menu_order);
});
foreach($full_menu as $fm) {
$mega_menu[] = array(
'id' => $fm->id,
'text' => $fm->title,
'href' => $fm->link,
'icon' => $fm->icon,
'target' => $fm->target,
'title' => $fm->name,
'order' => $fm->menu_order,
'parent' => $fm->parent
);
if($fm->parent != 0) {
$child_menu[] = array(
'id' => $fm->id,
'text' => $fm->title,
'href' => $fm->link,
'icon' => $fm->icon,
'target' => $fm->target,
'title' => $fm->name,
'order' => $fm->menu_order,
'parent' => $fm->parent
);
}
}
foreach($child_menu as $cm) {
$mega_menu[$this->array_search_multidim($mega_menu,'id',$cm['parent'])]['children'][] = array(
'id' => $cm['id'],
'text' => $cm['text'],
'href' => $cm['href'],
'icon' => $cm['icon'],
'target' => $cm['target'],
'title' => $cm['title'],
'order' => $cm['order'],
'parent' => $cm['parent']
);
}
echo '<pre>';
print_r($mega_menu);
echo '</pre>';
}
For now i recieve array like that
Array
(
[0] => Array
(
[id] => 1
[text] => Начало
[href] => http://localhost/roni/#top
[icon] => fas fa-home
[target] => _self
[title] => Начало
[order] => 1
[parent] => 0
)
[1] => Array
(
[id] => 4
[text] => Споделен хостинг
[href] => http://localhost/roni/#shared
[icon] => fas fa-home
[target] => _blank
[title] => shared
[order] => 1
[parent] => 3
[children] => Array
(
[0] => Array
(
[id] => 5
[text] => Софтуер
[href] => http://localhost/roni/#software
[icon] => fas fa-code
[target] => _self
[title] => software
[order] => 2
[parent] => 4
)
)
)
[2] => Array
(
[id] => 2
[text] => Интернет
[href] => http://localhost/roni/#internet2
[icon] => fas fa-wifi
[target] => _top
[title] => Интернет
[order] => 2
[parent] => 0
)
[3] => Array
(
[id] => 5
[text] => Софтуер
[href] => http://localhost/roni/#software
[icon] => fas fa-code
[target] => _self
[title] => software
[order] => 2
[parent] => 4
)
[4] => Array
(
[id] => 3
[text] => Хостинг
[href] => http://localhost/roni/#hosting
[icon] => fas fa-home
[target] => _self
[title] => hosting
[order] => 3
[parent] => 0
[children] => Array
(
[0] => Array
(
[id] => 4
[text] => Споделен хостинг
[href] => http://localhost/roni/#shared
[icon] => fas fa-home
[target] => _blank
[title] => shared
[order] => 1
[parent] => 3
)
)
)
[5] => Array
(
[id] => 6
[text] => Сервиз
[href] => http://localhost/roni/#service
[icon] => fas fa-wrench
[target] => _self
[title] => service
[order] => 5
[parent] => 0
)
[6] => Array
(
[id] => 7
[text] => Контакти
[href] => http://localhost/#contacts
[icon] => fas fa-address-book
[target] => _self
[title] => contacts
[order] => 6
[parent] => 0
)
)
I've kept the initial data as objects because I wanted to use array_walk_recursive() to find the point in which I want to add the nodes. But the basic logic is to look for any nodes that have a parent node, then search all of the leaf nodes to find if that is the node which is the parent. If it is then just add the node...
usort($full_menu, function($a, $b){
return strcmp($a->menu_order, $b->menu_order);
});
foreach ($full_menu as $key=>$menu ) {
if ( $menu->parent != 0 ) {
array_walk_recursive($full_menu, function (&$data) use ($menu) {
if ( $data->id == $menu->parent) {
$data->children[] = $menu;
}
});
}
}
function translate ( &$menu ) {
foreach ( $menu as &$menuItem ) {
$out = array(
'id' => $menuItem->id,
'text' => $menuItem->title,
'href' => $menuItem->link,
'icon' => $menuItem->icon,
'target' => $menuItem->target,
'title' => $menuItem->name,
'order' => $menuItem->menu_order,
'parent' => $menuItem->parent
);
if ( isset($menuItem->children)) {
$out['children'] = $menuItem->children;
translate($out['children']);
}
$menuItem = $out;
}
unset ( $menuItem );
}
translate ($full_menu);
$full_menu = array_filter ( $full_menu, function ($data ) { return $data['parent'] == 0;});
print_r($full_menu);
The last parts I've added will reformat the elements to be the array format you wanted and then it filters out the root level menu to remove any items that have been moved.
In the first foreach in get_all function you add element of $full_menu arrach to $mega_menu every time, event if $fm->parent != 0 (which means every child element is saved to the 1st level of array). Next you handle childs. You must save (at the 1st dimention on array) only those elements with $fm->parent == 0. You can do it by changing a condition in get_all.
function array_search_multidim($array, $column, $key){
return (array_search($key, array_column($array, $column)));
}
public function get_all() {
$full_menu = $this->Site_model->get_all_menu();
usort($full_menu, function($a, $b){
return strcmp($a->menu_order, $b->menu_order);
});
foreach($full_menu as $fm) {
if($fm->parent == 0) {
$mega_menu[] = array(
'id' => $fm->id,
'text' => $fm->title,
'href' => $fm->link,
'icon' => $fm->icon,
'target' => $fm->target,
'title' => $fm->name,
'order' => $fm->menu_order,
'parent' => $fm->parent
);
} else {
$child_menu[] = array(
'id' => $fm->id,
'text' => $fm->title,
'href' => $fm->link,
'icon' => $fm->icon,
'target' => $fm->target,
'title' => $fm->name,
'order' => $fm->menu_order,
'parent' => $fm->parent
);
}
}
foreach($child_menu as $cm) {
$mega_menu[$this->array_search_multidim($mega_menu,'id',$cm['parent'])]['children'][] = array(
'id' => $cm['id'],
'text' => $cm['text'],
'href' => $cm['href'],
'icon' => $cm['icon'],
'target' => $cm['target'],
'title' => $cm['title'],
'order' => $cm['order'],
'parent' => $cm['parent']
);
}
echo '<pre>';
print_r($mega_menu);
echo '</pre>';
}
If you want to remove an element from an array you can use unset.
For example
unset($mega_menu[4][0]);
Related
I'm losing my hair on this one... I have an array structure that print_r's like this (I've hidden unnecessary fields):
[0] => Array
(
[id] => 14
[name] => Foo Directory
)
[1] => Array
(
[id] => 16
[name] => Bar Project
[parent] => Array
(
[id] => 14
[name] => Foo Directory
)
)
[2] => Array
(
[id] => 20
[name] => Baz Project
[parent] => Array
(
[id] => 16
[name] => Bar Project
)
)
[3] => Array
(
[id] => 10
[name] => Qux Project
[parent] => Array
(
[id] => 16
[name] => Bar Project
)
And I need it to be nested like this:
[0] => Array
(
[id] => 14
[name] => Foo Directory
[children] => Array
(
[id] => 16
[name] => Bar Project
[children] => Array
(
[id] => 20
[name] => Baz Project
)
(
[id] => 10
[name] => Qux Project
)
)
)
What I've tried so far
$projTree = array();
foreach ($projects as $project) {
if (isset($project['parent']))
array_push($projTree['children'], $project);
$projTree['id'] = $project['parent']['id'];
}
But that overwrites the previous inserted element. I also tried to walk it recursively, but couldn't figure out the correct callback for that, since it only operates on the leafs of the tree and I need to fully walk it.
How can I do this? Thanks!
Please try like below:-
<?php
$array = Array(
'0' => Array
(
'id' => 14,
'name' => 'Foo Directory'
),
'1' => Array
(
'id' => 16,
'name' => 'Bar Project',
'parent' => Array
(
'id' => 14,
'name' => 'Foo Directory'
)
),
'2' => Array
(
'id' => 20,
'name' => 'Baz Project',
'parent' => Array
(
'id' => 16,
'name' => 'Bar Project'
)
),
'3' => Array
(
'id' => 10,
'name' => 'Qux Project',
'parent' => Array
(
'id' => 16,
'name' => 'Bar Project'
)
)
);
$new_array = array();
$i = 0;
$j = 0;
foreach ($array as $key=>$val){
if(array_key_exists('parent',$val)){
foreach($new_array as $key1=>$val1){
if($val['parent']['name'] === $val1['name']){
$new_array[$key1]['children'][$i]['id'] = $val['id'];
$new_array[$key1]['children'][$i]['name'] = $val['name'];
}else{
foreach ($val1['children'] as $key3=>$val3){
if($val['parent']['name'] === $val3['name']){
$new_array[$key1]['children'][$key3]['children'][$j]['id'] = $val['id'];
$new_array[$key1]['children'][$key3]['children'][$j]['name'] = $val['name'];
}
$j++;
}
$i++;
}
}
}else{
$new_array[$key] = $val;
}
}
echo "<pre/>";print_r($new_array);
Output:- https://eval.in/419591
I have a theme in Wordpress which use a code to show products of Woocommerce. However, I want to use the same function for showing also pages with a certain template. Here is what I have done until now:
<div>
<?php
$args = array(
'post_type' => 'page',
'meta_query' => array(
array(
'key' => '_wp_page_template',
'value' => 'room-template.php'
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div>
<div>
<?php if(has_post_thumbnail()) { the_post_thumbnail(); }?>
<div>
<div>
</div>
</div>
</div>
<div>Buy Now</div>
</div>
<?php endwhile;
} else {
echo __( 'No products found');
}
wp_reset_postdata();
?>
</div>
Whereas, I have 2 pages with this template, the result is "No products found". P.S. The divs are there because I use them for CSS classes. I've just removed the classed to make it easier to read.
Here is the output of the WP_Query:
WP_Query Object ( [query] => Array ( [post_type] => page [post_status] => publish [meta_query] => Array ( [0] => Array ( [key] => _wp_page_template [value] => room-template.php )))[query_vars] => Array ( [post_type] => page [post_status] => publish [meta_query] => Array ( [0] => Array ( [key] => _wp_page_template [value] => room-template.php ))[error] => [m] => [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [static] => [pagename] => [page_id] => 0 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [category_name] => [tag] => [cat] => [tag_id] => [author] => [author_name] => [feed] => [tb] => [paged] => 0 [comments_popup] => [meta_key] => [meta_value] => [preview] => [s] => [sentence] => [fields] => [menu_order] => [category__in] => Array ( )[category__not_in] => Array ( )[category__and] => Array ( )[post__in] => Array ( )[post__not_in] => Array ( )[tag__in] => Array ( )[tag__not_in] => Array ( )[tag__and] => Array ( )[tag_slug__in] => Array ( )[tag_slug__and] => Array ( )[post_parent__in] => Array ( )[post_parent__not_in] => Array ( )[author__in] => Array ( )[author__not_in] => Array ( )[suppress_filters] => [ignore_sticky_posts] => [cache_results] => 1 [update_post_term_cache] => 1 [update_post_meta_cache] => 1 [posts_per_page] => 10 [nopaging] => [comments_per_page] => 50 [no_found_rows] => [order] => DESC )[tax_query] => WP_Tax_Query Object ( [queries] => Array ( )[relation] => AND )[meta_query] => WP_Meta_Query Object ( [queries] => Array ( [0] => Array ( [key] => _wp_page_template [value] => room-template.php ))[relation] => AND )[date_query] => [request] => SELECT SQL_CALC_FOUND_ROWS exitn_posts.ID FROM exitn_posts INNER JOIN exitn_postmeta ON (exitn_posts.ID = exitn_postmeta.post_id) WHERE 1=1 AND exitn_posts.post_type = 'page' AND ((exitn_posts.post_status = 'publish')) AND ( (exitn_postmeta.meta_key = '_wp_page_template' AND CAST(exitn_postmeta.meta_value AS CHAR) = 'room-template.php') ) GROUP BY exitn_posts.ID ORDER BY exitn_posts.post_date DESC LIMIT 0, 10 [posts] => Array ( )[post_count] => 0 [current_post] => -1 [in_the_loop] => [comment_count] => 0 [current_comment] => -1 [found_posts] => 0 [max_num_pages] => 0 [max_num_comment_pages] => 0 [is_single] => [is_preview] => [is_page] => [is_archive] => [is_date] => [is_year] => [is_month] => [is_day] => [is_time] => [is_author] => [is_category] => [is_tag] => [is_tax] => [is_search] => [is_feed] => [is_comment_feed] => [is_trackback] => [is_home] => 1 [is_404] => [is_comments_popup] => [is_paged] => [is_admin] => [is_attachment] => [is_singular] => [is_robots] => [is_posts_page] => [is_post_type_archive] => [query_vars_hash:WP_Query:private] => 436de9012210c4e9c717fd5edf527108 [query_vars_changed:WP_Query:private] => 1 [thumbnails_cached] => [stopwords:WP_Query:private] => )
You want to use get_template_part to print a specific template.
https://developer.wordpress.org/reference/functions/get_template_part/
If you want to use a specific function() within a template file then you might want to move this function to a common function library or your functions.php file.
-- EDIT --
SELECT SQL_CALC_FOUND_ROWS exitn_posts.ID
FROM exitn_posts INNER JOIN exitn_postmeta ON (exitn_posts.ID = exitn_postmeta.post_id)
WHERE 1=1 AND exitn_posts.post_type = 'page'
AND ((exitn_posts.post_status = 'publish'))
AND ( (exitn_postmeta.meta_key = '_wp_page_template' AND CAST(exitn_postmeta.meta_value AS CHAR) = 'room-template.php') )
GROUP BY exitn_posts.ID ORDER BY exitn_posts.post_date DESC LIMIT 0, 10
If executing this on the Database doesn't bring up the data it means either it doesn't exist or something within is missing. Its hard to tell this from my hand. I have pretty much the same pattern in one of my personal query and its Data is returned without any issues.
Example :
$args = array(
'post_type' => explode( ',', $post_type ),
'tag' => $tag,
'p' => $id,
'category_name' => $category,
'posts_per_page' => $posts_per_page,
'order' => $order,
'orderby' => 'meta_value',
'meta_key' => '_my_named_key',
'meta_query' => array(
array(
'key' => '_my_named_key'
)
),
'no_found_rows' => 1
);
-- Working Edit --
Make sure the value is a existing in the Database by looking it via something like phpMyAdmin or making a SQL Query onto it. This case was solved in chat full value had an unknown prefix page-templates/.
Hello I have this multi dimensional array which needs to be inserted to the database table.
so far I have come up with this solution which don't work fully. I have a form that support dynamic fields many dynamic fields. I want to save those dynamically created fields using this function. First I had been able to create a loop to format the data.
I used this loop to format the data
$url = str_replace(' ','-',strtolower($this->input->post('title')));
$data = ['business' => [
'title' => $this->input->post('title'),
'URL' => $url,
'category' => $this->input->post('category'),
'region' => $this->input->post('region')
],
'description_' => [
'text' => $this->input->post('description'),
],
'logo_' => [
'blob' => $this->input->post('logo'),
]];
$nyingi = [ 'location_' => ['text' => 'loc_txt','priority' => 'loc_order'],
'photo_' => ['path' => 'p_txt'],
'video_' => ['path' => 'v_txt'],
'product_' => ['p_id' => 'pt_q', 'text' => 'pt_txt', 'expire' => 'pt_xpr', 'title' => 'pt_tt'],
'service_' => ['p_id' => 'st_q', 'text' => 'st_txt', 'expire' => 'st_xpr', 'title' => 'st_tt'],
'hours_' => ['time1' => 't1', 'time2' => 't2', 'type' => 'tt'],
'map_' => ['text' => 'm_txt']
];
foreach($nyingi as $ins => $vl){
foreach($vl as $fld => $box){
$uwazi = $this->input->post($box);
if(is_array($uwazi) && 1<count($uwazi)){
foreach($uwazi as $bb){
$one[$ins][$fld][] = $bb;
}
} elseif(is_array($uwazi) && 1==count($uwazi)) {
print_r($uwazi);
}
}
}
for($g=0;$g<count($one);$g++){
for($h=0;$h<count($one[$g]);$h++){
for($z=0;$z<count($one[$g][$h]);$z++){
//if(array_key_exists($one[$g][$h+1], $one)){
print $one[$g][$h][$z+1];
//}
}
}
}
$data = array_merge($data, $one);
The output of the above code
[location_] => Array
(
[text] => Array
(
[0] => Lemara Main Office
[1] => Themi branch
[2] => Sinoni branch
)
[priority] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
)
[photo_] => Array
(
[path] => Array
(
[0] => lemaraphoto.png
[1] => themiphoto.png
[2] => sinoniphoto.png
)
)
[video_] => Array
(
[path] => Array
(
[0] => lemaravideo.mp4
[1] => themivideo.mp4
[2] => sinonivideo.mp4
)
)
[product_] => Array
(
[p_id] => Array
(
[0] => product photo
[1] => product 3 photo
[2] => Product 2 photo
)
[text] => Array
(
[0] => product desc
[1] => product 3 desc
[2] => product 2 desc
)
[expire] => Array
(
[0] => product expire
[1] => product 3 expire
[2] => Product 2 expire
)
[title] => Array
(
[0] => product
[1] => Product 3
[2] => Product 2
)
)
[service_] => Array
(
[p_id] => Array
(
[0] => Service 2 photo
[1] => service 3 photo
[2] => service photo
)
[text] => Array
(
[0] => service 2 desc
[1] => service 3 desc
[2] => service desc
)
[expire] => Array
(
[0] => Service 2 expire
[1] => service 3 expire
[2] => service expire
)
[title] => Array
(
[0] => Service 2
[1] => service 3
[2] => service
)
)
)
I have this add_bz function
function add_bz($data){
$this->db->trans_start(); $er=0;
foreach($data as $table => $sql){
if($table==="business"){
$this->db->insert($table, $sql);
$id = $this->db->insert_id();
} else {
array_merge($sql, array('idd' => $id));
$this->db->insert($table, $sql);}
}
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE){print "Transaction Fails";return FALSE;}
return TRUE;
}
That function allows me to insert the data in this format only.
[[business] => Array
(
[title] => Email Marketing
[URL] => email-marketing
[category] => 3
[region] => 2
)
[description_] => Array
(
[text] => Some desc
)
[logo_] => Array
(
[blob] => mainlogo.png
)
]
as business, decription_, logo_ are db tables where title, URL, category, region, text, blob are the db columns to the corresponding tables and the rest is data that goes to those columns.
How I want it to works. For example in location_
I want to the array to change into my working format or something else that will work like this
location_ => ['text' => [[0] => 'Lemara Main Office'],
'priority' => [[0] => '1']
]
location_ => ['text' => [[1] => 'Themi Office'],
'priority' => [[1] => '2']
]
location_ => ['text' => [[2] => 'Sinoni Office'],
'priority' => [[2] => '3']
]
or like this
location_ => [0 => [['text' => 'Lemara Main Office'],
['priority' => '1']
],
1 => [['text' => 'Themi Main Office'],
['priority' => '2']
],
2 => [['text' => 'Sinoni Main Office'],
['priority' => '3']
]
],
And also support many columns not just the two.
Any help is deeply appreciated as I have been struggling for days to make this work. Thanks in advance.
I would suggest you convert your array to JSON String then insert it to DB as text.
JSON would handle your multidimentional data so well,
To convert your array to JSON : http://php.net/manual/en/function.json-encode.php
To convert your JSON to Array : http://php.net/manual/en/function.json-decode.php
i've a similar array:
Array
(
[0] => Array
(
[id] => 1
[name] => Mario
[type] => Doctor
[operations] => brain
[balance] => 3.00
)
[1] => Array
(
[id] => 3
[name] => Luca
[type] => Doctor
[operations] => hearth
[balance] => 6.00
)
[2] => Array
(
[id] => 3
[name] => Luca
[type] => Doctor
[operations] => hands
[balance] => 4.00
)
[3] => Array
(
[id] => 3
[name] => Luca
[type] => Doctor
[operations] => foots
[balance] => 1.00
)
)
I must to merge it for id, so obtain only 2 elements for array (0 and 1) and Luca (id 3) must be unified for operations, in a new array, similar to this, so in future print more clearly operations unified and not divided.
[...]
)
[1] => Array
(
[id] => 3
[name] => luca
[type] => doctore
[operations] => Array (6.00 hearts,
4.00 hands,
1.00 foots)
)
I can not figure how solve my trouble... Someone could help me please? Thank you very much to all!
<?php
$input = array(
array('id' => 1, 'name' => 'Mario', 'type' => 'Doctor', 'operations' => 'brain', 'balance' => 3.00),
array('id' => 3, 'name' => 'Luca', 'type' => 'Doctor', 'operations' => 'hearth', 'balance' => 6.00),
array('id' => 3, 'name' => 'Luca', 'type' => 'Doctor', 'operations' => 'hands', 'balance' => 4.00),
array('id' => 3, 'name' => 'Luca', 'type' => 'Doctor', 'operations' => 'foots', 'balance' => 1.00),
);
$output = array();
foreach ($input as $person)
{
// Add the person to the output, if needed.
if (array_key_exists($person['id'], $output) === false)
{
$output[$person['id']] =
array(
'id' => $person['id'],
'name' => $person['name'],
'type' => $person['type'],
'operations' => array(),
);
}
// Add the operation to the array of operations.
$output[$person['id']]['operations'][] =
array(
'type' => $person['operations'],
'balance' => $person['balance'],
));
}
foreach ($output as $person)
{
if (count($person['operations']) === 1)
{
// If the array contains only 1 item, pull it out of the array.
$output[$person['id']]['operations'] = $person['operations'][0];
}
}
print_r($output);
I have an array like below:
$cats = array();
$cats[1] = array('id' => 1,'parent' => 0, 'title' => 'Tutorials');
$cats[2] = array('id' => 2,'parent' => 1, 'title' => 'PHP');
$cats[3] = array('id' => 3,'parent' => 2, 'title' => 'OOP');
$cats[4] = array('id' => 4,'parent' => 2, 'title' => 'Tips');
$cats[5] = array('id' => 5,'parent' => 1, 'title' => 'JavaScript');
$cats[6] = array('id' => 6,'parent' => 5, 'title' => 'Basics');
$cats[7] = array('id' => 7,'parent' => 5, 'title' => 'Frameworks');
$cats[8] = array('id' => 8,'parent' => 7, 'title' => 'jQuery');
$cats[9] = array('id' => 9,'parent' => 7, 'title' => 'MooTools');
$cats[10] = array('id' => 10,'parent' => 0, 'title' => 'News');
$cats[11] = array('id' => 11,'parent' => 10, 'title' => 'PHP');
$cats[12] = array('id' => 12,'parent' => 10, 'title' => 'Wordpress');
$cats[13] = array('id' => 13,'parent' => 0, 'title' => 'New');
and want to show it in a PHP function like this that call an function and give this array,
for example
$id=1;
builder_tree($id);
and give me bellow array ,please help me
Array
(
[0] => Array
(
[id] => 1
[parent] => 0
[title] => Tutorials
[children] => Array
(
[0] => Array
(
[id] => 2
[parent] => 1
[title] => PHP
[children] => Array
(
)
)
[1] => Array
(
[id] => 3
[parent] => 1
[title] => PHP
[children] => Array
(
)
)
[2] => Array
(
[id] => 5
[parent] => 1
[title] => JavaScript
[children] => Array
(
[0] => Array
(
[id] => 6
[parent] => 5
[title] => PHP
[children] => Array
(
)
)
[1] => Array
(
[id] => 7
[parent] => 5
[title] => PHP
[children] => Array
(
[0] => Array
(
[id] => 8
[parent] => 7
[title] => jQuery
[children] => Array
(
)
)
[1] => Array
(
[id] => 9
[parent] => 7
[title] => MooTools
[children] => Array
(
)
)
)
)
)
)
)
)
I realize this is an old question, but I was looking for something similar to what the original poster needed and came up with the following.
Hope this helps any future Googlers.
The build_tree_array() basically takes what the original poster posted above and coverts it to a hierarchical array.
function folder_has_children($rows, $id) {
foreach ($rows as $row) {
if ($row['child_of'] == $id)
return true;
}
return false;
}
function build_tree_array(&$rows, $parent = 0) {
foreach ($rows as $row) {
if ($row['child_of'] == $parent) {
$result = $row;
if ($this->folder_has_children($rows, $row['id'])) {
$result['children'] = $this->build_tree_array($rows, $row['id']);
}
$out[] = $result;
}
}
return $out;
}