I have this flat array that I am trying to turn into a parent-child array:
Array
(
[0] => Array
(
[parent_id] => t3_42yrg7
[id] => t1_cze7b2e
)
[1] => Array
(
[parent_id] => t3_42yrg7
[id] => t1_czeg8ay
)
[2] => Array
(
[parent_id] => t1_czeg8ay
[id] => t1_czet481
)
[3] => Array
(
[parent_id] => t3_42yrg7
[id] => t1_cze4whs
)
[4] => Array
(
[parent_id] => t1_cze4whs
[id] => t1_cze9v0o
)
[5] => Array
(
[parent_id] => t1_cze9v0o
[id] => t1_czec4vd
)
[6] => Array
(
[parent_id] => t1_czec4vd
[id] => t1_czep9e2
)
[7] => Array
(
[parent_id] => t1_czep9e2
[id] => t1_czf2k8e
)
[8] => Array
(
[parent_id] => t1_czec4vd
[id] => t1_czeixa7
)
[9] => Array
(
[parent_id] => t1_cze9v0o
[id] => t1_czecx26
)
[10] => Array
(
[parent_id] => t1_czecx26
[id] => t1_czeenuz
)
[11] => Array
(
[parent_id] => t1_czeenuz
[id] => t1_czeftkf
)
[12] => Array
(
[parent_id] => t1_czecx26
[id] => t1_czehjrz
)
[13] => Array
(
[parent_id] => t1_czecx26
[id] => t1_czeien4
)
[14] => Array
(
[parent_id] => t1_cze9v0o
[id] => t1_czebugl
)
[15] => Array
(
[parent_id] => t1_cze4whs
[id] => t1_cze9hvb
)
[16] => Array
(
[parent_id] => t3_42yrg7
[id] => t1_cze3vku
)
[17] => Array
(
[parent_id] => t1_cze3vku
[id] => t1_czemk0g
)
[18] => Array
(
[parent_id] => t3_42yrg7
[id] => t1_czeedh9
)
[19] => Array
(
[parent_id] => t3_42yrg7
[id] => t1_czeenjx
)
[20] => Array
(
[parent_id] => t3_42yrg7
[id] => t1_cze5r5u
)
[21] => Array
(
[parent_id] => t1_cze5r5u
[id] => t1_czefle4
)
[22] => Array
(
[parent_id] => t3_42yrg7
[id] => t1_cze8oht
)
[23] => Array
(
[parent_id] => t1_cze8oht
[id] => t1_czelwvs
)
[24] => Array
(
[parent_id] => t3_42yrg7
[id] => t1_cze5cs6
)
[25] => Array
(
[parent_id] => t1_cze5cs6
[id] => t1_czefydi
)
[26] => Array
(
[parent_id] => t1_cze5cs6
[id] => t1_czedpml
)
[27] => Array
(
[parent_id] => t3_42yrg7
[id] => t1_cze42aq
)
[28] => Array
(
[parent_id] => t1_cze42aq
[id] => t1_cze8iei
)
[29] => Array
(
[parent_id] => t1_cze8iei
[id] => t1_czebkgk
)
[30] => Array
(
[parent_id] => t1_czebkgk
[id] => t1_czedid2
)
[31] => Array
(
[parent_id] => t1_cze8iei
[id] => t1_czebqgn
)
[32] => Array
(
[parent_id] => t1_cze42aq
[id] => t1_cze77xr
)
[33] => Array
(
[parent_id] => t3_42yrg7
[id] => t1_czen20j
)
)
Here is my recursive function that maps the ids and makes it into a parent-child array:
function buildTree(array $elements, $parentId = 0) {
$branch = array();
foreach ($elements as $element) {
if ($element['parent_id'] == $parentId) {
$children = buildTree($elements, $element['id']);
if ($children) {
$element['children'] = $children;
}
$branch[] = $element;
}
}
return $branch;
}
Here is the output I am getting from the above function:
Array
(
[0] => Array
(
[parent_id] => t3_42yrg7
[id] => t1_cze7b2e
)
[1] => Array
(
[parent_id] => t3_42yrg7
[id] => t1_czeg8ay
[children] => Array
(
[0] => Array
(
[parent_id] => t1_czeg8ay
[id] => t1_czet481
)
)
)
[2] => Array
(
[parent_id] => t1_czeg8ay
[id] => t1_czet481
)
[3] => Array
(
[parent_id] => t3_42yrg7
[id] => t1_cze4whs
[children] => Array
(
[0] => Array
(
[parent_id] => t1_cze4whs
[id] => t1_cze9v0o
[children] => Array
(
[0] => Array
(
[parent_id] => t1_cze9v0o
[id] => t1_czec4vd
[children] => Array
(
[0] => Array
(
[parent_id] => t1_czec4vd
[id] => t1_czep9e2
[children] => Array
(
[0] => Array
(
[parent_id] => t1_czep9e2
[id] => t1_czf2k8e
)
)
)
[1] => Array
(
[parent_id] => t1_czec4vd
[id] => t1_czeixa7
)
)
)
[1] => Array
(
[parent_id] => t1_cze9v0o
[id] => t1_czecx26
[children] => Array
(
[0] => Array
(
[parent_id] => t1_czecx26
[id] => t1_czeenuz
[children] => Array
(
[0] => Array
(
[parent_id] => t1_czeenuz
[id] => t1_czeftkf
)
)
)
[1] => Array
(
[parent_id] => t1_czecx26
[id] => t1_czehjrz
)
[2] => Array
(
[parent_id] => t1_czecx26
[id] => t1_czeien4
)
)
)
[2] => Array
(
[parent_id] => t1_cze9v0o
[id] => t1_czebugl
)
)
)
[1] => Array
(
[parent_id] => t1_cze4whs
[id] => t1_cze9hvb
)
)
)
[4] => Array
(
[parent_id] => t1_cze4whs
[id] => t1_cze9v0o
[children] => Array
(
[0] => Array
(
[parent_id] => t1_cze9v0o
[id] => t1_czec4vd
[children] => Array
(
[0] => Array
(
[parent_id] => t1_czec4vd
[id] => t1_czep9e2
[children] => Array
(
[0] => Array
(
[parent_id] => t1_czep9e2
[id] => t1_czf2k8e
)
)
)
[1] => Array
(
[parent_id] => t1_czec4vd
[id] => t1_czeixa7
)
)
)
[1] => Array
(
[parent_id] => t1_cze9v0o
[id] => t1_czecx26
[children] => Array
(
[0] => Array
(
[parent_id] => t1_czecx26
[id] => t1_czeenuz
[children] => Array
(
[0] => Array
(
[parent_id] => t1_czeenuz
[id] => t1_czeftkf
)
)
)
[1] => Array
(
[parent_id] => t1_czecx26
[id] => t1_czehjrz
)
[2] => Array
(
[parent_id] => t1_czecx26
[id] => t1_czeien4
)
)
)
[2] => Array
(
[parent_id] => t1_cze9v0o
[id] => t1_czebugl
)
)
)
[5] => Array
(
[parent_id] => t1_cze9v0o
[id] => t1_czec4vd
[children] => Array
(
[0] => Array
(
[parent_id] => t1_czec4vd
[id] => t1_czep9e2
[children] => Array
(
[0] => Array
(
[parent_id] => t1_czep9e2
[id] => t1_czf2k8e
)
)
)
[1] => Array
(
[parent_id] => t1_czec4vd
[id] => t1_czeixa7
)
)
)
[6] => Array
(
[parent_id] => t1_czec4vd
[id] => t1_czep9e2
[children] => Array
(
[0] => Array
(
[parent_id] => t1_czep9e2
[id] => t1_czf2k8e
)
)
)
[7] => Array
(
[parent_id] => t1_czep9e2
[id] => t1_czf2k8e
)
[8] => Array
(
[parent_id] => t1_czec4vd
[id] => t1_czeixa7
)
[9] => Array
(
[parent_id] => t1_cze9v0o
[id] => t1_czecx26
[children] => Array
(
[0] => Array
(
[parent_id] => t1_czecx26
[id] => t1_czeenuz
[children] => Array
(
[0] => Array
(
[parent_id] => t1_czeenuz
[id] => t1_czeftkf
)
)
)
[1] => Array
(
[parent_id] => t1_czecx26
[id] => t1_czehjrz
)
[2] => Array
(
[parent_id] => t1_czecx26
[id] => t1_czeien4
)
)
)
[10] => Array
(
[parent_id] => t1_czecx26
[id] => t1_czeenuz
[children] => Array
(
[0] => Array
(
[parent_id] => t1_czeenuz
[id] => t1_czeftkf
)
)
)
[11] => Array
(
[parent_id] => t1_czeenuz
[id] => t1_czeftkf
)
[12] => Array
(
[parent_id] => t1_czecx26
[id] => t1_czehjrz
)
[13] => Array
(
[parent_id] => t1_czecx26
[id] => t1_czeien4
)
[14] => Array
(
[parent_id] => t1_cze9v0o
[id] => t1_czebugl
)
[15] => Array
(
[parent_id] => t1_cze4whs
[id] => t1_cze9hvb
)
[16] => Array
(
[parent_id] => t3_42yrg7
[id] => t1_cze3vku
[children] => Array
(
[0] => Array
(
[parent_id] => t1_cze3vku
[id] => t1_czemk0g
)
)
)
[17] => Array
(
[parent_id] => t1_cze3vku
[id] => t1_czemk0g
)
[18] => Array
(
[parent_id] => t3_42yrg7
[id] => t1_czeedh9
)
[19] => Array
(
[parent_id] => t3_42yrg7
[id] => t1_czeenjx
)
[20] => Array
(
[parent_id] => t3_42yrg7
[id] => t1_cze5r5u
[children] => Array
(
[0] => Array
(
[parent_id] => t1_cze5r5u
[id] => t1_czefle4
)
)
)
[21] => Array
(
[parent_id] => t1_cze5r5u
[id] => t1_czefle4
)
[22] => Array
(
[parent_id] => t3_42yrg7
[id] => t1_cze8oht
[children] => Array
(
[0] => Array
(
[parent_id] => t1_cze8oht
[id] => t1_czelwvs
)
)
)
[23] => Array
(
[parent_id] => t1_cze8oht
[id] => t1_czelwvs
)
[24] => Array
(
[parent_id] => t3_42yrg7
[id] => t1_cze5cs6
[children] => Array
(
[0] => Array
(
[parent_id] => t1_cze5cs6
[id] => t1_czefydi
)
[1] => Array
(
[parent_id] => t1_cze5cs6
[id] => t1_czedpml
)
)
)
[25] => Array
(
[parent_id] => t1_cze5cs6
[id] => t1_czefydi
)
[26] => Array
(
[parent_id] => t1_cze5cs6
[id] => t1_czedpml
)
[27] => Array
(
[parent_id] => t3_42yrg7
[id] => t1_cze42aq
[children] => Array
(
[0] => Array
(
[parent_id] => t1_cze42aq
[id] => t1_cze8iei
[children] => Array
(
[0] => Array
(
[parent_id] => t1_cze8iei
[id] => t1_czebkgk
[children] => Array
(
[0] => Array
(
[parent_id] => t1_czebkgk
[id] => t1_czedid2
)
)
)
[1] => Array
(
[parent_id] => t1_cze8iei
[id] => t1_czebqgn
)
)
)
[1] => Array
(
[parent_id] => t1_cze42aq
[id] => t1_cze77xr
)
)
)
[28] => Array
(
[parent_id] => t1_cze42aq
[id] => t1_cze8iei
[children] => Array
(
[0] => Array
(
[parent_id] => t1_cze8iei
[id] => t1_czebkgk
[children] => Array
(
[0] => Array
(
[parent_id] => t1_czebkgk
[id] => t1_czedid2
)
)
)
[1] => Array
(
[parent_id] => t1_cze8iei
[id] => t1_czebqgn
)
)
)
[29] => Array
(
[parent_id] => t1_cze8iei
[id] => t1_czebkgk
[children] => Array
(
[0] => Array
(
[parent_id] => t1_czebkgk
[id] => t1_czedid2
)
)
)
[30] => Array
(
[parent_id] => t1_czebkgk
[id] => t1_czedid2
)
[31] => Array
(
[parent_id] => t1_cze8iei
[id] => t1_czebqgn
)
[32] => Array
(
[parent_id] => t1_cze42aq
[id] => t1_cze77xr
)
[33] => Array
(
[parent_id] => t3_42yrg7
[id] => t1_czen20j
)
)
While the first part of it is correct, it sometimes labels the child arrays as a parent after mapping it correctly the first time. Why are some child arrays being mapped as a parent?
Edit: I'm an idiot, when I calling buildTree() I was not passing in a parentId and just left the value as zero.
I don't know what you want exactly but I have added my code to separate parent and children in array. Please check below code:
function buildTree($elements = array()) {
$branch = array();
if(!empty($elements))
{
foreach ($elements as $element) {
if(!isset($branch[$element['parent_id']]))
{
$branch['parent_id'][]=$element['parent_id'];
$branch[$element['parent_id']]=array();
$branch[$element['parent_id']]['children'][]=$element['id'];
}
else
{
$branch[$element['parent_id']]['children'][]=$element['id'];
}
}
}
return $branch;
}
$arr_rtn = buildTree($arr);
Related
I have an input category array with childs as follows
I want to convert it using a recursive function to another array like
OUTPUT NEEDED
['1'=>'fashion', '10' => 'fashion > women','23'=> 'fashion > women > clothing','29'=> 'fashion > women > clothing > dresses' ... and so on ]
My purpose is to use output array to populate select box options to adda category.
INPUT ARRAY
Array
(
[1] => Array
(
[id] => 1
[name] => fashion
[parent_id] => 0
[childs] => Array
(
[10] => Array
(
[id] => 10
[name] => women
[parent_id] => 1
[childs] => Array
(
[23] => Array
(
[id] => 23
[name] => clothing
[parent_id] => 10
[childs] => Array
(
[29] => Array
(
[id] => 29
[name] => dresses
[parent_id] => 23
[childs] => Array
(
)
)
[30] => Array
(
[id] => 30
[name] => jumpsuits
[parent_id] => 23
[childs] => Array
(
)
)
)
)
[24] => Array
(
[id] => 24
[name] => bags & accessories
[parent_id] => 10
[childs] => Array
(
)
)
[25] => Array
(
[id] => 25
[name] => shoes
[parent_id] => 10
[childs] => Array
(
)
)
[26] => Array
(
[id] => 26
[name] => watches
[parent_id] => 10
[childs] => Array
(
)
)
[27] => Array
(
[id] => 27
[name] => jewelery
[parent_id] => 10
[childs] => Array
(
)
)
[28] => Array
(
[id] => 28
[name] => eye-wear
[parent_id] => 10
[childs] => Array
(
)
)
)
)
[11] => Array
(
[id] => 11
[name] => men
[parent_id] => 1
[childs] => Array
(
)
)
[12] => Array
(
[id] => 12
[name] => kids
[parent_id] => 1
[childs] => Array
(
)
)
[13] => Array
(
[id] => 13
[name] => sports
[parent_id] => 1
[childs] => Array
(
)
)
[14] => Array
(
[id] => 14
[name] => bags
[parent_id] => 1
[childs] => Array
(
)
)
[15] => Array
(
[id] => 15
[name] => eyewear
[parent_id] => 1
[childs] => Array
(
)
)
[16] => Array
(
[id] => 16
[name] => watches & jewelery
[parent_id] => 1
[childs] => Array
(
)
)
)
)
[2] => Array
(
[id] => 2
[name] => supermarket
[parent_id] => 0
[childs] => Array
(
[17] => Array
(
[id] => 17
[name] => food & beverages
[parent_id] => 2
[childs] => Array
(
[31] => Array
(
[id] => 31
[name] => breakfast
[parent_id] => 17
[childs] => Array
(
)
)
[32] => Array
(
[id] => 32
[name] => snacks
[parent_id] => 17
[childs] => Array
(
)
)
)
)
[18] => Array
(
[id] => 18
[name] => dairy products
[parent_id] => 2
[childs] => Array
(
)
)
[19] => Array
(
[id] => 19
[name] => beauty
[parent_id] => 2
[childs] => Array
(
)
)
[20] => Array
(
[id] => 20
[name] => homecare
[parent_id] => 2
[childs] => Array
(
)
)
[21] => Array
(
[id] => 21
[name] => baby world
[parent_id] => 2
[childs] => Array
(
)
)
[22] => Array
(
[id] => 22
[name] => pet world
[parent_id] => 2
[childs] => Array
(
)
)
)
)
[3] => Array
(
[id] => 3
[name] => electronics
[parent_id] => 0
[childs] => Array
(
[33] => Array
(
[id] => 33
[name] => laptops
[parent_id] => 3
[childs] => Array
(
)
)
[34] => Array
(
[id] => 34
[name] => television
[parent_id] => 3
[childs] => Array
(
)
)
)
)
[4] => Array
(
[id] => 4
[name] => mobiles & tablets
[parent_id] => 0
[childs] => Array
(
[35] => Array
(
[id] => 35
[name] => mobiles
[parent_id] => 4
[childs] => Array
(
)
)
[36] => Array
(
[id] => 36
[name] => tablets
[parent_id] => 4
[childs] => Array
(
)
)
)
)
[5] => Array
(
[id] => 5
[name] => baby & toys
[parent_id] => 0
[childs] => Array
(
)
)
[6] => Array
(
[id] => 6
[name] => home
[parent_id] => 0
[childs] => Array
(
)
)
[7] => Array
(
[id] => 7
[name] => perfumes & beauty
[parent_id] => 0
[childs] => Array
(
)
)
[8] => Array
(
[id] => 8
[name] => sports & fitness
[parent_id] => 0
[childs] => Array
(
)
)
[9] => Array
(
[id] => 9
[name] => automotive
[parent_id] => 0
[childs] => Array
(
)
)
)
I need an output array with key,the category id
You can create array by this way:
$array = array(
'category_1' => 'value',
'category_2' => array(
'subcategory_1' => 'value',
'subcategory_2' => 'value',
),
);
I have a multidimensional array in PHP which looks like the following code:
Array
(
[0] => Array
(
[ID] => 6898
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 8214
[ParentID] => 6898
)
[1] => Array
(
[ID] => 5595
[ParentID] => 6898
[children] => Array
(
[0] => Array
(
[ID] => 7238
[ParentID] => 5595
)
)
)
)
)
[1] => Array
(
[ID] => 8216
[ParentID] =>
)
[2] => Array
(
[ID] => 7046
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 5555
[ParentID] => 7046
[children] => Array
(
[0] => Array
(
[ID] => 7294
[ParentID] => 5555
)
)
)
[1] => Array
(
[ID] => 5706
[ParentID] => 7046
)
)
)
[3] => Array
(
[ID] => 5884
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 7431
[ParentID] => 5884
[children] => Array
(
[0] => Array
(
[ID] => 5618
[ParentID] => 7431
)
)
)
[1] => Array
(
[ID] => 6993
[ParentID] => 5884
)
)
)
[4] => Array
(
[ID] => 6002
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 5801
[ParentID] => 6002
)
[1] => Array
(
[ID] => 6615
[ParentID] => 6002
[children] => Array
(
[0] => Array
(
[ID] => 6595
[ParentID] => 6615
)
)
)
)
)
[5] => Array
(
[ID] => 8119
[ParentID] =>
)
[6] => Array
(
[ID] => 8116
[ParentID] =>
)
[7] => Array
(
[ID] => 5992
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 7614
[ParentID] => 5992
[children] => Array
(
[0] => Array
(
[ID] => 7647
[ParentID] => 7614
)
)
)
[1] => Array
(
[ID] => 8215
[ParentID] => 5992
)
)
)
[8] => Array
(
[ID] => 5690
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 8188
[ParentID] => 5690
)
[1] => Array
(
[ID] => 5678
[ParentID] => 5690
)
)
)
[9] => Array
(
[ID] => 5825
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 6842
[ParentID] => 5825
)
[1] => Array
(
[ID] => 5653
[ParentID] => 5825
[children] => Array
(
[0] => Array
(
[ID] => 6490
[ParentID] => 5653
[children] => Array
(
[0] => Array
(
[ID] => 7737
[ParentID] => 6490
)
)
)
)
)
)
)
[10] => Array
(
[ID] => 8114
[ParentID] =>
)
[11] => Array
(
[ID] => 8113
[ParentID] =>
)
[12] => Array
(
[ID] => 7826
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 7871
[ParentID] => 7826
[children] => Array
(
[0] => Array
(
[ID] => 5891
[ParentID] => 7871
)
)
)
[1] => Array
(
[ID] => 5983
[ParentID] => 7826
)
)
)
)
Array
(
[0] => Array
(
[ID] => 6898
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 8214
[ParentID] => 6898
)
[1] => Array
(
[ID] => 5595
[ParentID] => 6898
[children] => Array
(
[0] => Array
(
[ID] => 7238
[ParentID] => 5595
)
)
)
)
)
[1] => Array
(
[ID] => 8216
[ParentID] =>
)
[2] => Array
(
[ID] => 7046
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 5555
[ParentID] => 7046
[children] => Array
(
[0] => Array
(
[ID] => 7294
[ParentID] => 5555
)
)
)
[1] => Array
(
[ID] => 5706
[ParentID] => 7046
)
)
)
[3] => Array
(
[ID] => 5884
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 7431
[ParentID] => 5884
[children] => Array
(
[0] => Array
(
[ID] => 5618
[ParentID] => 7431
)
)
)
[1] => Array
(
[ID] => 6993
[ParentID] => 5884
)
)
)
[4] => Array
(
[ID] => 6002
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 5801
[ParentID] => 6002
)
[1] => Array
(
[ID] => 6615
[ParentID] => 6002
[children] => Array
(
[0] => Array
(
[ID] => 6595
[ParentID] => 6615
)
)
)
)
)
[5] => Array
(
[ID] => 8119
[ParentID] =>
)
[6] => Array
(
[ID] => 8116
[ParentID] =>
)
[7] => Array
(
[ID] => 5992
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 7614
[ParentID] => 5992
[children] => Array
(
[0] => Array
(
[ID] => 7647
[ParentID] => 7614
)
)
)
[1] => Array
(
[ID] => 8215
[ParentID] => 5992
)
)
)
[8] => Array
(
[ID] => 5690
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 8188
[ParentID] => 5690
)
[1] => Array
(
[ID] => 5678
[ParentID] => 5690
)
)
)
[9] => Array
(
[ID] => 5825
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 6842
[ParentID] => 5825
)
[1] => Array
(
[ID] => 5653
[ParentID] => 5825
[children] => Array
(
[0] => Array
(
[ID] => 6490
[ParentID] => 5653
[children] => Array
(
[0] => Array
(
[ID] => 7737
[ParentID] => 6490
)
)
)
)
)
)
)
[10] => Array
(
[ID] => 8114
[ParentID] =>
)
[11] => Array
(
[ID] => 8113
[ParentID] =>
)
[12] => Array
(
[ID] => 7826
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 7871
[ParentID] => 7826
[children] => Array
(
[0] => Array
(
[ID] => 5891
[ParentID] => 7871
)
)
)
[1] => Array
(
[ID] => 5983
[ParentID] => 7826
)
)
)
)
This is the hierarchical array in which I want to pass two node ID's something like this -> getLevelDifference(7826, 5891) and it should give 2 as result or a function in which I can get that level child array like -> getLevelChild(7826, 2) and it should return 5891 as the result or other childs of the same level ?
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 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>';?>