I select all the category elements starting from parent id= 0 and put the items which their parent id value equals to parent node's id.
public function get_categories($table_name, $parent_id = 0)
{
$this->db->where('`parent`', $parent);
$parent = $this->db->get($table_name);
$categories = $parent->result();
$i = 0;
foreach ($categories as $p_cat) {
$categories[$i]->sub = $this->sub_categories($table_name, $p_cat->id);
$i++;
}
return $categories;
}
public function sub_categories($table_name, $id)
{
$this->db->where('`parent`', $id);
$child = $this->db->get($table_name);
$categories = $child->result();
$i = 0;
foreach ($categories as $p_cat) {
$categories[$i]->sub = $this->sub_categories($table_name, $p_cat->id);
$i++;
}
return $categories;
}
This is the output of my_categories table. $categories content is as follows:
Array (
[0] => stdClass Object (
[id] => 1
[name] => XMLELEMENT10
[parent] => 0
[sub] => Array (
[0] => stdClass Object (
[id] => 13
[name] => XMLELEMENT113
[parent] => 1
[sub] => Array ( )
)
[1] => stdClass Object (
[id] => 14
[name] => XMLELEMENT114
[parent] => 1
[sub] => Array ( )
)
[2] => stdClass Object (
[id] => 15
[name] => XMLELEMENT115
[parent] => 1
[sub] => Array ( ) )
[3] => stdClass Object (
[id] => 16
[name] => XMLELEMENT116
[parent] => 1
[sub] => Array ( )
)
)
)
[1] => stdClass Object (
[id] => 2
[name] => XMLELEMENT20
[parent] => 0
[sub] => Array (
[0] => stdClass Object (
[id] => 5
[name] => XMLELEMENT52
[parent] => 2
[sub] => Array ( )
)
)
)
[2] => stdClass Object (
[id] => 3
[name] => XMLELEMENT30
[parent] => 0
[sub] => Array (
[0] => stdClass Object (
[id] => 6
[name] => XMLELEMENT63
[parent] => 3
[sub] => Array ( )
)
[1] => stdClass Object (
[id] => 7
[name] => XMLELEMENT73
[parent] => 3
[sub] => Array ( )
)
[2] => stdClass Object (
[id] => 8
[name] => XMLELEMENT83
[parent] => 3
[sub] => Array ( )
)
[3] => stdClass Object (
[id] => 9
[name] => XMLELEMENT93
[parent] => 3
[sub] => Array ( )
)
)
)
[3] => stdClass Object (
[id] => 4
[name] => XMLELEMENT40
[parent] => 0
[sub] => Array (
[0] => stdClass Object (
[id] => 10
[name] => XMLELEMENT104
[parent] => 4
[sub] => Array ( )
)
[1] => stdClass Object (
[id] => 11
[name] => XMLELEMENT2017114
[parent] => 4
[sub] => Array ( )
)
[2] => stdClass Object (
[id] => 12
[name] => XMLELEMENT122
[parent] => 4
[sub] => Array ( )
)
)
)
[4] => stdClass Object (
[id] => 17
[name] => XMLELEMENT170
[parent] => 0
[sub] => Array (
[0] => stdClass Object (
[id] => 18
[name] => XMLELEMENT1817
[parent] => 17
[sub] => Array ( )
)
[1] => stdClass Object (
[id] => 19
[name] => XMLELEMENT1917
[parent] => 17
[sub] => Array ( )
)
[2] => stdClass Object (
[id] => 20
[name] => XMLELEMENT2017
[parent] => 17
[sub] => Array ( )
)
)
)
)
Question is simple but I cannot figure out. I just need to list all items like
<ul>
<li>root
<!-- if has children -->
<ul>
<li>child
<!-- if has children -->
<ul>
<li>child of child
.
.
.
</ul>
</li>
</ul>
I was unable to write that simple recursive function.
Very simple solution:
<?php
function printList(array $data){
print "<ul>\n";
foreach($data as $set){
print "<li>$set->name";
if(is_array($set->sub) && count($set->sub)){
print "\n";
printList($set->sub);
}
print "</li>\n";
}
print "</ul>\n";
}
#example data like your $categories array
$subexample = new stdClass;
$subexample->name = 'foobar';
$subexample->sub = array();
$example = new stdClass;
$example->name = 'foobar';
$example->sub = array($subexample);
$data = array($example,$example,$example);
printList($data);
Related
I have two multidimensional arrays. I'm looping through both arrays, checking for certain values & creating a new array.
$full_cats array
Array
(
[0] => Array
(
[parent_cats] => Array
(
[id] => 384
[name] => Beers & Ales
[parent] => 0
)
)
[1] => Array
(
[parent_cats] => Array
(
[id] => 387
[name] => Wines
[parent] => 0
)
)
)
$sub_cats array
Array
(
[0] => Array
(
[sub_cats] => Array
(
[id] => 386
[name] => Ales
[parent] => 384
)
)
[1] => Array
(
[sub_cats] => Array
(
[id] => 385
[name] => Beers
[parent] => 384
)
)
[2] => Array
(
[sub_cats] => Array
(
[id] => 403
[name] => Red
[parent] => 387
)
)
)
I'm looping through both arrays to check if $sub_cat['sub_cats']['parent'] value is the same as the $full_cat['parent_cats']['id'] value. If this is true, both values are added to the $master_cats array.
$master_cats = array();
foreach ($sub_cats as $sub_cat) {
foreach ($full_cats as $full_cat) {
if( $sub_cat['sub_cats']['parent'] == $full_cat['parent_cats']['id'] ){
$master_cats[] = array(
"parent_cats" => array(
$full_cat['parent_cats'],
),
"sub_cats" => array(
$sub_cat['sub_cats'],
)
);
};
};
};
The $master_cats output -
Array
(
[0] => Array
(
[parent_cats] => Array
(
[0] => Array
(
[id] => 384
[name] => Beers & Ales
[parent] => 0
)
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 386
[name] => Ales
[parent] => 384
)
)
)
[1] => Array
(
[parent_cats] => Array
(
[0] => Array
(
[id] => 384
[name] => Beers & Ales
[parent] => 0
)
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 385
[name] => Beers
[parent] => 384
)
)
)
[2] => Array
(
[parent_cats] => Array
(
[0] => Array
(
[id] => 387
[name] => Wines
[parent] => 0
)
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 403
[name] => Red
[parent] => 387
)
)
)
)
I'm having two issues with the $master_cats array.
1st problem - As you can see, index 0 & 1 have the same [parent_cats] values. I only want to add the [parent_cats] key/values if they dont already exist.
2nd Problem - the $master_cats array index 0 & 1, the sub_cats array, some values are different but both have the same [sub_cats][parent] => 384 so they belong in the same array index, eg 0.
Below is what I'm hoping to achieve with the $master_cats array from the foreach/loop above
Array
(
[0] => Array
(
[parent_cats] => Array
(
[0] => Array
(
[id] => 384 <-- Parent ID
[name] => Beers & Ales
[parent] => 0
)
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 386
[name] => Ales
[parent] => 384 <-- belongs to [parent_cats][id]
)
[1] => array
(
[id] => 385
[name] => Beers
[parent] => 384 <-- belongs to [parent_cats][id]
)
)
)
[1] => Array
(
[parent_cats] => Array
(
[0] => Array
(
[id] => 387 <-- Parent ID
[name] => Wines
[parent] => 0
)
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 403
[name] => Red
[parent] => 387 <-- belongs to [parent_cats][id]
)
)
)
)
Here's one way to go about it. Note: your array structures are a bit over-nested... but here's a solution for the given structure
$all = [];
$tmpsubs = [];
foreach ($full_cats as $parent) {
$tmp = ['parent_cats' => $parent['parent_cats'], 'sub_cats' => []];
foreach ($sub_cats as $sub) {
if ($sub['sub_cats']['parent'] == $parent['parent_cats']['id']) {
$tmp['sub_cats'][]=$sub['sub_cats'];
}
}
$all[] = $tmp;
}
print_r($all);
Example: https://3v4l.org/01oLF
Output:
Array
(
[0] => Array
(
[parent_cats] => Array
(
[id] => 384
[name] => Beers & Ales
[parent] => 0
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 386
[name] => Ales
[parent] => 384
)
[1] => Array
(
[id] => 385
[name] => Beers
[parent] => 384
)
)
)
[1] => Array
(
[parent_cats] => Array
(
[id] => 387
[name] => Wines
[parent] => 0
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 403
[name] => Red
[parent] => 387
)
)
)
)
Note: here's an example of simplifying your array structure (which would require changing the code in my answer, but ultimately might make your life a little easier)
$parent_cats= array(
array(
"id" => "384",
"name" => "Beers & Ales",
"parent" => "0"
),
array(
"id" => "387",
"name" => "Wines",
"parent" => "0"
));
Update: I have a solution - please see below for details.
I have an array where the keys are levels (in a navigation tree for example) - something like
Array
(
[0] => Array
(
[100] => Array
(
[name] => foo100
[slug] => foo100
[id] => 100
[parent] => 0
[level] => 0
)
[101] => Array
(
[name] => foo101
[slug] => foo101
[id] => 101
[parent] => 0
[level] => 0
)
)
[1] => Array
(
[200] => Array
(
[name] => foo200
[slug] => foo200
[id] => 200
[parent] => 100
[level] => 1
)
[201] => Array
(
[name] => foo201
[slug] => foo201
[id] => 201
[parent] => 101
[level] => 1
)
)
[2] => Array
(
[300] => Array
(
[name] => foo300
[slug] => foo300
[id] => 300
[parent] => 200
[level] => 2
)
[301] => Array
(
[name] => foo301
[slug] => foo301
[id] => 301
[parent] => 201
[level] => 2
)
)
[3] => Array
(
[400] => Array
(
[name] => foo400
[slug] => foo400
[id] => 400
[parent] => 300
[level] => 3
)
)
[4] => Array
(
[500] => Array
(
[name] => foo500
[slug] => foo500
[id] => 500
[parent] => 400
[level] => 4
)
)
)
I need to create an array from this which iterates from the top most level and creates an array with the key being the slug of that level - to produce the following:
Array
(
[foo500] => Array
(
[4] => Array
(
[name] => foo500
)
[3] => Array
(
[name] => foo400
)
[2] => Array
(
[name] => foo300
)
[1] => Array
(
[name] => foo200
)
[0] => Array
(
[name] => foo100
)
)
[foo400] => Array
(
[3] => Array
(
[name] => foo400
)
[2] => Array
(
[name] => foo300
)
[1] => Array
(
[name] => foo200
)
[0] => Array
(
[name] => foo100
)
)
[foo300] => Array
(
[2] => Array
(
[name] => foo300
)
[1] => Array
(
[name] => foo200
)
[0] => Array
(
[name] => foo100
)
)
[foo301] => Array
(
[2] => Array
(
[name] => foo301
)
[1] => Array
(
[name] => foo201
)
[0] => Array
(
[name] => foo101
)
)
[foo200] => Array
(
[1] => Array
(
[name] => foo200
)
[0] => Array
(
[name] => foo100
)
)
[foo201] => Array
(
[1] => Array
(
[name] => foo201
)
[0] => Array
(
[name] => foo101
)
)
[foo100] => Array
(
[0] => Array
(
[name] => foo100
)
)
[foo101] => Array
(
[0] => Array
(
[name] => foo101
)
)
)
I hope this explains the issue - struggling to get this right! Any help much appreciated!
Update - solution.
For this I removed the first level of keys to leave
Array
(
[107] => Array
(
[id] => 107
[name] => About Us
[indexID] => about
[level] => 0
[parent] => 0
)
[109] => Array
(
[id] => 109
[name] => Home
[indexID] => index
[level] => 0
[parent] => 0
)
}
etc etc
Assuming $data is the above array I went with:
foreach ($data as $k => $v) {
if ($v['parent'] == 0) {
$bc[$v['indexID']][0]['name'] = $v['name'];
$bc[$v['indexID']][0]['indexID'] = $v['indexID'];
}
else {
$nextParent = $v['parent'];
$currentIndexID = $v['indexID'];
$currentName = $v['name'];
$bc[$v['indexID']][0]['name'] = $currentName;
$bc[$v['indexID']][0]['indexID'] = $currentIndexID;
for($i=1;$i<=$level;$i++) {
foreach ($data as $a => $b) {
if ($a == $nextParent) {
$nextParent = $b['parent'];
$bc[$v['indexID']][$i]['name'] = $b['name'];
$bc[$v['indexID']][$i]['indexID'] = $b['indexID'];
}
}
}
}
}
I have the array like this:
Array
(
[28748] => stdClass Object
(
[uid] => 28748
[status] => 1
[children] => Array
(
[29163] => stdClass Object
(
[uid] => 29163
[status] => 1
)
)
)
[28708] => stdClass Object
(
[uid] => 28708
[status] => 1
[children] => Array
(
[27104] => stdClass Object
(
[uid] => 27104
[status] => 1
[children] => Array
(
[28250] => stdClass Object
(
[uid] => 28250
[status] => 1
)
)
)
[29448] => stdClass Object
(
[uid] => 29448
[status] => 1
[children] => Array
(
[28528] => stdClass Object
(
[uid] => 28528
[status] => 1
)
[28329] => stdClass Object
(
[uid] => 28329
[status] => 1
[children] => Array
(
[28533] => stdClass Object
(
[uid] => 28533
[status] => 1
)
)
[26548] => stdClass Object
(
[uid] => 26548
[status] => 1
)
)
)
)
)
)
And I want to sort the array by the count of sub array, so the no of children is greater will come first. The level of dimension is unlimited. In my case I want to become this array:
Array
(
[28708] => stdClass Object
(
[uid] => 28708
[status] => 1
[children] => Array
(
[29448] => stdClass Object
(
[uid] => 29448
[status] => 1
[children] => Array
(
[28329] => stdClass Object
(
[uid] => 28329
[status] => 1
[children] => Array
(
[28533] => stdClass Object
(
[uid] => 28533
[status] => 1
)
)
[28528] => stdClass Object
(
[uid] => 28528
[status] => 1
)
[26548] => stdClass Object
(
[uid] => 26548
[status] => 1
)
)
)
[27104] => stdClass Object
(
[uid] => 27104
[status] => 1
[children] => Array
(
[28250] => stdClass Object
(
[uid] => 28250
[status] => 1
)
)
)
)
)
[28748] => stdClass Object
(
[uid] => 28748
[status] => 1
[children] => Array
(
[29163] => stdClass Object
(
[uid] => 29163
[status] => 1
)
)
)
)
Here is the new array that i am checking:
Array
(
[27104] => stdClass Object
(
[uid] => 27104
[status] => 1
[children] => Array
(
[28250] => stdClass Object
(
[uid] => 28250
[status] => 1
[children] => Array
(
[28839] => stdClass Object
(
[uid] => 28839
[status] => 1
[children] => Array
(
[27102] => stdClass Object
(
[uid] => 27102
[status] => 1
)
)
)
)
)
[26551] => stdClass Object
(
[uid] => 26551
[status] => 1
[children] => Array
(
[25368] => stdClass Object
(
[uid] => 25368
[status] => 1
)
)
)
)
)
[28708] => stdClass Object
(
[uid] => 28708
[status] => 1
[children] => Array
(
[29448] => stdClass Object
(
[uid] => 29448
[status] => 1
[children] => Array
(
[28528] => stdClass Object
(
[uid] => 28528
[status] => 1
)
[28329] => stdClass Object
(
[uid] => 28329
[status] => 1
[children] => Array
(
[28654] => stdClass Object
(
[uid] => 28654
[status] => 1
)
)
)
[26548] => stdClass Object
(
[uid] => 26548
[status] => 1
)
)
)
)
)
[28748] => stdClass Object
(
[uid] => 28748
[status] => 1
[children] => Array
(
[28838] => stdClass Object
(
[uid] => 28838
[status] => 1
)
[28685] => stdClass Object
(
[uid] => 28685
[status] => 1
)
[29163] => stdClass Object
(
[uid] => 29163
[status] => 1
)
)
)
)
and after using "sortByNumChildren($data)" i'm getting
Array
(
[28748] => stdClass Object
(
[uid] => 28748
[status] => 1
[children] => Array
(
[29163] => stdClass Object
(
[uid] => 29163
[status] => 1
)
[28685] => stdClass Object
(
[uid] => 28685
[status] => 1
)
[28838] => stdClass Object
(
[uid] => 28838
[status] => 1
)
)
)
[28708] => stdClass Object
(
[uid] => 28708
[status] => 1
[children] => Array
(
[29448] => stdClass Object
(
[uid] => 29448
[status] => 1
[children] => Array
(
[26548] => stdClass Object
(
[uid] => 26548
[status] => 1
)
[28329] => stdClass Object
(
[uid] => 28329
[status] => 1
[children] => Array
(
[28654] => stdClass Object
(
[uid] => 28654
[status] => 1
)
)
)
[28528] => stdClass Object
(
[uid] => 28528
[status] => 1
)
)
)
)
)
[27104] => stdClass Object
(
[uid] => 27104
[status] => 1
[children] => Array
(
[26551] => stdClass Object
(
[uid] => 26551
[status] => 1
[children] => Array
(
[25368] => stdClass Object
(
[uid] => 25368
[status] => 1
)
)
)
[28250] => stdClass Object
(
[uid] => 28250
[status] => 1
[children] => Array
(
[28839] => stdClass Object
(
[uid] => 28839
[status] => 1
[children] => Array
(
[27102] => stdClass Object
(
[uid] => 27102
[status] => 1
)
)
)
)
)
)
)
)
and this looks incorrect as this is not sorted by all children's count.
sorting should be according to the number of all descendants.
Please check this.
thank you so much for your answer #trincot .
but i think one thing i missed here
if the total number of children is greater then that array will come first.
As mentioned in my array suppose i have added 2 more elements in the first sub array likewise
28748 => stdClass Object
(
[uid] => 28748
[status] => 1
[children] => Array
(
[29163] => stdClass Object
(
[uid] => 29163
[status] => 1
)
[29173] => stdClass Object
(
[uid] => 29173
[status] => 1
)
[29174] => stdClass Object
(
[uid] => 29174
[status] => 1
)
)
)
total no of children in first sub array = 3
total no of children in second sub array = 7
but total no of children(counting inner children as well) in the second sub array is greater so the second sub array will come first and this shold work same as in the inner part as well.
Please look into this.
You could use a recursive function that sorts each level with uasort:
function sortByNumChildren(&$array) {
foreach ($array as $key => &$obj) {
if (isset($obj->children)) sortByNumChildren($obj->children);
}
uasort($array, function ($a, $b) {
if (!isset($a->children)) return 1;
if (!isset($b->children)) return -1;
return count($b->children) - count($a->children);
});
}
Call this function as follows:
sortByNumChildren($data);
See it run, together with output, on eval.in
This code will sort by number of (immediate) children. See below for sorting on number of descendants.
Code for sorting by number of all descendants
As requested here is an alternative piece of code which sorts the nested array by number of descendants, so also counting grandchildren and their children, etc.:
function sortByNumChildren(&$array) {
$childCounts = array();
foreach ($array as &$obj) {
$childCounts[$obj->uid] = isset($obj->children) ?
sortByNumChildren($obj->children) : 0;
}
uasort($array, function ($a, $b) use ($childCounts) {
return $childCounts[$b->uid] - $childCounts[$a->uid];
});
return count($array) + array_sum($childCounts);
}
Call this function as follows:
sortByNumChildren($data);
See it run, together with output, on eval.in
I have an array after an SQL query made with cake PHP which returns me a tree. I do not the number of dimension of my array.
I want transform it to use it with jstree. I'm fighting with a recursive function and I didn't success.
Can you help me.
My original array looks like this:
Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 815
[Name] => 1
[parent_id] =>
[lft] => 1
[rght] => 30
)
[children] => Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 816
[Name] => 2
[parent_id] => 815
[lft] => 2
[rght] => 15
)
[children] => Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 817
[parent_id] => 816
[lft] => 3
[rght] => 8
)
[children] => Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 818
[Name] => 4
[parent_id] => 817
[lft] => 4
[rght] => 5
)
[children] => Array
(
)
)
[1] => Array
(
[Confsave] => Array
(
[id] => 819
[Name] => 5
[parent_id] => 817
[lft] => 6
[rght] => 7
)
[children] => Array
(
)
)
)
)
)
)
)
)
)
And I want have something like this :
Array
(
[0] => Array
(
[text] => 1
[id] => 815
[children] => Array
(
[0] => Array
(
[text] => 2
[id] => 816
[children] => Array
(
[0] => Array
(
[text] => 3
[id] => 817
[children] => Array
(
[0] => Array
(
[text] => 4
[id] => 818
)
)
)
[1] => Array
(
[text] => 5
[id] => 819
)
)
)
)
)
)
I have try with a recursive function like this but I don't success
private function buildTree(array $elements) {
$branch=array();
foreach ($elements as $element){
$branch[]=$element['Confsave']['Name'];
if(is_array($element['children'])){
$this->buildTree($element);
}
}
return $branch;
}
Edit :
After test and remarks my function is now
private function buildTree(array $elements) {
$branch=array();
foreach ($elements as $element){
$branch[]=$element['Confsave']['Name'];
if(is_array($element['children'])){
$this->buildTree($element['children']);
}
}
return $branch;
}
When I am debuging, I can see that I go to my function for each child (what I want). But I don't know how to make the new array()
this worked for me...
$array = buildTree($array);
print_r($array);
function buildTree(array $parent) {
$branch = array();
foreach ($parent as $index => $element){
$node = array();
$node['id'] = $element['Confsave']['id'];
$node['text'] = isset($element['Confsave']['Name']) ?
$element['Confsave']['Name'] : 'no name';
$node['children'] = buildTree($element['children']);
$branch[] = $node;
}
return $branch;
}
my initial test array....
Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 999
[Name] => 999
[parent_id] =>
[lft] => 1
[rght] => 30
)
[children] => Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 777
[Name] => 777
[parent_id] => 999
[lft] => 1
[rght] => 30
)
[children] => Array
(
)
)
[1] => Array
(
[Confsave] => Array
(
[id] => 888
[Name] => 888
[parent_id] => 999
[lft] => 1
[rght] => 30
)
[children] => Array
(
)
)
)
)
[1] => Array
(
[Confsave] => Array
(
[id] => 815
[Name] => 1
[parent_id] =>
[lft] => 1
[rght] => 30
)
[children] => Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 816
[Name] => 2
[parent_id] => 815
[lft] => 2
[rght] => 15
)
[children] => Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 817
[parent_id] => 816
[lft] => 3
[rght] => 8
)
[children] => Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 818
[Name] => 4
[parent_id] => 817
[lft] => 4
[rght] => 5
)
[children] => Array
(
)
)
[1] => Array
(
[Confsave] => Array
(
[id] => 819
[Name] => 5
[parent_id] => 817
[lft] => 6
[rght] => 7
)
[children] => Array
(
)
)
)
)
)
)
)
)
)
my result array....
Array
(
[0] => Array
(
[id] => 999
[text] => 999
[children] => Array
(
[0] => Array
(
[id] => 777
[text] => 777
[children] => Array
(
)
)
[1] => Array
(
[id] => 888
[text] => 888
[children] => Array
(
)
)
)
)
[1] => Array
(
[id] => 815
[text] => 1
[children] => Array
(
[0] => Array
(
[id] => 816
[text] => 2
[children] => Array
(
[0] => Array
(
[id] => 817
[text] => no name
[children] => Array
(
[0] => Array
(
[id] => 818
[text] => 4
[children] => Array
(
)
)
[1] => Array
(
[id] => 819
[text] => 5
[children] => Array
(
)
)
)
)
)
)
)
)
)
Array
(
[0] => Array
(
[id] => 1
[parent] => 0
[title] => Parent 1
[children] => Array
(
[0] => Array
(
[id] => 2
[parent] => 1
[title] => Child 1
[children] => Array
(
[0] => Array
(
[id] => 3
[parent] => 2
[title] => child 2
[children] => Array
(
)
)
)
)
[1] => Array
(
[id] => 9
[parent] => 1
[title] => parent of one
[children] => Array
(
)
)
[2] => Array
(
[id] => 19
[parent] => 1
[title] => df
[children] => Array
(
)
)
)
)
[1] => Array
(
[id] => 4
[parent] => 0
[title] => parent 2
[children] => Array
(
[0] => Array
(
[id] => 5
[parent] => 4
[title] => child 4
[children] => Array
(
[0] => Array
(
[id] => 6
[parent] => 5
[title] => child 5
[children] => Array
(
[0] => Array
(
[id] => 7
[parent] => 6
[title] => child 7
[children] => Array
(
)
)
[1] => Array
(
[id] => 8
[parent] => 6
[title] => child 8
[children] => Array
(
[0] => Array
(
[id] => 12
[parent] => 8
[title] => child 9
[children] => Array
(
)
)
)
)
)
)
[1] => Array
(
[id] => 17
[parent] => 5
[title] => child unknown
[children] => Array
(
[0] => Array
(
[id] => 18
[parent] => 17
[title] => asdasd
[children] => Array
(
)
)
)
)
)
)
)
)
[2] => Array
(
[id] => 13
[parent] => 0
[title] => parent 3
[children] => Array
(
)
)
[3] => Array
(
[id] => 14
[parent] => 0
[title] => parent 4
[children] => Array
(
[0] => Array
(
[id] => 21
[parent] => 14
[title] => sad
[children] => Array
(
[0] => Array
(
[id] => 22
[parent] => 21
[title] => sdfsaf
[children] => Array
(
[0] => Array
(
[id] => 23
[parent] => 22
[title] => test
[children] => Array
(
[0] => Array
(
[id] => 24
[parent] => 23
[title] => tester
[children] => Array
(
[0] => Array
(
[id] => 25
[parent] => 24
[title] => tested
[children] => Array
(
[0] => Array
(
[id] => 26
[parent] => 25
[title] => example
[children] => Array
(
[0] => Array
(
[id] => 27
[parent] => 26
[title] => examples
[children] => Array
(
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
[4] => Array
(
[id] => 15
[parent] => 0
[title] => parent 5
[children] => Array
(
)
)
[5] => Array
(
[id] => 16
[parent] => 0
[title] => parent 6
[children] => Array
(
)
)
[6] => Array
(
[id] => 20
[parent] => 0
[title] => aaa
[children] => Array
(
)
)
)
any suggestions on how to make a nested dropdown out of this array ?
i have no idea where to start.....
$HOST="localhost";
$DB="db_dir";
$USER="root";
$PASS="";
mysql_connect($HOST,$USER,$PASS);
mysql_select_db($DB);
function RecursiveCat($pid)
{
static $level=0;
static $strid="";
static $strname="";
$sql=mysql_query("select * from categories where cat_parent =".$pid." ");
//var_dump($sql);
while($row=mysql_fetch_assoc($sql))
{
$id=$row['cat_id'];
$level--;
$pad="";
for($p=1;$p<($level*-1);$p++) $pad.=" > ";
$strname.='<option value="'.$row['cat_id'].'">'.$pad.$row['cat_title'].'</option>';
$rid=RecursiveCat($id);
$strid[]=$row['cat_id'];
$level++;
}
return $strname;
}
<?php echo '<select name="parent">'.'<option value="0">None</option>';
echo RecursiveCat(0);echo '</select>';?>