Error to print tree array with php - php

i have a big problem to u resolve, it's killing me.
See a Array response:
Array (
[category_id] => 1
[parent_id] => 0
[name] => Root Catalog
[is_active] =>
[position] => 0
[level] => 0
[children] => Array
(
[0] => Array
(
[category_id] => 2
[parent_id] => 1
[name] => AutomaBrasil
[is_active] => 1
[position] => 1
[level] => 1
[children] => Array
(
[0] => Array
(
[category_id] => 3
[parent_id] => 2
[name] => Automação
[is_active] => 1
[position] => 1
[level] => 2
[children] => Array
(
)
)
[1] => Array
(
[category_id] => 4
[parent_id] => 2
[name] => Balança
[is_active] => 1
[position] => 2
[level] => 2
[children] => Array
(
)
)
[2] => Array
(
[category_id] => 5
[parent_id] => 2
[name] => Caixa Registradora
[is_active] => 1
[position] => 3
[level] => 2
[children] => Array
(
)
)
[3] => Array
(
[category_id] => 6
[parent_id] => 2
[name] => Gaveta de Dinheiro
[is_active] => 1
[position] => 4
[level] => 2
[children] => Array
(
[0] => Array
(
[category_id] => 10
[parent_id] => 6
[name] => Acessórios
[is_active] => 1
[position] => 1
[level] => 3
[children] => Array
(
)
)
[1] => Array
(
[category_id] => 12
[parent_id] => 6
[name] => Gaveta de Dinheiro Automática
[is_active] => 1
[position] => 2
[level] => 3
[children] => Array
(
)
)
)
)
[4] => Array
(
[category_id] => 7
[parent_id] => 2
[name] => Impressora
[is_active] => 1
[position] => 5
[level] => 2
[children] => Array
(
)
)
)
)
))
Until then beauty, but when i try the "foreach", it returns me this:
1
0
Root Catalog
0
0
Array
I need it to print the complete tree inside the tags <ul> <li>.
I'm use recursive foreach:
public function createTree($lists) {
echo '<ul>';
foreach ($lists as $list) {
echo '<li>';
echo $list['name'] . '';
echo '</li>';
if (is_array($list['children'])) {
$this->createTree($list['children']);
}
}
echo '</ul>';
}
U can help me? Tks!

