search array and find depedent items - php

I have a tree like below, which contains the nth level permission.
Landing Page Footer Archive
|| || ||
|| || ||
Current ========== Pipeline=== BHI Phonebook==Password Keeper Edit==View==Delete
|| ||
Open Forms Open Forms
|| ||
Blog Request Blog Request
|| ||
Add == Edit Add == Edit
Sample Array
$list = Array
(
[0] => Array
(
[parent] => 0
[item_level] => 1
[id] => 1
[display_name] => Landing Page
)
[1] => Array
(
[parent] => 1
[item_level] => 2
[id] => 2
[display_name] => Current
)
[2] => Array
(
[parent] => 2
[item_level] => 3
[id] => 5
[display_name] => Open Forms
)
[3] => Array
(
[parent] => 5
[item_level] => 4
[id] => 9
[display_name] => Blog Request
)
[4] => Array
(
[parent] => 9
[item_level] => 5
[id] => 10
[display_name] => Add
)
[5] => Array
(
[parent] => 9
[item_level] => 5
[id] => 11
[display_name] => Update
)
[6] => Array
(
[parent] => 1
[item_level] => 2
[id] => 3
[display_name] => Pipeline
)
[7] => Array
(
[parent] => 3
[item_level] => 3
[id] => 6
[display_name] => Open Forms
)
[8] => Array
(
[parent] => 6
[item_level] => 4
[id] => 12
[display_name] => Blog Request
)
[9] => Array
(
[parent] => 12
[item_level] => 5
[id] => 13
[display_name] => Add
)
[10] => Array
(
[parent] => 12
[item_level] => 5
[id] => 14
[display_name] => Update
)
[11] => Array
(
[parent] => 1
[item_level] => 2
[id] => 4
[display_name] => BHI
)
[12] => Array
(
[parent] => 1
[item_level] => 2
[id] => 7
[display_name] => Add Property
)
[13] => Array
(
[parent] => 1
[item_level] => 2
[id] => 8
[display_name] => Show Child Properties
)
[14] => Array
(
[parent] => 0
[item_level] => 1
[id] => 15
[display_name] => Footer
)
[15] => Array
(
[parent] => 15
[item_level] => 2
[id] => 16
[display_name] => Phonebook
)
[16] => Array
(
[parent] => 15
[item_level] => 2
[id] => 17
[display_name] => Password Keeper
)
[17] => Array
(
[parent] => 0
[item_level] => 1
[id] => 18
[display_name] => Archive
)
[18] => Array
(
[parent] => 18
[item_level] => 2
[id] => 19
[display_name] => Edit
)
[19] => Array
(
[parent] => 18
[item_level] => 2
[id] => 20
[display_name] => View
)
[20] => Array
(
[parent] => 18
[item_level] => 2
[id] => 21
[display_name] => Delete
)
)
Expected Output
//Here Key is the value of "id" attribute
$final_output = array(
1 => array(
2=>array(5=>array(9=>array(10,11))),
3=>array(6=>array(12=>array(13,14))),
4,//As it doesnt have any child
7,//As it doesnt have any child
8 //As it doesnt have any child
),
15 => array(
16, //As it doesnt have any child
17 ////As it doesnt have any child
),
18 => array(
19, //As it doesnt have any child
20, ////As it doesnt have any child
21 ////As it doesnt have any child
),
);
here, 1: Landing Page ,15 : Footer ,18 : Archive (its "Id" attribute) etc.
After above array i will have to do a nth level loop.
like:
foreach($final_output as $res)
{
if(is_array($res))
{
//Then do something, i am not sure how this achivable
}
}
What i have done so far, but no success.
//To find out the how manhy levels are in array
$unique_level = array_unique(array_map(function ($i) { return $i['item_level']; }, $list));
$parent_data = $level_data = array();
foreach($list as $key=>$res)
{
if($res['parent']==0)
{
if(!array_key_exists($res['id'],$parent_data)) // First insert all unique parent at level 1
{
$parent_data[$res['id']] = $res;
}
}
$level_data[$res['parent']][] = array(
'display_name' => $res['display_name'],
'id' => $res['id'],
'parent' => $res['parent'],
);
}
Ready to use json
[{"parent":"0","item_level":"1","id":"1","display_name":"Landing Page"},{"parent":"1","item_level":"2","id":"2","display_name":"Current"},{"parent":"2","item_level":"3","id":"5","display_name":"Open Forms"},{"parent":"5","item_level":"4","id":"9","display_name":"Blog Request"},{"parent":"9","item_level":"5","id":"10","display_name":"Add"},{"parent":"9","item_level":"5","id":"11","display_name":"Update"},{"parent":"1","item_level":"2","id":"3","display_name":"Pipeline"},{"parent":"3","item_level":"3","id":"6","display_name":"Open Forms"},{"parent":"6","item_level":"4","id":"12","display_name":"Blog Request"},{"parent":"12","item_level":"5","id":"13","display_name":"Add"},{"parent":"12","item_level":"5","id":"14","display_name":"Update"},{"parent":"1","item_level":"2","id":"4","display_name":"BHI"},{"parent":"1","item_level":"2","id":"7","display_name":"Add Property"},{"parent":"1","item_level":"2","id":"8","display_name":"Show Child Properties"},{"parent":"0","item_level":"1","id":"15","display_name":"Footer"},{"parent":"15","item_level":"2","id":"16","display_name":"Phonebook"},{"parent":"15","item_level":"2","id":"17","display_name":"Password Keeper"},{"parent":"0","item_level":"1","id":"18","display_name":"Archive"},{"parent":"18","item_level":"2","id":"19","display_name":"Edit"},{"parent":"18","item_level":"2","id":"20","display_name":"View"},{"parent":"18","item_level":"2","id":"21","display_name":"Delete"}]

The nearest I've got is...
$list = json_decode($data, true);
$baseList = array_combine(array_column($list,'id'), $list);
$hierarchy = [];
foreach ( $baseList as $newItem ) {
$parent = $newItem['parent'];
$hierarchy[$parent][] = $newItem['id'];
}
function buildTree ( $startNode, $hierarchy) {
$newLevel = [];
foreach ( $startNode as $children ) {
if ( isset($hierarchy[$children])) {
$newLevel[$children] = buildTree($hierarchy[$children], $hierarchy);
}
else {
$newLevel[$children] = $children;
}
}
return $newLevel;
}
$finalOutput = buildTree ( $hierarchy[0], $hierarchy );
print_r($finalOutput);
The problem you have is where you have elements like...
1 => array(
2=>array(5=>array(9=>array(10,11))),
3=>array(6=>array(12=>array(13,14))),
4,//As it doesnt have any child
7,//As it doesnt have any child
8 //As it doesnt have any child
),
As you almost have elements 4, 7 and 8 which don't have a key - all elements in an array have a key, this isn't going to work.
The output which I've done is...
Array
(
[1] => Array
(
[2] => Array
(
[5] => Array
(
[9] => Array
(
[10] => 10
[11] => 11
)
)
)
[3] => Array
(
[6] => Array
(
[12] => Array
(
[13] => 13
[14] => 14
)
)
)
[4] => 4
[7] => 7
[8] => 8
)
[15] => Array
(
[16] => 16
[17] => 17
)
[18] => Array
[19] => 19
[20] => 20
[21] => 21
)
) [19] => 19
[20] => 20
[21] => 21
)
)
Which as you can see, has element 4 having a value of 4.

