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
(
)
)
)
)
)
)
)
)
)
Related
Hi guys I was wondering how can I add a incremental all elements? Because as of now I am not sure where can I include the "inc" in all elements
Ex:
MY_ARRAY = (
[id] => 4
[children] => Array
(
[0] => Array
(
[id] => 18
[children] => Array
(
[0] => Array
(
[id] => 21
)
[1] => Array
(
[id] => 22
)
)
)
[1] => Array
(
[id] => 19
)
[2] => Array
(
[id] => 20
[children] => Array
(
[0] => Array
(
[id] => 26
)
)
)
)
)
Using these code:
$in = MY_ARRAY
function generateArray($in, $parent = 0){
foreach ($in as $key => $value) {
if(is_numeric($key)){
$in = $value;
$out[$key] = $this->generateArray($in, $parent);
}else{
$out[$key]=$value;
if($key=="id"){
$out['p_id'] = $parent;
$parent=$value;
}elseif($key=="children"){
$in = $value;
$out[$key] = $this->generateArray($in, $parent);
}
}
}
return $out;
}
Will give me this output, not including the [inc].
[id] => 4
[P_id] => 0
[inc] => 1
[children] => Array
(
[0] => Array
(
[id] => 18
[P_id] => 4
[inc] => 2
[children] => Array
(
[0] => Array
(
[id] => 21
[P_id] => 18
[inc] => 3
)
[1] => Array
(
[id] => 22
[P_id] => 18
[inc] => 4
)
)
)
[1] => Array
(
[id] => 19
[P_id] => 4
[inc] => 5
)
[2] => Array
(
[id] => 20
[P_id] => 4
[inc] => 6
[children] => Array
(
[0] => Array
(
[id] => 26
[P_id] => 20
[inc] => 7
)
)
)
)
)
Now I'm not sure where and how can I include the [inc] or the incremental value of each element in the array using my code above.
Need really help here guys...
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 two array $array1 and $array2 which I get dynamically and look like
$array1 = Array
(
[0] => Array
(
[hour] => 10
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 2
)
[1] => Array
(
[activity_id] => 2
[cnt] => 1
)
)
)
[1] => Array
(
[hour] => 11
[activity] => Array
(
)
)
[2] => Array
(
[hour] => 12
[percentage] => 0
[activity] => Array
(
[0] => Array
(
[activity_id] => 2
[cnt] => 5
)
[1] => Array
(
[activity_id] => 3
[cnt] => 2
)
)
)
);
$array2 = Array
(
[0] => Array
(
[id] => 1
[name] => Phone Calls
[readable] => 1
[status] => active
)
[1] => Array
(
[id] => 2
[name] => Meeting With Customer
[readable] => 1
[status] => active
)
[2] => Array
(
[id] => 3
[name] => Others Works
[readable] => 1
[status] => active
)
);
which i need to compare.
if $array2['id'] is not in $array1["activity"](i.e"activity_id") add array ['activity_id'=>$array2['id'],'cnt'=>0] to $array1['activity'].
My result must be like
$result = Array
(
[0] => Array
(
[hour] => 10
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 2
)
[1] => Array
(
[activity_id] => 2
[cnt] => 1
)
[2] => Array
(
[activity_id] => 3
[cnt] => 0
)
)
)
[1] => Array
(
[hour] => 11
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 0
)
[1] => Array
(
[activity_id] => 2
[cnt] => 0
)
[2] => Array
(
[activity_id] => 3
[cnt] => 0
)
)
)
[2] => Array
(
[hour] => 12
[percentage] => 0
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 0
)
[1] => Array
(
[activity_id] => 2
[cnt] => 5
)
[2] => Array
(
[activity_id] => 3
[cnt] => 2
)
)
)
);
What i have tried is
$finalArray = array();
foreach($array1 as $arr1) {
foreach($array2 as $arr2) {
if(!in_array($arr2['id'], $arr1['activity'])) {
$array = ['activity_id'=>$arr2['id'], 'cnt'=>0];
}
array_push($arr1['activity'], $array);
unset($array);
}
array_push($finalArray, $result);
}
print_r($finalArray);
in_array() function is not working as I excepted or I am trying to do it in the wrong way. Can someone helps me with this?
Sorry,finally i get what i did wrong.May be someone get helped.
everything is ok just change the line
if(!in_array($arr2['id'], $arr1['activity'])) {
into
if(!in_array( $readActivity['id'], array_column($result['activity'],'activity_id'))){
I want to convert this array in a single dimensional flat array without losing the sort order.
Array
(
[0] => Array
(
[id] => 1
[title] => Computer
[parent_id] => 0
[children] => Array
(
[0] => Array
(
[id] => 4
[title] => keyboard
[parent_id] => 1
[children] => Array
(
[0] => Array
(
[id] => 6
[title] => Mouse
[parent_id] => 4
[children] => Array
(
[0] => Array
(
[id] => 7
[title] => webcam
[parent_id] => 6
)
)
)
)
)
)
)
[1] => Array
(
[id] => 43
[title] => Mobile
[parent_id] => 0
[children] => Array
(
[0] => Array
(
[id] => 5
[title] => bar phones
[parent_id] => 43
)
[1] => Array
(
[id] => 47
[title] => Touchscreen
[parent_id] => 43
[children] => Array
(
[0] => Array
(
[id] => 41
[title] => Samsung
[parent_id] => 47
)
[1] => Array
(
[id] => 44
[title] => Micromax
[parent_id] => 47
)
[2] => Array
(
[id] => 45
[title] => Huawei
[parent_id] => 47
)
)
)
)
)
[2] => Array
(
[id] => 46
[title] => Camera
[parent_id] => 0
)
[3] => Array
(
[id] => 42
[title] => Heater
[parent_id] => 0
)
)
Give it try with below function:
function makeOneDimensionArray(array $array, &$res = array())
{
foreach($array as $arr)
{
$res[] = array(
'id' => $arr['id'],
'title' => $arr['title'],
'parent_id' => $arr['parent_id']
);
if(isset($arr['children']))
{
makeOneDimensionArray($arr['children'], $res);
}
}
return $res;
}
$finalArr = makeOneDimensionArray($your_array);
print_r($finalArr);
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>';?>