You could try to wrap the output children array into li:
Wrap the $this->createTree($list['children'] into echo '<li>'; $this->createTree($list['children']); echo '</li>'

Related

Fetch values from an array using nested foreach loop

I have a long array from which I wish to fetch all the values and store it in a separate variable, and store each value in database.
The array that I have is:
Array
(
[success] => 1
[categories] => Array
(
[0] => Array
(
[category_id] => 39
[name] => BAGS
[categories] => Array
(
[0] => Array
(
[category_id] => 59
[name] => Handcrafted Purses
[categories] =>
[status] => 1
)
[1] => Array
(
[category_id] => 45
[parent_id] => 39
[name] => Laptop Bag
[categories] =>
[status] => 1
)
)
[status] => 1
)
[1] => Array
(
[category_id] => 40
[name] => BOXERS
[categories] => Array
(
[0] => Array
(
[category_id] => 56
[parent_id] => 40
[name] => Women Boxers
[status] => 1
)
)
[status] => 1
)
[2] => Array
(
[category_id] => 91
[parent_id] => 0
[name] => Business Corporate
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 92
[parent_id] => 91
[name] => Bags
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 93
[parent_id] => 92
[name] => Potli Bags
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
)
[status] => 1
)
[3] => Array
(
[category_id] => 60
[parent_id] => 0
[name] => Business Corporates
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 90
[parent_id] => 60
[name] => Art Cushions
[image] =>
[categories] =>
[status] => 1
)
[1] => Array
(
[category_id] => 67
[parent_id] => 60
[name] => Bags
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 77
[parent_id] => 67
[name] => Potli Bags
[image] =>
[categories] =>
[status] => 1
)
[1] => Array
(
[category_id] => 76
[parent_id] => 67
[name] => Smart Bags
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
[2] => Array
(
[category_id] => 86
[parent_id] => 60
[name] => Fashion Jewellery
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 88
[parent_id] => 86
[name] => Coming Soon - Products Uploading
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
[3] => Array
(
[category_id] => 61
[parent_id] => 60
[name] => Men Footwear
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 65
[parent_id] => 61
[name] => Canvas Loafers
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
[4] => Array
(
[category_id] => 87
[parent_id] => 60
[name] => Shawls And Stoles
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 89
[parent_id] => 87
[name] => Coming Soon - Products Uploading
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
)
[status] => 1
)
[4] => Array
(
[category_id] => 15
[parent_id] => 0
[name] => ETHNIC WEAR
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 28
[parent_id] => 15
[name] => Designer Lehngas
[image] =>
[categories] =>
[status] => 1
)
[1] => Array
(
[category_id] => 2
[parent_id] => 15
[name] => Suits
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
)
)
I am able to fetch the outer values of this array by using this code:
if (!empty($array))
{
foreach ($array['categories'] as $category)
{
echo $category['category_id'];
echo "<br>";
}
}
I got values as:
39
40
91
60
15
16
38
57
But I also wish to access the inner most values of the array. Can anyone tell how I can create a nested loop?
If you now how deep your array is you can just simply add other foreach-loops within your main loop.
if (!empty($array))
{
foreach ($array['categories'] as $category)
{
echo $category['category_id'];
echo "<br>";
if(isset($category['categories'])){
foreach($category['categories'] as $category2)
{
echo $category2['category_id'];
echo "<br>";
if (isset($category2['categories'])){
foreach($category2['categories'] as $category3)
{
echo $category3['category_id'];
echo "<br>";
...
} }
}}
}
}

Convert flat PHP array to multidimensional array

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.

make tree with a multidimensional array

how to make tree with the following multidimensional array?
the child items have a non-zero parentID
Array
(
[0] => Array
(
[id] => 6
[title] => zzz
[parentID] => 0
[parentName] =>
[section] => articles
[sort] => 0
[level] => 0
)
[1] => Array
(
[0] => Array
(
[id] => 7
[title] => 7
[parentID] => 6
[parentName] =>
[section] => articles
[sort] => 0
[level] => 0
)
[1] => Array
(
[0] => Array
(
[id] => 8
[title] => 8
[parentID] => 7
[parentName] =>
[section] => articles
[sort] => 0
[level] => 0
)
)
)
[2] => Array
(
[id] => 1
[title] => تست
[parentID] => 0
[parentName] =>
[section] => articles
[sort] => 0
[level] => 0
)
[3] => Array
(
[0] => Array
(
[id] => 4
[title] => 4
[parentID] => 1
[parentName] =>
[section] => articles
[sort] => 0
[level] => 0
)
[1] => Array
(
[id] => 5
[title] => 5
[parentID] => 1
[parentName] =>
[section] => articles
[sort] => 0
[level] => 0
)
)
)
if i want to show items by id, i need following result :
6
-7
--8
1
-4
-5
my attempt :
foreach ($array as $category)
{
$dash = str_repeat("-", array_depth($category));
echo $dash . $category['id'];
}
Is that what you want it?
function arrayStack( $array, $dash ){
foreach($array as $value){
if(isset($value['id'])){
echo str_repeat("-", $dash).$value['id'].',';
}else{
$dash++;
arrayStack( $value, $dash );
$dash--;
}
}
}
arrayStack( $myArray, 0 );
Demo

build tree with a multidimensional array

how to make tree with the following multidimensional array?
the child items have a non-zero parentID
Array
(
[0] => Array
(
[id] => 6
[title] => zzz
[parentID] => 0
[parentName] =>
[section] => articles
[sort] => 0
[level] => 0
)
[1] => Array
(
[0] => Array
(
[id] => 7
[title] => 7
[parentID] => 6
[parentName] =>
[section] => articles
[sort] => 0
[level] => 0
)
[1] => Array
(
[0] => Array
(
[id] => 8
[title] => 8
[parentID] => 7
[parentName] =>
[section] => articles
[sort] => 0
[level] => 0
)
)
)
[2] => Array
(
[id] => 1
[title] => تست
[parentID] => 0
[parentName] =>
[section] => articles
[sort] => 0
[level] => 0
)
[3] => Array
(
[0] => Array
(
[id] => 4
[title] => 4
[parentID] => 1
[parentName] =>
[section] => articles
[sort] => 0
[level] => 0
)
[1] => Array
(
[id] => 5
[title] => 5
[parentID] => 1
[parentName] =>
[section] => articles
[sort] => 0
[level] => 0
)
)
)
if i want to show items by id, i need following result :
6
-7
--8
1
-4
-5
my attempt :
foreach ($array as $category)
{
$dash = str_repeat("-", array_depth($category));
echo $dash . $category['id'];
}
Something along the lines of
function render_node($node, $indent = '') {
$indent .= '-';
echo $indent . $node[0]['id'];
for($i = 1; $i < array_length($node); $i++) {
render_node($node[$i], "$indent-");
}
}
Not tested, but hope you get the idea. There may be an off by one error and some other oddities.

Array management in php

Its generally related to a question in magento ..but i think using core php can also give me a wat i want
Im having a looping array issue here,My aray is like this
Array
(
[0] => Array
(
[category_id] => 2
[parent_id] => 1
[name] => Koffersenkisten
[is_active] => 0
[position] => 1
[level] => 1
[children] => Array
(
[0] => Array
(
[category_id] => 40
[parent_id] => 2
[name] => Muziek
[is_active] => 1
[position] => 1
[level] => 2
[children] => Array
(
[0] => Array
(
[category_id] => 46
[parent_id] => 40
[name] => Gitaar koffer
[is_active] => 1
[position] => 1
[level] => 3
[children] => Array
(
[0] => Array
(
[category_id] => 50
[parent_id] => 46
[name] => Bas gitaar koffer
[is_active] => 1
[position] => 1
[level] => 4
[children] => Array
(
)
)
[1] => Array
(
[category_id] => 51
[parent_id] => 46
[name] => Electrische gitaar koffer
[is_active] => 1
[position] => 2
[level] => 4
[children] => Array
(
)
)
[2] => Array
(
[category_id] => 47
[parent_id] => 46
[name] => Akoestische gitaar koffer
[is_active] => 1
[position] => 3
[level] => 4
[children] => Array
(
)
)
[3] => Array
(
[category_id] => 49
[parent_id] => 46
[name] => Gitaar soft cases
[is_active] => 1
[position] => 4
[level] => 4
[children] => Array
(
)
)
[4] => Array
(
[category_id] => 52
[parent_id] => 46
[name] => Gitaar gig bags
[is_active] => 1
[position] => 5
[level] => 4
[children] => Array
(
)
)
[5] => Array
(
[category_id] => 53
[parent_id] => 46
[name] => Pedalboards
[is_active] => 1
[position] => 6
[level] => 4
[children] => Array
(
)
)
[6] => Array
(
[category_id] => 48
[parent_id] => 46
[name] => Amp Utility Vehicles
[is_active] => 1
[position] => 7
[level] => 4
[children] => Array
(
)
)
)
)
[1] => Array
(
[category_id] => 67
[parent_id] => 40
[name] => Percussie koffer
[is_active] => 1
[position] => 2
[level] => 3
[children] => Array
(
[0] => Array
(
[category_id] => 73
[parent_id] => 67
[name] => Tom koffer
[is_active] => 1
[position] => 1
[level] => 4
[children] => Array
(
)
)
.......
......
>> it goes like this based on the number of categories
How do i get a simple array of the above like this
array('2' => 'Koffersenkisten','40'=> 'Muziek'...........etc); where 2 is the category_id and Koffersenkisten is the name
This function iterates over all items, and recurses when children are found. It passes the output array along at every invocation and appends to it:
function get_cats(array &$output, array $arr)
{
foreach ($arr as $item) {
$output[$item['category_id']] = $item['name'];
if (count($item['children'])) {
get_cats($output, $item['children']);
}
}
}
// start with empty result
$output = array();
get_cats($output, $a);
print_r($output);
This should work. Didn't test it.
$result = array();
function buildArray($array, $stack)
{
foreach($array as $item)
{
$stack[$item['category_id']] = $item['name'];
if(isset($item['children']) && count($item['children']) > 0)
foreach($item['children'] as $child)
buildArray($item['children'], &$stack);
}
}
buildArray($array, &$result);
print_r($result);
Here is a recursive solution (the array should be in $list before, the created array will be in $result after calling the create function):
function create($list, &$result) {
if (is_array($list) && !isset($list['category_id'])) {
foreach ($list as $element) {
create($element, $result);
}
} else {
$result[$list['category_id']] = $list['name'];
create($list['children'], $result);
}
}
create($list, $result);
Also see this example.

Categories