$list = Array
(
0 => Array
(
'parent' => 0,
'item_level' => 1,
'id' => 1,
'display_name' => 'Landing Page'
),
1 => Array
(
'parent' => 1,
'item_level' => 2,
'id' => 2,
'display_name' => 'Current'
),
2 => Array
(
'parent' => 2,
'item_level' => 3,
'id' => 5,
'display_name' => 'Open Forms'
),
3 => Array
(
'parent' => 5,
'item_level' => 4,
'id' => 9,
'display_name' => 'Blog Request'
),
4 => Array
(
'parent' => 9,
'item_level' => 5,
'id' => 10,
'display_name' => 'Add'
),
5 => Array
(
'parent' => 9,
'item_level' => 5,
'id' => 11,
'display_name' => 'Update'
),
6 => Array
(
'parent' => 1,
'item_level' => 2,
'id' => 3,
'display_name' => 'Pipeline'
),
7 => Array
(
'parent' => 3,
'item_level' => 3,
'id' => 6,
'display_name' => 'Open Forms'
),
8 => Array
(
'parent' => 6,
'item_level' => 4,
'id' => 12,
'display_name' => 'Blog Request'
),
9 => Array
(
'parent' => 12,
'item_level' => 5,
'id' => 13,
'display_name' => 'Add'
),
10 => Array
(
'parent' => 12,
'item_level' => 5,
'id' => 14,
'display_name' => 'Update'
),
11 => Array
(
'parent' => 1,
'item_level' => 2,
'id' => 4,
'display_name' => 'BHI'
),
12 => Array
(
'parent' => 1,
'item_level' => 2,
'id' => 7,
'display_name' => 'Add Property'
),
13 => Array
(
'parent' => 1,
'item_level' => 2,
'id' => 8,
'display_name' => 'Show Child Properties'
),
14 => Array
(
'parent' => 0,
'item_level' => 1,
'id' => 15,
'display_name' => 'Footer'
),
15 => Array
(
'parent' => 15,
'item_level' => 2,
'id' => 16,
'display_name' => 'Phonebook'
),
16 => Array
(
'parent' => 15,
'item_level' => 2,
'id' => 17,
'display_name' => 'Password Keeper'
),
17 => Array
(
'parent' => 0,
'item_level' => 1,
'id' => 18,
'display_name' => 'Archive'
),
18 => Array
(
'parent' => 18,
'item_level' => 2,
'id' => 19,
'display_name' => 'Edit'
),
19 => Array
(
'parent' => 18,
'item_level' => 2,
'id' => 20,
'display_name' => 'View'
),
20 => Array
(
'parent' => 18,
'item_level' => 2,
'id' => 21,
'display_name' => 'Delete'
),
);
Hi, try this code:
$results=array();
$parent1='';$parent=$parent2='';
foreach ($list as $key => $value) {
if($value['parent']==0)
{
$results[$value['id']]=$value;
$id=$value["id"];
}
if($id==$value['parent']&&$id!="")
{
$results[$id][$value['id']]=$value;
$parent=$value['id'];
}
if($parent==$value['parent']&&$parent!="")
{
$results[$id][$parent][$value['id']]=$value;
$parent1=$value['id'];
}
if($parent1==$value['parent']&&$parent1!="")
{
$results[$id][$parent][$parent1][$value['id']]=$value;
$parent2=$value['id'];
}
if($parent2==$value['parent']&&$parent2!="")
{
$results[$id][$parent][$parent1][$parent2][$value['id']]=$value;
$parent3=$value['id'];
}
}
echo '<pre>';
print_r($results);
echo '</pre>';

Related

How to shuffle the array index of same grade on each page refresh?

**e.g grade = 8, id = 724 but index can be 0 to 2 randomly, grade=6, id=899 index can be any between 3 to 6 on page refresh and for other grade index should be in same manner. I need every time when the page gets reload the index value of same grade should be reshuffle within it's own index value **
$arrval = Array (
Array ( 'id' => 19, 'grade' => 5, 'county' => 219 ),
Array ( 'id' => 20, 'grade' => 5, 'county' => 219 ),
Array ( 'id' => 21, 'grade' => 5, 'county' => 219 ),
Array ( 'id' => 22, 'grade' => 5, 'county' => 219 ),
Array ( 'id' => 899, 'grade' => 6, 'county' => 10 ),
Array ( 'id' => 898, 'grade' => 6, 'county' => 10 ),
Array ( 'id' => 897, 'grade' => 6, 'county' => 10 ),
Array ( 'id' => 896, 'grade' => 6, 'county' => 10 ),
Array ( 'id' => 895, 'grade' => 8, 'county' => 10 ),
Array ( 'id' => 894, 'grade' => 8, 'county' => 10 ),
Array ( 'id' => 724, 'grade' => 8, 'county' => 3 )
);
foreach ($arrval as $key => $row)
{
$grade[$key] = $row['grade'];
}
array_multisort($grade, SORT_DESC, $arrval);
echo '<pre>';
print_r($arrval);exit;
Array
(
[0] => Array
(
[id] => 724
[grade] => 8
[county] => 3
)
[1] => Array
(
[id] => 894
[grade] => 8
[county] => 10
)
[2] => Array
(
[id] => 895
[grade] => 8
[county] => 10
)
[3] => Array
(
[id] => 896
[grade] => 6
[county] => 10
)
[4] => Array
(
[id] => 897
[grade] => 6
[county] => 10
)
[5] => Array
(
[id] => 898
[grade] => 6
[county] => 10
)
[6] => Array
(
[id] => 899
[grade] => 6
[county] => 10
)
[7] => Array
(
[id] => 19
[grade] => 5
[county] => 219
)
[8] => Array
(
[id] => 20
[grade] => 5
[county] => 219
)
[9] => Array
(
[id] => 21
[grade] => 5
[county] => 219
)
[10] => Array
(
[id] => 22
[grade] => 5
[county] => 219
)
)
I would shuffle the results before ordering them by grade.
<?php
// Comparison function
function cmp($a, $b) {
if ($a['grade'] == $b['grade']) {
return 0;
}
return ($a['grade'] < $b['grade']) ? -1 : 1;
}
$arrval = Array (
Array ( 'id' => 19, 'grade' => 5, 'county' => 219 ),
Array ( 'id' => 20, 'grade' => 5, 'county' => 219 ),
Array ( 'id' => 21, 'grade' => 5, 'county' => 219 ),
Array ( 'id' => 22, 'grade' => 5, 'county' => 219 ),
Array ( 'id' => 899, 'grade' => 6, 'county' => 10 ),
Array ( 'id' => 898, 'grade' => 6, 'county' => 10 ),
Array ( 'id' => 897, 'grade' => 6, 'county' => 10 ),
Array ( 'id' => 896, 'grade' => 6, 'county' => 10 ),
Array ( 'id' => 895, 'grade' => 8, 'county' => 10 ),
Array ( 'id' => 894, 'grade' => 8, 'county' => 10 ),
Array ( 'id' => 724, 'grade' => 8, 'county' => 3 )
);
shuffle($arrval);
usort($arrval, "cmp");
echo '<pre>';
print_r($arrval);
exit;

Get 3 element per category From array in PHP

