I have an array of categories where id is the id of the category, parent denotes the parent id of the category (id 0 denotes the top most parent node) and value is the title of the array. The path is initially set to the id of the category. The array is as follows:
Array
(
[0] => Array
(
[id] => 1
[parent] => 0
[value] => Corporate Files
[path] => 1
)
[1] => Array
(
[id] => 2
[parent] => 0
[value] => Products Files
[path] => 2
)
[2] => Array
(
[id] => 3
[parent] => 1
[value] => Communications Materials
[path] => 3
)
[3] => Array
(
[id] => 4
[parent] => 1
[value] => Group Technical
[path] => 4
)
[4] => Array
(
[id] => 5
[parent] => 1
[value] => New Projects
[path] => 5
)
[5] => Array
(
[id] => 6
[parent] => 2
[value] => Product Range
[path] => 6
)
[6] => Array
(
[id] => 7
[parent] => 2
[value] => WL4
[path] => 7
)
);
I want to generate the paths of categories in the array. so the output should be
Array
(
[0] => Array
(
[id] => 1
[parent] => 0
[value] => Corporate Files
[path] => 1
)
[1] => Array
(
[id] => 2
[parent] => 0
[value] => Products Files
[path] => 2
)
[2] => Array
(
[id] => 3
[parent] => 1
[value] => Communications Materials
[path] => 1,3
)
[3] => Array
(
[id] => 4
[parent] => 1
[value] => Group Technical
[path] => 1,4
)
[4] => Array
(
[id] => 5
[parent] => 1
[value] => New Projects
[path] => 1,5
)
[5] => Array
(
[id] => 6
[parent] => 2
[value] => Product Range
[path] => 2,6
)
[6] => Array
(
[id] => 7
[parent] => 2
[value] => WL4
[path] => 2,7
)
);
I wrote the following function.
function findparent($id,$path){
global $categories;
global $catcnt;
if($id==0){
echo $path."<br />"; //this outputs path currently
return $path;
}
for($i=0;$i<$catcnt;$i++){
if($id==$categories[$i]['id']){
$path=$id.",".$path;
findparent($categories[$i]['parent'],$path);
}
}
}
for($i=0;$i<count($categories);$i++){
$categories[$i]['path']=(string)findparent($categories[$i]['parent'],$categories[$i]['id']); //this doesnt assign it currectly
}
and the output is:
Array
(
[0] => Array
(
[id] => 1
[parent] => 0
[value] => Corporate Files
[path] =>
)
[1] => Array
(
[id] => 2
[parent] => 0
[value] => Products Files
[path] =>
)
[2] => Array
(
[id] => 3
[parent] => 1
[value] => Communications Materials
[path] =>
)
[3] => Array
(
[id] => 4
[parent] => 1
[value] => Group Technical
[path] =>
)
[4] => Array
(
[id] => 5
[parent] => 1
[value] => New Projects
[path] =>
)
[5] => Array
(
[id] => 6
[parent] => 2
[value] => Product Range
[path] =>
)
[6] => Array
(
[id] => 7
[parent] => 2
[value] => WL4
[path] =>
)
);
Where am I going wrong?
findparent only returns if the id is zero.
You need a second return statement, before the recursive findparent call.
As you don't need to deal with multiple levels, a function of it's own is a bit of an overhead as you could work straight on the array itself with a simple foreach:
foreach ($array as &$node)
{
if ($node['parent'])
{
$node['path'] = $node['parent'] . ',' . $node['path'];
}
}
unset($node);
However, you can put this into a function of it's own as well, but you won't need any global variables as far as I can see.
What you see here is simple string concatenation, this makes the array as you wrote you wanted it to have. My first comment was more meant to manage the structure with an n-depth not a 1-depth. Demo
Related
I want key rating sum common slug key value. Please help me out.
Array
(
[0] => Array
(
[0] => Array
(
[id] => 6
[slug] => scenario
[avis_id] => 2
[rating] => 0
)
[1] => Array
(
[id] => 7
[slug] => jeu_d_acteur
[avis_id] => 2
[rating] => 9
)
[2] => Array
(
[id] => 8
[slug] => effets_speciaux
[avis_id] => 2
[rating] => 0
)
[3] => Array
(
[id] => 9
[slug] => prises_de_vues
[avis_id] => 2
[rating] => 0
)
[4] => Array
(
[id] => 10
[slug] => bande_son
[avis_id] => 2
[rating] => 6.8
)
)
[1] => Array
(
[0] => Array
(
[id] => 1
[slug] => scenario
[avis_id] => 1
[rating] => 8
)
[1] => Array
(
[id] => 2
[slug] => jeu_d_acteur
[avis_id] => 1
[rating] => 5
)
[2] => Array
(
[id] => 3
[slug] => effets_speciaux
[avis_id] => 1
[rating] => 5
)
[3] => Array
(
[id] => 4
[slug] => prises_de_vues
[avis_id] => 1
[rating] => 6
)
[4] => Array
(
[id] => 5
[slug] => bande_son
[avis_id] => 1
[rating] => 8
)
)
)
output:
Array
(
[slug] => scenario
[rating] => 8
)
[1] => Array
(
[slug] => jeu_d_acteur
[rating] => 14
)
[2] => Array
(
[slug] => effets_speciaux
[rating] => 3
)
[3] => Array
(
[slug] => prises_de_vues
[rating] => 6
)
[4] => Array
(
[slug] => bande_son
[rating] => 14.8
)
)
You can merge them by using ... splat operator and loop through to group by slug and sum rating
$merged = array_merge(...$a);
$r = [];
foreach($merged as $v){
isset($r[$v['slug']])
? ($r[$v['slug']]['rating'] += $v['rating'] )
: ($r[$v['slug']] = ['slug'=>$v['slug'],'rating'=>$v['rating']]);
}
Working example :- https://3v4l.org/odCl6
I get an array as shown below using this query:
SELECT id, person_id, description, path FROM comment WHERE path <# '0001' ORDER by path
My question is how am I suppose to use the to display the output as threaded? Do I check the first four numbers and increment to see if they are in the same level or is there something that I'm failing to understand?
[0] => Array
(
[id] => 1
[person_id] => 1
[description] => a75e62536ca2687ae5817acb739e61f3
[path] => 0001
)
[1] => Array
(
[id] => 2
[person_id] => 2
[description] => 010242f78cd5d41abb16a8ba488957a1
[path] => 0001.0001.0001
)
[2] => Array
(
[id] => 3
[person_id] => 2
[description] => cdde38b2b13ee808ce5423cdf8d14bab
[path] => 0001.0001.0001.0001
)
[3] => Array
(
[id] => 4
[person_id] => 1
[description] => 6b837ce73896fbd080bd0b93837ad537
[path] => 0001.0001.0001.0002
)
[4] => Array
(
[id] => 5
[person_id] => 5
[description] => e5694a273ed95d36934cff48cd8fb12a
[path] => 0001.0001.0001.0003
)
[5] => Array
(
[id] => 6
[person_id] => 6
[description] => 15b2ad371934352c0b16c5c1585f3d7d
[path] => 0001.0002
)
[6] => Array
(
[id] => 7
[person_id] => 6
[description] => 2d1595f0c4ec527c601f404c7a272407
[path] => 0001.0002.0001
)
[7] => Array
(
[id] => 8
[person_id] => 6
[description] => 8286c3bbe511d866e4e2ef4321a429d7
[path] => 0001.0003
)
[8] => Array
(
[id] => 9
[person_id] => 8
[description] => dc455dd484220176d1d2198f84bdbc90
[path] => 0001.0003.0001
)
[9] => Array
(
[id] => 10
[person_id] => 9
[description] => b8a8f41b224f900f6f5de0e591ba13b8
[path] => 0001.0003.0002
)
[10] => Array
(
[id] => 11
[person_id] => 11
[description] => ef132c4663961ff09ca61f40eec72697
[path] => 0001.0003.0002.0001
)
I am trying to merge 2 arrays a single array on a multidimensional array where a given key-value = a value
the first array looks like this:
Array
(
[0] => Array
(
[id] => 4
[subcategories] => Array
(
[0] => Array
(
[id] => 5
[category_order] => 0
[parent_id] => 4
[name] => Audio Equipment
)
[1] => Array
(
[id] => 6
[category_order] => 0
[parent_id] => 4
[name] => Home Entertainment
)
[2] => Array
(
[id] => 7
[category_order] => 0
[parent_id] => 4
[name] => Photography
)
[3] => Array
(
[id] => 8
[category_order] => 0
[parent_id] => 4
[name] => Portable Audio
)
[4] => Array
(
[id] => 9
[category_order] => 0
[parent_id] => 4
[name] => Televisions
)
)
)
)
and the second like this:
Array
(
[0] => Array
(
[id] => 10
[parent_id] => 5
[name] => Amplifiers & Receivers
)
[1] => Array
(
[id] => 11
[parent_id] => 5
[name] => Audio Systems
)
[2] => Array
(
[id] => 12
[parent_id] => 5
[name] => Cassette Decks
)
[3] => Array
(
[id] => 13
[parent_id] => 5
[name] => CD Players
)
[4] => Array
(
[id] => 14
[parent_id] => 5
[name] => Radios
)
[5] => Array
(
[id] => 15
[parent_id] => 5
[name] => HiFi Speakers
)
)
What I want to do is add each of the second arrays to a sub array of the first multidimensional array where the parent_id of the second array = the id of the subcategories array of the first array so it will look like this:
array
(
[0]=> Array
(
[id] => 4
[subcategories] => Array
(
[0] => Array
(
[id] => 5
[category_order] => 0
[parent_id] => 4
[name] => Audio Equipment
[subsubcategories] = array
(
[id] => 10
[parent_id] => 5
[name] => Amplifiers & Receivers
)
)
Something like this should work just rename the array names because you didn't provide them. But I think you'll get the idea :) Mainly you loop through all subcategories with foreach loop or another you'll get the parent id and can access the main array with that parent id and save the sub categories info in there.
foreach( $sub_array as $item ) {
$main_array[ category_id ][ $item[ 'parent_id' ] ][ 'subsubcategories' ] = $item;
}
I have the following array structure:
[parents] => Array
(
[0] => Array
(
[id] => 1
[user_id] => 1
[created] => 2014-11-09 13:47:37
[content] => This is a test discussion
[status] => 1
[parent] => 0
[project_id] => 1
)
[1] => Array
(
[id] => 4
[user_id] => 1
[created] => 2014-11-09 13:52:02
[content] => 456789
[status] => 1
[parent] => 0
[project_id] => 1
)
)
[children] => Array
(
[0] => Array
(
[id] => 2
[user_id] => 1
[created] => 2014-11-09 13:47:53
[content] => This is a test reply....
[status] => 1
[parent] => 1
[project_id] => 1
)
[1] => Array
(
[id] => 3
[user_id] => 1
[created] => 2014-11-09 13:48:13
[content] => This is a test reply....!!!
[status] => 1
[parent] => 1
[project_id] => 1
)
[2] => Array
(
[id] => 5
[user_id] => 1
[created] => 2014-11-09 13:52:17
[content] => 8765432
[status] => 1
[parent] => 4
[project_id] => 1
)
)
I would like to merge them into a parent/child relationship so it looks like follows:
[parents] => Array
(
[0] => Array
(
[id] => 1
[user_id] => 1
[created] => 2014-11-09 13:47:37
[content] => This is a test discussion
[status] => 1
[parent] => 0
[project_id] => 1
[children] => Array
(
[0] => Array
(
[id] => 2
[user_id] => 1
[created] => 2014-11-09 13:47:53
[content] => This is a test reply....
[status] => 1
[parent] => 1
[project_id] => 1
)
[1] => Array
(
[id] => 3
[user_id] => 1
[created] => 2014-11-09 13:48:13
[content] => This is a test reply....!!!
[status] => 1
[parent] => 1
[project_id] => 1
)
)
)
)
How could I go about doing that with PHP?
Assuming that all ids are unique in your parents array, you can do this:
// build a new array using parent's ID values as the key,
// to simplify searching for parent IDs.
foreach ($parents as $key => $value){
$merged[$value['id']] = $value;
}
foreach ($children as $child){
$parentID = $child['parent'];
if (array_key_exists( $parentID, $merged )) {
// add the child array to its parent's ['children'] value
$merged[$parentID]['children'][] = $child;
}
}
In the resulting array $merged, the key of each parent item will be set to its id, and all children will be nested under their corresponding parents.
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>';?>