I have 2 arrays category and product_to_category (Structure shown below)
[category] => Array
(
[0] => 299
[2] => 342
[3] => 134
[4] => 333
[5] => 347
[9] => 296
)
And
[product_to_category] => Array
(
[0] => Array
(
[product_id] => 23895
[category_id] => 296
[relevance] => 77.73432159423828
)
[1] => Array
(
[product_id] => 17218
[category_id] => 296
[relevance] => 77.73432159423828
)
[2] => Array
(
[product_id] => 23896
[category_id] => 347
[relevance] => 77.73432159423828
)
....So On (5000+ records)
i have almost 5000+ records in product_to_category array, But i want to pick only 3 products from P2C array per Category. For Example category_id=299 so 3 products of 299 will be added to my new array and same for other categories.
Is there any possible way to do that .
Try this query to fetch record you wanted,
select p2c.product_id, p2c.category_id, p2c.relevance
from category c
LEFT JOIN product_to_category p2c ON p2c.category_id = category.category_id
GROUP BY p2c.product_id, p2c.category_id
HAVING count(*) <= 3
ORDER BY category_id, product_id
You can refer table category to category table and product_to_category to your table name in database.
$category = [0 => 299, 2 => 342, 3 => 134, 4 => 333, 5 => 347, 9 => 296];
$product_to_category = Array
(
0 => Array
(
'product_id' => 23895,
'category_id' => 296,
'relevance' => 77.73432159423828
),
1 => Array
(
'product_id' => 17218,
'category_id' => 296,
'relevance' => 77.73432159423828
),
2 => Array
(
'product_id' => 23896,
'category_id' => 347,
'relevance' => 77.73432159423828
),
3 => Array
(
'product_id' => 23897,
'category_id' => 296,
'relevance' => 77.73432159423828
),
4 => Array
(
'product_id' => 23899,
'category_id' => 296,
'relevance' => 77.73432159423828
),
5 => Array
(
'product_id' => 23894,
'category_id' => 347,
'relevance' => 77.73432159423828
),
6 => Array
(
'product_id' => 23892,
'category_id' => 347,
'relevance' => 77.73432159423828
),
7 => Array
(
'product_id' => 23833,
'category_id' => 347,
'relevance' => 77.73432159423828
)
);
$new_product_to_category = [];
foreach ($category as $categories) {
$count_category_ids = [];
$i=0;
foreach ($product_to_category as $product_to_categories){
if ($categories == $product_to_categories['category_id']){
$count_category_ids[] = $product_to_categories['category_id'];
if (count($count_category_ids) < 4) {
$new_product_to_category[$i]['product_id'] = $product_to_categories['product_id'];
$new_product_to_category[$i]['category_id'] = $product_to_categories['category_id'];
$new_product_to_category[$i]['relevance'] = $product_to_categories['relevance'];
}
}
$i++;
}
}
echo "<pre>";
print_r($new_product_to_category);
Output shows like this :
Array
(
[2] => Array
(
[product_id] => 23896
[category_id] => 347
[relevance] => 77.734321594238
)
[5] => Array
(
[product_id] => 23894
[category_id] => 347
[relevance] => 77.734321594238
)
[6] => Array
(
[product_id] => 23892
[category_id] => 347
[relevance] => 77.734321594238
)
[0] => Array
(
[product_id] => 23895
[category_id] => 296
[relevance] => 77.734321594238
)
[1] => Array
(
[product_id] => 17218
[category_id] => 296
[relevance] => 77.734321594238
)
[3] => Array
(
[product_id] => 23897
[category_id] => 296
[relevance] => 77.734321594238
)
)
$categories = array(6, 5, 3, 4, 2, 1);
$products = array(
array('product_id' => 231, 'category_id' => 1, 'relevance' => 321),
array('product_id' => 232, 'category_id' => 4, 'relevance' => 322),
array('product_id' => 233, 'category_id' => 2, 'relevance' => 323),
array('product_id' => 234, 'category_id' => 4, 'relevance' => 324),
array('product_id' => 235, 'category_id' => 4, 'relevance' => 325),
array('product_id' => 236, 'category_id' => 2, 'relevance' => 326),
array('product_id' => 237, 'category_id' => 1, 'relevance' => 327),
array('product_id' => 238, 'category_id' => 4, 'relevance' => 328),
array('product_id' => 239, 'category_id' => 1, 'relevance' => 329),
array('product_id' => 240, 'category_id' => 1, 'relevance' => 330)
);
$categoryWiseProduct = array();
foreach ($products as $p) {
foreach ($categories as $c) {
if ($c == $p['category_id']) {
if (isset($categoryWiseProduct[$c]) && count($categoryWiseProduct[$c]) > 2) {
break;
}
$categoryWiseProduct[$c][] = $p;
}
}
}
echo'<pre>';print_r($categoryWiseProduct);die;
Output will be:
Array
(
[1] => Array
(
[0] => Array
(
[product_id] => 231
[category_id] => 1
[relevance] => 321
)
[1] => Array
(
[product_id] => 237
[category_id] => 1
[relevance] => 327
)
[2] => Array
(
[product_id] => 239
[category_id] => 1
[relevance] => 329
)
)
[4] => Array
(
[0] => Array
(
[product_id] => 232
[category_id] => 4
[relevance] => 322
)
[1] => Array
(
[product_id] => 234
[category_id] => 4
[relevance] => 324
)
[2] => Array
(
[product_id] => 235
[category_id] => 4
[relevance] => 325
)
)
[2] => Array
(
[0] => Array
(
[product_id] => 233
[category_id] => 2
[relevance] => 323
)
[1] => Array
(
[product_id] => 236
[category_id] => 2
[relevance] => 326
)
)
)
try this code
$collection = array();
foreach($arr as $key => $value){
if(
isset($collection[$value['category_id']])
&&
count($collection[$value['category_id']]) >= 3
) continue;
$collection[$value['product_id']][] = $value['product_id'];
}
In this way, you will get 3 products from each category.
You can add more condition if you need, like check the product id you can use inside if condition
&& in_array($product_id, $collection[$value['category_id']])

PHP traverse category subcategory tree array into a custom 2D array

I have an parent child array similar to this:
Array
(
[0] => Array
(
[category_id] => 1
[category_name] => Home & Garden
[parent_id] => 0
[level] => 1
)
[1] => Array
(
[category_id] => 2
[category_name] => Kitchen & Dining
[parent_id] => 1
[level] => 2
)
[2] => Array
(
[category_id] => 3
[category_name] => Food & Beverage Carriers
[parent_id] => 2
[level] => 3
)
[3] => Array
(
[category_id] => 4
[category_name] => Flasks
[parent_id] => 3
[level] => 4
)
[4] => Array
(
[category_id] => 5
[category_name] => Clothing & Accessories
[parent_id] => 0
[level] => 1
)
[5] => Array
(
[category_id] => 6
[category_name] => Clothing
[parent_id] => 5
[level] => 2
)
[6] => Array
(
[category_id] => 7
[category_name] => Shorts
[parent_id] => 6
[level] => 3
)
[7] => Array
(
[category_id] => 8
[category_name] => Shirts & Tops
[parent_id] => 6
[level] => 3
)
[8] => Array
(
[category_id] => 9
[category_name] => Jumpers & Cardigans
[parent_id] => 8
[level] => 4
)
[9] => Array
(
[category_id] => 10
[category_name] => T-Shirts
[parent_id] => 8
[level] => 4
)
[10] => Array
(
[category_id] => 11
[category_name] => Skirts
[parent_id] => 6
[level] => 3
)
[11] => Array
(
[category_id] => 12
[category_name] => Shirts & Blouses
[parent_id] => 8
[level] => 4
)
[12] => Array
(
[category_id] => 13
[category_name] => Trousers & Jeans
[parent_id] => 6
[level] => 3
)
[13] => Array
(
[category_id] => 14
[category_name] => Chinos
[parent_id] => 13
[level] => 4
)
[14] => Array
(
[category_id] => 15
[category_name] => Tops
[parent_id] => 6
[level] => 3
)
[15] => Array
(
[category_id] => 16
[category_name] => Sweatshirts
[parent_id] => 8
[level] => 4
)
[16] => Array
(
[category_id] => 17
[category_name] => Clothing Accessories
[parent_id] => 5
[level] => 2
)
[17] => Array
(
[category_id] => 18
[category_name] => Hats
[parent_id] => 17
[level] => 3
)
[18] => Array
(
[category_id] => 19
[category_name] => Shirts
[parent_id] => 15
[level] => 4
)
[19] => Array
(
[category_id] => 20
[category_name] => Cargo Trousers
[parent_id] => 13
[level] => 4
)
[20] => Array
(
[category_id] => 21
[category_name] => Underwear & Socks
[parent_id] => 6
[level] => 3
)
[21] => Array
(
[category_id] => 22
[category_name] => Socks
[parent_id] => 21
[level] => 4
)
[22] => Array
(
[category_id] => 23
[category_name] => Polo Shirts
[parent_id] => 8
[level] => 4
)
[23] => Array
(
[category_id] => 24
[category_name] => Apparel & Accessories
[parent_id] => 0
[level] => 1
)
[24] => Array
(
[category_id] => 25
[category_name] => Clothing Accessories
[parent_id] => 24
[level] => 2
)
[25] => Array
(
[category_id] => 26
[category_name] => Belts
[parent_id] => 25
[level] => 3
)
[26] => Array
(
[category_id] => 27
[category_name] => Gloves & Mittens
[parent_id] => 17
[level] => 3
)
[27] => Array
(
[category_id] => 28
[category_name] => Gloves
[parent_id] => 27
[level] => 4
)
[28] => Array
(
[category_id] => 29
[category_name] => Shoes
[parent_id] => 24
[level] => 2
)
[29] => Array
(
[category_id] => 30
[category_name] => Sandals
[parent_id] => 29
[level] => 3
)
[30] => Array
(
[category_id] => 31
[category_name] => Thongs & Flip-Flops
[parent_id] => 30
[level] => 4
)
[31] => Array
(
[category_id] => 32
[category_name] => Luggage & Bags
[parent_id] => 0
[level] => 1
)
[32] => Array
(
[category_id] => 33
[category_name] => Messenger Bags
[parent_id] => 32
[level] => 2
)
[33] => Array
(
[category_id] => 34
[category_name] => Jeans
[parent_id] => 13
[level] => 4
)
[34] => Array
(
[category_id] => 35
[category_name] => Clothing
[parent_id] => 24
[level] => 2
)
[35] => Array
(
[category_id] => 36
[category_name] => Pants
[parent_id] => 35
[level] => 3
)
[36] => Array
(
[category_id] => 37
[category_name] => Jeans
[parent_id] => 36
[level] => 4
)
[37] => Array
(
[category_id] => 38
[category_name] => Handbags, Wallets & Cases
[parent_id] => 5
[level] => 2
)
[38] => Array
(
[category_id] => 39
[category_name] => Wallets & Money Clips
[parent_id] => 38
[level] => 3
)
[39] => Array
(
[category_id] => 40
[category_name] => Baseball Hats
[parent_id] => 18
[level] => 4
)
[40] => Array
(
[category_id] => 41
[category_name] => Handbag & Wallet Accessories
[parent_id] => 5
[level] => 2
)
[41] => Array
(
[category_id] => 42
[category_name] => Key Chains
[parent_id] => 41
[level] => 3
)
[42] => Array
(
[category_id] => 43
[category_name] => Shoes
[parent_id] => 5
[level] => 2
)
[43] => Array
(
[category_id] => 44
[category_name] => Boots
[parent_id] => 43
[level] => 3
)
[44] => Array
(
[category_id] => 45
[category_name] => Waterproof Boots & Wellingtons
[parent_id] => 44
[level] => 4
)
[45] => Array
(
[category_id] => 46
[category_name] => Barware
[parent_id] => 2
[level] => 3
)
[46] => Array
(
[category_id] => 47
[category_name] => Drink Shakers & Tools
[parent_id] => 46
[level] => 4
)
[47] => Array
(
[category_id] => 48
[category_name] => Bottle Openers
[parent_id] => 47
[level] => 5
)
[48] => Array
(
[category_id] => 49
[category_name] => Tableware
[parent_id] => 2
[level] => 3
)
[49] => Array
(
[category_id] => 50
[category_name] => Drinkware
[parent_id] => 49
[level] => 4
)
[50] => Array
(
[category_id] => 51
[category_name] => Mugs
[parent_id] => 50
[level] => 5
)
[51] => Array
(
[category_id] => 52
[category_name] => Outerwear
[parent_id] => 6
[level] => 3
)
[52] => Array
(
[category_id] => 53
[category_name] => Coats & Jackets
[parent_id] => 52
[level] => 4
)
[53] => Array
(
[category_id] => 54
[category_name] => Puffer jackets
[parent_id] => 53
[level] => 5
)
[54] => Array
(
[category_id] => 55
[category_name] => Shirts & Tops
[parent_id] => 35
[level] => 3
)
[55] => Array
(
[category_id] => 56
[category_name] => T-Shirts
[parent_id] => 55
[level] => 4
)
[56] => Array
(
[category_id] => 57
[category_name] => T-Shirts
[parent_id] => 15
[level] => 4
)
[57] => Array
(
[category_id] => 58
[category_name] => Sandals
[parent_id] => 43
[level] => 3
)
[58] => Array
(
[category_id] => 59
[category_name] => Thongs & Flip Flops
[parent_id] => 58
[level] => 4
)
[59] => Array
(
[category_id] => 60
[category_name] => Belts
[parent_id] => 17
[level] => 3
)
[60] => Array
(
[category_id] => 61
[category_name] => Cargo Shorts
[parent_id] => 7
[level] => 4
)
[61] => Array
(
[category_id] => 62
[category_name] => Jumpers & Cardigans
[parent_id] => 15
[level] => 4
)
[62] => Array
(
[category_id] => 63
[category_name] => Cosmetic & Toiletry Bags
[parent_id] => 32
[level] => 2
)
[63] => Array
(
[category_id] => 64
[category_name] => Activewear
[parent_id] => 35
[level] => 3
)
[64] => Array
(
[category_id] => 65
[category_name] => Sweatshirts
[parent_id] => 64
[level] => 4
)
[65] => Array
(
[category_id] => 66
[category_name] => Sun Hats
[parent_id] => 18
[level] => 4
)
[66] => Array
(
[category_id] => 67
[category_name] => Fleece Jackets
[parent_id] => 53
[level] => 5
)
[67] => Array
(
[category_id] => 68
[category_name] => Gilets
[parent_id] => 52
[level] => 4
)
[68] => Array
(
[category_id] => 69
[category_name] => Handbags
[parent_id] => 38
[level] => 3
)
[69] => Array
(
[category_id] => 70
[category_name] => Tote Bags & Shoppers
[parent_id] => 69
[level] => 4
)
[70] => Array
(
[category_id] => 71
[category_name] => Outdoor Shoes
[parent_id] => 29
[level] => 3
)
[71] => Array
(
[category_id] => 72
[category_name] => Water Shoes
[parent_id] => 71
[level] => 4
)
[72] => Array
(
[category_id] => 73
[category_name] => Backpacks
[parent_id] => 32
[level] => 2
)
[73] => Array
(
[category_id] => 74
[category_name] => Shirts & Blouses
[parent_id] => 55
[level] => 4
)
[74] => Array
(
[category_id] => 75
[category_name] => Scarves & Snoods
[parent_id] => 17
[level] => 3
)
[75] => Array
(
[category_id] => 76
[category_name] => Suitcases
[parent_id] => 32
[level] => 2
)
[76] => Array
(
[category_id] => 77
[category_name] => Hand Luggage
[parent_id] => 76
[level] => 3
)
[77] => Array
(
[category_id] => 78
[category_name] => Vests & Tank Tops
[parent_id] => 8
[level] => 4
)
[78] => Array
(
[category_id] => 79
[category_name] => Parasols & Umbrellas
[parent_id] => 1
[level] => 2
)
[79] => Array
(
[category_id] => 80
[category_name] => Carry-On Luggage
[parent_id] => 76
[level] => 3
)
[80] => Array
(
[category_id] => 81
[category_name] => Dresses
[parent_id] => 6
[level] => 3
)
[81] => Array
(
[category_id] => 82
[category_name] => Day Dresses
[parent_id] => 81
[level] => 4
)
[82] => Array
(
[category_id] => 83
[category_name] => Cargo Pants
[parent_id] => 13
[level] => 4
)
[83] => Array
(
[category_id] => 84
[category_name] => Hand Luggage
[parent_id] => 32
[level] => 2
)
[84] => Array
(
[category_id] => 85
[category_name] => Shoulder Bags
[parent_id] => 69
[level] => 4
)
[85] => Array
(
[category_id] => 86
[category_name] => Bermuda Shorts
[parent_id] => 7
[level] => 4
)
[86] => Array
(
[category_id] => 87
[category_name] => Shorts
[parent_id] => 35
[level] => 3
)
[87] => Array
(
[category_id] => 88
[category_name] => Cargo Shorts
[parent_id] => 87
[level] => 4
)
[88] => Array
(
[category_id] => 89
[category_name] => Trousers
[parent_id] => 6
[level] => 3
)
[89] => Array
(
[category_id] => 90
[category_name] => Casual Trousers
[parent_id] => 89
[level] => 4
)
[90] => Array
(
[category_id] => 91
[category_name] => Handbag & Wallet Accessories
[parent_id] => 24
[level] => 2
)
[91] => Array
(
[category_id] => 92
[category_name] => Lanyards
[parent_id] => 91
[level] => 3
)
[92] => Array
(
[category_id] => 93
[category_name] => Duffel Bags
[parent_id] => 32
[level] => 2
)
[93] => Array
(
[category_id] => 94
[category_name] => Polo Shirts
[parent_id] => 15
[level] => 4
)
[94] => Array
(
[category_id] => 95
[category_name] => Skorts
[parent_id] => 6
[level] => 3
)
[95] => Array
(
[category_id] => 96
[category_name] => Casual Jackets
[parent_id] => 53
[level] => 5
)
[96] => Array
(
[category_id] => 97
[category_name] => Leggings
[parent_id] => 89
[level] => 4
)
[97] => Array
(
[category_id] => 98
[category_name] => Canteens
[parent_id] => 3
[level] => 4
)
[98] => Array
(
[category_id] => 99
[category_name] => Caps
[parent_id] => 18
[level] => 4
)
[99] => Array
(
[category_id] => 100
[category_name] => Baseball Hats
[parent_id] => 99
[level] => 5
)
[100] => Array
(
[category_id] => 101
[category_name] => Rain Gear
[parent_id] => 52
[level] => 4
)
[101] => Array
(
[category_id] => 102
[category_name] => Waterproof Coats
[parent_id] => 101
[level] => 5
)
[102] => Array
(
[category_id] => 103
[category_name] => Capes & Ponchos
[parent_id] => 53
[level] => 5
)
[103] => Array
(
[category_id] => 104
[category_name] => Sweatshirts
[parent_id] => 15
[level] => 4
)
[104] => Array
(
[category_id] => 105
[category_name] => Outdoor Shoes
[parent_id] => 43
[level] => 3
)
[105] => Array
(
[category_id] => 106
[category_name] => Water Shoes
[parent_id] => 105
[level] => 4
)
[106] => Array
(
[category_id] => 107
[category_name] => Polo Shirts
[parent_id] => 55
[level] => 4
)
[107] => Array
(
[category_id] => 108
[category_name] => Dresses
[parent_id] => 35
[level] => 3
)
[108] => Array
(
[category_id] => 109
[category_name] => Day Dresses
[parent_id] => 108
[level] => 4
)
[109] => Array
(
[category_id] => 110
[category_name] => Sweaters & Cardigans
[parent_id] => 55
[level] => 4
)
[110] => Array
(
[category_id] => 111
[category_name] => Shopping Totes
[parent_id] => 32
[level] => 2
)
[111] => Array
(
[category_id] => 112
[category_name] => Sweatshirts
[parent_id] => 55
[level] => 4
)
[112] => Array
(
[category_id] => 113
[category_name] => Scarves & Shawls
[parent_id] => 25
[level] => 3
)
[113] => Array
(
[category_id] => 114
[category_name] => Handbags, Wallets & Cases
[parent_id] => 24
[level] => 2
)
[114] => Array
(
[category_id] => 115
[category_name] => Handbags
[parent_id] => 114
[level] => 3
)
[115] => Array
(
[category_id] => 116
[category_name] => Tote Handbags
[parent_id] => 115
[level] => 4
)
[116] => Array
(
[category_id] => 117
[category_name] => Underwear & Socks
[parent_id] => 35
[level] => 3
)
[117] => Array
(
[category_id] => 118
[category_name] => Socks
[parent_id] => 117
[level] => 4
)
[118] => Array
(
[category_id] => 119
[category_name] => Cross-Body Handbags
[parent_id] => 115
[level] => 4
)
[119] => Array
(
[category_id] => 120
[category_name] => Underwear
[parent_id] => 117
[level] => 4
)
[120] => Array
(
[category_id] => 121
[category_name] => Outerwear
[parent_id] => 35
[level] => 3
)
[121] => Array
(
[category_id] => 122
[category_name] => Coats & Jackets
[parent_id] => 121
[level] => 4
)
[122] => Array
(
[category_id] => 123
[category_name] => Camisoles & Tank Tops
[parent_id] => 55
[level] => 4
)
[123] => Array
(
[category_id] => 124
[category_name] => Skirts
[parent_id] => 35
[level] => 3
)
[124] => Array
(
[category_id] => 125
[category_name] => Fleece Jackets
[parent_id] => 122
[level] => 5
)
[125] => Array
(
[category_id] => 126
[category_name] => Khaki & Chino Pants
[parent_id] => 36
[level] => 4
)
[126] => Array
(
[category_id] => 127
[category_name] => Cargo Pants
[parent_id] => 36
[level] => 4
)
[127] => Array
(
[category_id] => 128
[category_name] => Hats
[parent_id] => 25
[level] => 3
)
[128] => Array
(
[category_id] => 129
[category_name] => Beanies
[parent_id] => 128
[level] => 4
)
[129] => Array
(
[category_id] => 130
[category_name] => Gloves & Mittens
[parent_id] => 25
[level] => 3
)
[130] => Array
(
[category_id] => 131
[category_name] => Scarves & Shawls
[parent_id] => 17
[level] => 3
)
[131] => Array
(
[category_id] => 132
[category_name] => Pants
[parent_id] => 6
[level] => 3
)
[132] => Array
(
[category_id] => 133
[category_name] => Khaki & Chino Pants
[parent_id] => 132
[level] => 4
)
[133] => Array
(
[category_id] => 134
[category_name] => Tote Handbags
[parent_id] => 69
[level] => 4
)
[134] => Array
(
[category_id] => 135
[category_name] => Camisoles & Tank Tops
[parent_id] => 8
[level] => 4
)
[135] => Array
(
[category_id] => 136
[category_name] => Jeans
[parent_id] => 132
[level] => 4
)
[136] => Array
(
[category_id] => 137
[category_name] => Sweaters & Cardigans
[parent_id] => 8
[level] => 4
)
[137] => Array
(
[category_id] => 138
[category_name] => Cargo Pants
[parent_id] => 132
[level] => 4
)
[138] => Array
(
[category_id] => 139
[category_name] => Beanies
[parent_id] => 18
[level] => 4
)
[139] => Array
(
[category_id] => 140
[category_name] => Parasols & Rain Umbrellas
[parent_id] => 1
[level] => 2
)
[140] => Array
(
[category_id] => 141
[category_name] => Lanyards
[parent_id] => 41
[level] => 3
)
[141] => Array
(
[category_id] => 142
[category_name] => Cross-Body Handbags
[parent_id] => 69
[level] => 4
)
[142] => Array
(
[category_id] => 143
[category_name] => Khaki & Chino Trousers
[parent_id] => 13
[level] => 4
)
[143] => Array
(
[category_id] => 144
[category_name] => Underwear
[parent_id] => 21
[level] => 4
)
)
How to convert this array similar to this:
Array
(
[0] => Array
(
[0] => Home & Garden
[1] => Kitchen & Dining
[2] => Food & Beverage Carriers
[3] => Flasks
[4] =>
)
[1] => Array
(
[0] =>
[1] =>
[2] =>
[3] => Canteens
[4] =>
)
[2] => Array
(
[0] =>
[1] =>
[2] => Barware
[3] => Drink Shakers & Tools
[4] => Bottle Openers
)
[3] => Array
(
[0] =>
[1] =>
[2] => Tableware
[3] => Drinkware
[4] => Mugs
)
[4] => Array
(
[0] =>
[1] => Parasols & Umbrellas
[2] =>
[3] =>
[4] =>
)
[5] => Array
(
[0] =>
[1] => Parasols & Rain Umbrellas
[2] =>
[3] =>
[4] =>
)
[6] => Array
(
[0] => Clothing & Accessories
[1] => Clothing
[2] => Shorts
[3] => Cargo Shorts
[4] =>
)
)
Parent child representation for reference:
Any Ideas???
Very interesting task. See my solution below:
categories array
$categoriesrRaw = Array
(
'0' => Array
(
'category_id' => 1,
'category_name' => 'Home & Garden',
'parent_id' => 0,
'level' => 1,
),
'1' => Array
(
'category_id' => 2,
'category_name' => 'Kitchen & Dining',
'parent_id' => 1,
'level' => 2,
),
'2' => Array
(
'category_id' => 3,
'category_name' => 'Food & Beverage Carriers',
'parent_id' => 2,
'level' => 3,
),
'3' => Array
(
'category_id' => 4,
'category_name' => 'Flasks',
'parent_id' => 3,
'level' => 4,
),
'4' => Array
(
'category_id' => 5,
'category_name' => 'Clothing & Accessories',
'parent_id' => 0,
'level' => 1,
),
'5' => Array
(
'category_id' => 6,
'category_name' => 'Clothing',
'parent_id' => 5,
'level' => 2,
),
'6' => Array
(
'category_id' => 7,
'category_name' => 'Shorts',
'parent_id' => 6,
'level' => 3,
),
'7' => Array
(
'category_id' => 8,
'category_name' => 'Shirts & Tops',
'parent_id' => 6,
'level' => 3,
),
'8' => Array
(
'category_id' => 9,
'category_name' => 'Jumpers & Cardigans',
'parent_id' => 8,
'level' => 4,
),
'9' => Array
(
'category_id' => 10,
'category_name' => 'T-Shirts',
'parent_id' => 8,
'level' => 4,
),
'10' => Array
(
'category_id' => 11,
'category_name' => 'Skirts',
'parent_id' => 6,
'level' => 3,
),
'11' => Array
(
'category_id' => 12,
'category_name' => 'Shirts & Blouses',
'parent_id' => 8,
'level' => 4,
),
'12' => Array
(
'category_id' => 13,
'category_name' => 'Trousers & Jeans',
'parent_id' => 6,
'level' => 3,
),
'13' => Array
(
'category_id' => 14,
'category_name' => 'Chinos',
'parent_id' => 13,
'level' => 4,
),
'14' => Array
(
'category_id' => 15,
'category_name' => 'Tops',
'parent_id' => 6,
'level' => 3,
),
'15' => Array
(
'category_id' => 16,
'category_name' => 'Sweatshirts',
'parent_id' => 8,
'level' => 4,
),
'16' => Array
(
'category_id' => 17,
'category_name' => 'Clothing Accessories',
'parent_id' => 5,
'level' => 2,
),
'17' => Array
(
'category_id' => 18,
'category_name' => 'Hats',
'parent_id' => 17,
'level' => 3,
),
'18' => Array
(
'category_id' => 19,
'category_name' => 'Shirts',
'parent_id' => 15,
'level' => 4,
),
'19' => Array
(
'category_id' => 20,
'category_name' => 'Cargo Trousers',
'parent_id' => 13,
'level' => 4,
),
'20' => Array
(
'category_id' => 21,
'category_name' => 'Underwear & Socks',
'parent_id' => 6,
'level' => 3,
),
'21' => Array
(
'category_id' => 22,
'category_name' => 'Socks',
'parent_id' => 21,
'level' => 4,
),
'22' => Array
(
'category_id' => 23,
'category_name' => 'Polo Shirts',
'parent_id' => 8,
'level' => 4,
),
'23' => Array
(
'category_id' => 24,
'category_name' => 'Apparel & Accessories',
'parent_id' => 0,
'level' => 1,
),
'24' => Array
(
'category_id' => 25,
'category_name' => 'Clothing Accessories',
'parent_id' => 24,
'level' => 2,
),
'25' => Array
(
'category_id' => 26,
'category_name' => 'Belts',
'parent_id' => 25,
'level' => 3,
),
'26' => Array
(
'category_id' => 27,
'category_name' => 'Gloves & Mittens',
'parent_id' => 17,
'level' => 3,
),
'27' => Array
(
'category_id' => 28,
'category_name' => 'Gloves',
'parent_id' => 27,
'level' => 4,
),
'28' => Array
(
'category_id' => 29,
'category_name' => 'Shoes',
'parent_id' => 24,
'level' => 2,
),
'29' => Array
(
'category_id' => 30,
'category_name' => 'Sandals',
'parent_id' => 29,
'level' => 3,
),
'30' => Array
(
'category_id' => 31,
'category_name' => 'Thongs & Flip-Flops',
'parent_id' => 30,
'level' => 4,
),
'31' => Array
(
'category_id' => 32,
'category_name' => 'Luggage & Bags',
'parent_id' => 0,
'level' => 1,
),
'32' => Array
(
'category_id' => 33,
'category_name' => 'Messenger Bags',
'parent_id' => 32,
'level' => 2,
),
'33' => Array
(
'category_id' => 34,
'category_name' => 'Jeans',
'parent_id' => 13,
'level' => 4,
),
'34' => Array
(
'category_id' => 35,
'category_name' => 'Clothing',
'parent_id' => 24,
'level' => 2,
),
'35' => Array
(
'category_id' => 36,
'category_name' => 'Pants',
'parent_id' => 35,
'level' => 3,
),
'36' => Array
(
'category_id' => 37,
'category_name' => 'Jeans',
'parent_id' => 36,
'level' => 4,
),
'37' => Array
(
'category_id' => 38,
'category_name' => 'Handbags, Wallets & Cases',
'parent_id' => 5,
'level' => 2,
),
'38' => Array
(
'category_id' => 39,
'category_name' => 'Wallets & Money Clips',
'parent_id' => 38,
'level' => 3,
),
'39' => Array
(
'category_id' => 40,
'category_name' => 'Baseball Hats',
'parent_id' => 18,
'level' => 4,
),
'40' => Array
(
'category_id' => 41,
'category_name' => 'Handbag & Wallet Accessories',
'parent_id' => 5,
'level' => 2,
),
'41' => Array
(
'category_id' => 42,
'category_name' => 'Key Chains',
'parent_id' => 41,
'level' => 3,
),
'42' => Array
(
'category_id' => 43,
'category_name' => 'Shoes',
'parent_id' => 5,
'level' => 2,
),
'43' => Array
(
'category_id' => 44,
'category_name' => 'Boots',
'parent_id' => 43,
'level' => 3,
),
'44' => Array
(
'category_id' => 45,
'category_name' => 'Waterproof Boots & Wellingtons',
'parent_id' => 44,
'level' => 4,
),
'45' => Array
(
'category_id' => 46,
'category_name' => 'Barware',
'parent_id' => 2,
'level' => 3,
),
'46' => Array
(
'category_id' => 47,
'category_name' => 'Drink Shakers & Tools',
'parent_id' => 46,
'level' => 4,
),
'47' => Array
(
'category_id' => 48,
'category_name' => 'Bottle Openers',
'parent_id' => 47,
'level' => 5,
),
'48' => Array
(
'category_id' => 49,
'category_name' => 'Tableware',
'parent_id' => 2,
'level' => 3,
),
'49' => Array
(
'category_id' => 50,
'category_name' => 'Drinkware',
'parent_id' => 49,
'level' => 4,
),
'50' => Array
(
'category_id' => 51,
'category_name' => 'Mugs',
'parent_id' => 50,
'level' => 5,
),
'51' => Array
(
'category_id' => 52,
'category_name' => 'Outerwear',
'parent_id' => 6,
'level' => 3,
),
'52' => Array
(
'category_id' => 53,
'category_name' => 'Coats & Jackets',
'parent_id' => 52,
'level' => 4,
),
'53' => Array
(
'category_id' => 54,
'category_name' => 'Puffer jackets',
'parent_id' => 53,
'level' => 5,
),
'54' => Array
(
'category_id' => 55,
'category_name' => 'Shirts & Tops',
'parent_id' => 35,
'level' => 3,
),
'55' => Array
(
'category_id' => 56,
'category_name' => 'T-Shirts',
'parent_id' => 55,
'level' => 4,
),
'56' => Array
(
'category_id' => 57,
'category_name' => 'T-Shirts',
'parent_id' => 15,
'level' => 4,
),
'57' => Array
(
'category_id' => 58,
'category_name' => 'Sandals',
'parent_id' => 43,
'level' => 3,
),
'58' => Array
(
'category_id' => 59,
'category_name' => 'Thongs & Flip Flops',
'parent_id' => 58,
'level' => 4,
),
'59' => Array
(
'category_id' => 60,
'category_name' => 'Belts',
'parent_id' => 17,
'level' => 3,
),
'60' => Array
(
'category_id' => 61,
'category_name' => 'Cargo Shorts',
'parent_id' => 7,
'level' => 4,
),
'61' => Array
(
'category_id' => 62,
'category_name' => 'Jumpers & Cardigans',
'parent_id' => 15,
'level' => 4,
),
'62' => Array
(
'category_id' => 63,
'category_name' => 'Cosmetic & Toiletry Bags',
'parent_id' => 32,
'level' => 2,
),
'63' => Array
(
'category_id' => 64,
'category_name' => 'Activewear',
'parent_id' => 35,
'level' => 3,
),
'64' => Array
(
'category_id' => 65,
'category_name' => 'Sweatshirts',
'parent_id' => 64,
'level' => 4,
),
'65' => Array
(
'category_id' => 66,
'category_name' => 'Sun Hats',
'parent_id' => 18,
'level' => 4,
),
'66' => Array
(
'category_id' => 67,
'category_name' => 'Fleece Jackets',
'parent_id' => 53,
'level' => 5,
),
'67' => Array
(
'category_id' => 68,
'category_name' => 'Gilets',
'parent_id' => 52,
'level' => 4,
),
'68' => Array
(
'category_id' => 69,
'category_name' => 'Handbags',
'parent_id' => 38,
'level' => 3,
),
'69' => Array
(
'category_id' => 70,
'category_name' => 'Tote Bags & Shoppers',
'parent_id' => 69,
'level' => 4,
),
'70' => Array
(
'category_id' => 71,
'category_name' => 'Outdoor Shoes',
'parent_id' => 29,
'level' => 3,
),
'71' => Array
(
'category_id' => 72,
'category_name' => 'Water Shoes',
'parent_id' => 71,
'level' => 4,
),
'72' => Array
(
'category_id' => 73,
'category_name' => 'Backpacks',
'parent_id' => 32,
'level' => 2,
),
'73' => Array
(
'category_id' => 74,
'category_name' => 'Shirts & Blouses',
'parent_id' => 55,
'level' => 4,
),
'74' => Array
(
'category_id' => 75,
'category_name' => 'Scarves & Snoods',
'parent_id' => 17,
'level' => 3,
),
'75' => Array
(
'category_id' => 76,
'category_name' => 'Suitcases',
'parent_id' => 32,
'level' => 2,
),
'76' => Array
(
'category_id' => 77,
'category_name' => 'Hand Luggage',
'parent_id' => 76,
'level' => 3,
),
'77' => Array
(
'category_id' => 78,
'category_name' => 'Vests & Tank Tops',
'parent_id' => 8,
'level' => 4,
),
'78' => Array
(
'category_id' => 79,
'category_name' => 'Parasols & Umbrellas',
'parent_id' => 1,
'level' => 2,
),
'79' => Array
(
'category_id' => 80,
'category_name' => 'Carry-On Luggage',
'parent_id' => 76,
'level' => 3,
),
'80' => Array
(
'category_id' => 81,
'category_name' => 'Dresses',
'parent_id' => 6,
'level' => 3,
),
'81' => Array
(
'category_id' => 82,
'category_name' => 'Day Dresses',
'parent_id' => 81,
'level' => 4,
),
'82' => Array
(
'category_id' => 83,
'category_name' => 'Cargo Pants',
'parent_id' => 13,
'level' => 4,
),
'83' => Array
(
'category_id' => 84,
'category_name' => 'Hand Luggage',
'parent_id' => 32,
'level' => 2,
),
'84' => Array
(
'category_id' => 85,
'category_name' => 'Shoulder Bags',
'parent_id' => 69,
'level' => 4,
),
'85' => Array
(
'category_id' => 86,
'category_name' => 'Bermuda Shorts',
'parent_id' => 7,
'level' => 4,
),
'86' => Array
(
'category_id' => 87,
'category_name' => 'Shorts',
'parent_id' => 35,
'level' => 3,
),
'87' => Array
(
'category_id' => 88,
'category_name' => 'Cargo Shorts',
'parent_id' => 87,
'level' => 4,
),
'88' => Array
(
'category_id' => 89,
'category_name' => 'Trousers',
'parent_id' => 6,
'level' => 3,
),
'89' => Array
(
'category_id' => 90,
'category_name' => 'Casual Trousers',
'parent_id' => 89,
'level' => 4,
),
'90' => Array
(
'category_id' => 91,
'category_name' => 'Handbag & Wallet Accessories',
'parent_id' => 24,
'level' => 2,
),
'91' => Array
(
'category_id' => 92,
'category_name' => 'Lanyards',
'parent_id' => 91,
'level' => 3,
),
'92' => Array
(
'category_id' => 93,
'category_name' => 'Duffel Bags',
'parent_id' => 32,
'level' => 2,
),
'93' => Array
(
'category_id' => 94,
'category_name' => 'Polo Shirts',
'parent_id' => 15,
'level' => 4,
),
'94' => Array
(
'category_id' => 95,
'category_name' => 'Skorts',
'parent_id' => 6,
'level' => 3,
),
'95' => Array
(
'category_id' => 96,
'category_name' => 'Casual Jackets',
'parent_id' => 53,
'level' => 5,
),
'96' => Array
(
'category_id' => 97,
'category_name' => 'Leggings',
'parent_id' => 89,
'level' => 4,
),
'97' => Array
(
'category_id' => 98,
'category_name' => 'Canteens',
'parent_id' => 3,
'level' => 4,
),
'98' => Array
(
'category_id' => 99,
'category_name' => 'Caps',
'parent_id' => 18,
'level' => 4,
),
'99' => Array
(
'category_id' => 100,
'category_name' => 'Baseball Hats',
'parent_id' => 99,
'level' => 5,
),
'100' => Array
(
'category_id' => 101,
'category_name' => 'Rain Gear',
'parent_id' => 52,
'level' => 4,
),
'101' => Array
(
'category_id' => 102,
'category_name' => 'Waterproof Coats',
'parent_id' => 101,
'level' => 5,
),
'102' => Array
(
'category_id' => 103,
'category_name' => 'Capes & Ponchos',
'parent_id' => 53,
'level' => 5,
),
'103' => Array
(
'category_id' => 104,
'category_name' => 'Sweatshirts',
'parent_id' => 15,
'level' => 4,
),
'104' => Array
(
'category_id' => 105,
'category_name' => 'Outdoor Shoes',
'parent_id' => 43,
'level' => 3,
),
'105' => Array
(
'category_id' => 106,
'category_name' => 'Water Shoes',
'parent_id' => 105,
'level' => 4,
),
'106' => Array
(
'category_id' => 107,
'category_name' => 'Polo Shirts',
'parent_id' => 55,
'level' => 4,
),
'107' => Array
(
'category_id' => 108,
'category_name' => 'Dresses',
'parent_id' => 35,
'level' => 3,
),
'108' => Array
(
'category_id' => 109,
'category_name' => 'Day Dresses',
'parent_id' => 108,
'level' => 4,
),
'109' => Array
(
'category_id' => 110,
'category_name' => 'Sweaters & Cardigans',
'parent_id' => 55,
'level' => 4,
),
'110' => Array
(
'category_id' => 111,
'category_name' => 'Shopping Totes',
'parent_id' => 32,
'level' => 2,
),
'111' => Array
(
'category_id' => 112,
'category_name' => 'Sweatshirts',
'parent_id' => 55,
'level' => 4,
),
'112' => Array
(
'category_id' => 113,
'category_name' => 'Scarves & Shawls',
'parent_id' => 25,
'level' => 3,
),
'113' => Array
(
'category_id' => 114,
'category_name' => 'Handbags, Wallets & Cases',
'parent_id' => 24,
'level' => 2,
),
'114' => Array
(
'category_id' => 115,
'category_name' => 'Handbags',
'parent_id' => 114,
'level' => 3,
),
'115' => Array
(
'category_id' => 116,
'category_name' => 'Tote Handbags',
'parent_id' => 115,
'level' => 4,
),
'116' => Array
(
'category_id' => 117,
'category_name' => 'Underwear & Socks',
'parent_id' => 35,
'level' => 3,
),
'117' => Array
(
'category_id' => 118,
'category_name' => 'Socks',
'parent_id' => 117,
'level' => 4,
),
'118' => Array
(
'category_id' => 119,
'category_name' => 'Cross-Body Handbags',
'parent_id' => 115,
'level' => 4,
),
'119' => Array
(
'category_id' => 120,
'category_name' => 'Underwear',
'parent_id' => 117,
'level' => 4,
),
);
code
function categoriesCmd($a, $b)
{
return strnatcmp($a['path'], $b['path']);
}
//
// sort all categories values by path. Path - it is category parent ids:
// 1
// 1/2
// 1/2/3
// 1/2/3/4
// 1/2/3/98
// 1/2/46
// 1/2/46/47
// 1/2/46/47/48
// 1/2/49
// etc
//
$maxLevel = 0;
$categories = array();
foreach ($categoriesrRaw as $key => $category) {
$parentId = $category['parent_id'];
$category['path'] = array($category['category_id']);
if ($maxLevel < $category['level']) {
$maxLevel = $category['level'];
}
while ($parentId) {
foreach ($categoriesrRaw as $item) {
if ($item['category_id'] == $parentId) {
$newParentId = $item['parent_id'];
$category['path'][] = $item['category_id'];
break;
}
}
$parentId = $newParentId;
// $category['level']++;
}
$category['path'] = implode('/', array_reverse($category['path']));
$categories[] = $category;
}
usort($categories, 'categoriesCmd');
//
// create needed array structure
//
$prev = array_shift($categories);
$level = 0;
$result = array(array_fill(0, $maxLevel, ""));
$result[0][0] = $category['category_name'];
foreach ($categories as $category) {
if ($prev['level'] >= $category['level']) {
$level++;
$result[$level] = array_fill(0, $maxLevel, "");
}
$result[$level][$category['level']-1] = $category['category_name'];
$prev = $category;
}
print_r($result);

Get all the latest children

I need a little help. My knowledge of algorithms are weak. I can not write a recursive function in PHP that returns all the latest children.
Suppose our array looks like this:
Array
(
[0] => Array
(
[id_category] => 1
[name] => Accueil
[id_parent] => 0
)
[1] => Array
(
[id_category] => 2
[name] => Accessoires
[id_parent] => 1
)
[2] => Array
(
[id_category] => 3
[name] => Merchandising
[id_parent] => 1
)
[3] => Array
(
[id_category] => 4
[name] => Pièces détachées
[id_parent] => 1
)
[4] => Array
(
[id_category] => 5
[name] => Excavateur
[id_parent] => 4
)
[5] => Array
(
[id_category] => 6
[name] => série 100
[id_parent] => 5
)
[6] => Array
(
[id_category] => 7
[name] => above
[id_parent] => 6
)
[7] => Array
(
[id_category] => 8
[name] => système hydraulique
[id_parent] => 7
)
[8] => Array
(
[id_category] => 9
[name] => série 200
[id_parent] => 5
)
[9] => Array
(
[id_category] => 10
[name] => thru
[id_parent] => 6
)
[10] => Array
(
[id_category] => 11
[name] => Compaction
[id_parent] => 4
)
[11] => Array
(
[id_category] => 12
[name] => système électrique
[id_parent] => 7
)
)
I would like getLastChildren (5) or getLastChildren (6) or getLastChildren (7), the function returns me an answer array ("8", "12")
I will try to give an example.
If I take the category 5: = 6 and 9 are the children.
I look through children. The child 6 has two children, 7 and 10, Child 9: no children.
I put 9 in the list of children.
The child 7 has two children, 8 and 12. 8 has no children. 12 has no children. I add 8 and 12.
So we return (9,8,12) 10 has no children. I also added.
In the end I (9,8,12,10)
So what I would do, if I search "all the last children in category 7" => 8, and 12. I hope my explanation is "a little clearer."
I would do something like that:
$array = array(
array (
'id_category' => 1,
'name' => 'Accueil',
'id_parent' => 0,
),
array (
'id_category' => 2,
'name' => 'Accessoires',
'id_parent' => 1,
),
array (
'id_category' => 3,
'name' => 'Merchandising',
'id_parent' => 1,
),
array (
'id_category' => 4,
'name' => 'Pièces détachées',
'id_parent' => 1,
),
array (
'id_category' => 5,
'name' => 'Excavateur',
'id_parent' => 4,
),
array (
'id_category' => 6,
'name' => 'série 100',
'id_parent' => 5,
),
array (
'id_category' => 7,
'name' => 'above',
'id_parent' => 6,
),
array (
'id_category' => 8,
'name' => 'système hydraulique',
'id_parent' => 7,
),
array (
'id_category' => 9,
'name' => 'série 200',
'id_parent' => 5,
),
array (
'id_category' => 10,
'name' => 'thru',
'id_parent' => 6,
),
array (
'id_category' => 11,
'name' => 'Compaction',
'id_parent' => 4,
),
array (
'id_category' => 12,
'name' => 'système électrique',
'id_parent' => 7,
),
);
I split data and code, here is the code:
$children = array();
function getLastChildren($array, $parent) {
global $children;
foreach ($array as $key => $value) {
if ($value['id_parent'] == $parent) {
if (hasChild($array, $value['id_category'])) {
getLastChildren($array, $value['id_category']);
} else {
$children[] = $value['id_category'];
}
}
}
}
function hasChild($array, $parent) {
foreach ($array as $key => $value) {
if ($value['id_parent'] == $parent) {
return true;
}
}
return false;
}
getLastChildren($array, 5);
print_r($children);
output:
Array
(
[0] => 8
[1] => 12
[2] => 10
[3] => 9
)

PHP Arrays, appending count of array items recursively to an array

Pointless Dribble
Okay This is another weird one from me, i want to thank OIS for helping me out on my last question... which deals with this same kind of funky array manipulation... i studied that code in depth and i feel it has helped me become better with recursive array manipulative functions. However, once again i find my self in another tough spot
Actual Problem
I am trying to write a recursive manipulative function such as this. Except for getting the depth of each array element. It will add incremental count to each array element with a certain depth. The easiest way to see what I'm trying to do is to view the "sample array" and "Desired result Array"... i feel like I'm getting better at understanding these kind of recursive functions. but this one is giving me hell, thanks in advance for any kind of help you can give me with this.
Please disregard the [depth] result of the sample array i already have a function that adds this.
Thanks again,
-- YouDontMeanMuch
Sample Array
array (
52 =>
array (
'title' => 'Website Navigation',
'path' => '',
'type' => '115',
'pid' => 0,
'hasChildren' => 1,
'children' =>
array (
53 =>
array (
'title' => 'Home',
'path' => '',
'type' => '118',
'pid' => 52,
'hasChildren' => 0,
),
54 =>
array (
'title' => 'Features',
'path' => 'features',
'type' => '374',
'pid' => 52,
'hasChildren' => 1,
'children' =>
array (
59 =>
array (
'title' => 'artistic',
'path' => 'features/artistic',
'type' => '374',
'pid' => 54,
'hasChildren' => 1,
'children' =>
array (
63 =>
array (
'title' => 'galleries',
'path' => 'features/artistic/galleries',
'type' => '374',
'pid' => 59,
'hasChildren' => 1,
'children' =>
array (
65 =>
array (
'title' => 'graphics',
'path' => 'features/artistic/galleries/graphics',
'type' => '118',
'pid' => 63,
'hasChildren' => 0,
),
67 =>
array (
'title' => 'mixed medium',
'path' => 'features/artistic/galleries/mixed-medium',
'type' => '118',
'pid' => 63,
'hasChildren' => 0,
),
64 =>
array (
'title' => 'overview',
'path' => 'features/artistic/galleries',
'type' => '118',
'pid' => 63,
'hasChildren' => 0,
),
68 =>
array (
'title' => 'photography',
'path' => 'features/artistic/galleries/photography',
'type' => '118',
'pid' => 63,
'hasChildren' => 0,
),
66 =>
array (
'title' => 'traditional',
'path' => 'features/artistic/galleries/traditional',
'type' => '118',
'pid' => 63,
'hasChildren' => 0,
),
),
),
62 =>
array (
'title' => 'overview',
'path' => 'features/artistic',
'type' => '118',
'pid' => 59,
'hasChildren' => 0,
),
69 =>
array (
'title' => 'tutorials',
'path' => 'features/artistic/tutorials',
'type' => '374',
'pid' => 59,
'hasChildren' => 1,
'children' =>
array (
71 =>
array (
'title' => 'by category',
'path' => 'features/artistic/tutorials/by-category/',
'type' => '118',
'pid' => 69,
'hasChildren' => 0,
),
72 =>
array (
'title' => 'by date',
'path' => 'features/artistic/tutorials/by-date/',
'type' => '118',
'pid' => 69,
'hasChildren' => 0,
),
70 =>
array (
'title' => 'overview',
'path' => 'features/artistic/tutorials',
'type' => '118',
'pid' => 69,
'hasChildren' => 0,
),
),
),
),
),
58 =>
array (
'title' => 'overview',
'path' => 'features',
'type' => '118',
'pid' => 54,
'hasChildren' => 0,
),
61 =>
array (
'title' => 'projects / labs',
'path' => 'features/projects-labs/',
'type' => '374',
'pid' => 54,
'hasChildren' => 0,
),
60 =>
array (
'title' => 'web development',
'path' => 'features/web-development',
'type' => '374',
'pid' => 54,
'hasChildren' => 1,
'children' =>
array (
74 =>
array (
'title' => 'articles',
'path' => 'features/web-development/articles/',
'type' => '374',
'pid' => 60,
'hasChildren' => 0,
),
73 =>
array (
'title' => 'overview',
'path' => 'features/web-development',
'type' => '118',
'pid' => 60,
'hasChildren' => 0,
),
75 =>
array (
'title' => 'tutorials',
'path' => 'features/web-development/tutorials',
'type' => '374',
'pid' => 60,
'hasChildren' => 0,
),
),
),
),
),
55 =>
array (
'title' => 'Activity',
'path' => 'activity',
'type' => '374',
'pid' => 52,
'hasChildren' => 0,
),
56 =>
array (
'title' => 'Blog',
'path' => 'blog',
'type' => '374',
'pid' => 52,
'hasChildren' => 0,
),
57 =>
array (
'title' => 'About',
'path' => 'about',
'type' => '374',
'pid' => 52,
'hasChildren' => 1,
'children' =>
array (
76 =>
array (
'title' => 'the author',
'path' => 'about/the-author',
'type' => '118',
'pid' => 57,
'hasChildren' => 0,
),
77 =>
array (
'title' => 'the website',
'path' => 'about/the-website',
'type' => '118',
'pid' => 57,
'hasChildren' => 0,
),
),
),
),
),
)
Desired result Array Notice the [count] index
Array
(
[53] => Array
(
[title] => Home
[path] =>
[type] => 118
[pid] => 52
[mid] => 53
[hasChildren] => 0
[depth] => 1
[count] => 1
)
[54] => Array
(
[title] => Features
[path] => features
[type] => 374
[pid] => 52
[mid] => 54
[hasChildren] => 1
[depth] => 1
[count] => 2
[children] => Array
(
[59] => Array
(
[title] => artistic
[path] => features/artistic
[type] => 374
[pid] => 54
[mid] => 59
[hasChildren] => 1
[depth] => 2
[count] => 1
[children] => Array
(
[63] => Array
(
[title] => galleries
[path] => features/artistic/galleries
[type] => 374
[pid] => 59
[mid] => 63
[hasChildren] => 1
[depth] => 3
[count] => 1
[children] => Array
(
[65] => Array
(
[title] => graphics
[path] => features/artistic/galleries/graphics
[type] => 118
[pid] => 63
[mid] => 65
[hasChildren] => 0
[depth] => 4
[count] => 1
)
[67] => Array
(
[title] => mixed medium
[path] => features/artistic/galleries/mixed-medium
[type] => 118
[pid] => 63
[mid] => 67
[hasChildren] => 0
[depth] => 4
[count] => 2
)
[64] => Array
(
[title] => overview
[path] => features/artistic/galleries
[type] => 118
[pid] => 63
[mid] => 64
[hasChildren] => 0
[depth] => 4
[count] => 3
)
[68] => Array
(
[title] => photography
[path] => features/artistic/galleries/photography
[type] => 118
[pid] => 63
[mid] => 68
[hasChildren] => 0
[depth] => 4
[count] => 4
)
[66] => Array
(
[title] => traditional
[path] => features/artistic/galleries/traditional
[type] => 118
[pid] => 63
[mid] => 66
[hasChildren] => 0
[depth] => 4
[count] => 5
)
)
)
[62] => Array
(
[title] => overview
[path] => features/artistic
[type] => 118
[pid] => 59
[mid] => 62
[hasChildren] => 0
[depth] => 3
[count] => 2
)
[69] => Array
(
[title] => tutorials
[path] => features/artistic/tutorials
[type] => 374
[pid] => 59
[mid] => 69
[hasChildren] => 1
[depth] => 3
[count] => 3
[children] => Array
(
[71] => Array
(
[title] => by category
[path] => features/artistic/tutorials/by-category/
[type] => 118
[pid] => 69
[mid] => 71
[hasChildren] => 0
[depth] => 4
[count] => 1
)
[72] => Array
(
[title] => by date
[path] => features/artistic/tutorials/by-date/
[type] => 118
[pid] => 69
[mid] => 72
[hasChildren] => 0
[depth] => 4
[count] => 2
)
[70] => Array
(
[title] => overview
[path] => features/artistic/tutorials
[type] => 118
[pid] => 69
[mid] => 70
[hasChildren] => 0
[depth] => 4
[count] => 3
)
)
)
)
)
[58] => Array
(
[title] => overview
[path] => features
[type] => 118
[pid] => 54
[mid] => 58
[hasChildren] => 0
[depth] => 2
[count] => 2
)
[61] => Array
(
[title] => projects / labs
[path] => features/projects-labs/
[type] => 374
[pid] => 54
[mid] => 61
[hasChildren] => 0
[depth] => 2
[count] => 3
)
[60] => Array
(
[title] => web development
[path] => features/web-development
[type] => 374
[pid] => 54
[mid] => 60
[hasChildren] => 1
[depth] => 2
[count] => 4
[children] => Array
(
[74] => Array
(
[title] => articles
[path] => features/web-development/articles/
[type] => 374
[pid] => 60
[mid] => 74
[hasChildren] => 0
[depth] => 3
[count] => 1
)
[73] => Array
(
[title] => overview
[path] => features/web-development
[type] => 118
[pid] => 60
[mid] => 73
[hasChildren] => 0
[depth] => 3
[count] => 2
)
[75] => Array
(
[title] => tutorials
[path] => features/web-development/tutorials
[type] => 374
[pid] => 60
[mid] => 75
[hasChildren] => 0
[depth] => 3
[count] => 3
)
I think this should work... I wasn't able to test on your example array but it seems to work on a smaller array I made.
Edit: Changed the function now that you've removed the 'depth' keys from your example array. Now it finds the depth on its own. I've also added my test code and output:
<?php
function array_depth_count(&$array, $count=array(), $depth=1) {
foreach ($array as &$value) {
if (is_array($value)) {
$value['count'] = ++$count[$depth];
array_depth_count($value, $count, $depth + 1);
}
}
}
$a = array(array(array(array(0),array(0),array(),array()),0,array()));
echo "Before\n";
print_r($a);
array_depth_count($a);
echo "\n\nAfter\n";
print_r($a);
?>
Output:
Before
Array
(
[0] => Array
(
[0] => Array
(
[0] => Array
(
[0] => 0
)
[1] => Array
(
[0] => 0
)
[2] => Array
(
)
[3] => Array
(
)
)
[1] => 0
[2] => Array
(
)
)
)
After
Array
(
[0] => Array
(
[0] => Array
(
[0] => Array
(
[0] => 0
[count] => 1
)
[1] => Array
(
[0] => 0
[count] => 2
)
[2] => Array
(
[count] => 3
)
[3] => Array
(
[count] => 4
)
[count] => 1
)
[1] => 0
[2] => Array
(
[count] => 2
)
[count] => 1
)
)
I really want to say this will work
function deep(&$layer)
{
$count = 1;
$keys = array_keys($layer);
foreach($keys as $key)
if(is_array($layer[$key]))
deep($layer[$key]);
$layer['depth'] = $count++;
}
(Tested and works fine for me)
(Another case of me misunderstanding the question. This should be what you want)

Categories