Array management in php - 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.

Related

Converting an Object to an Array with PHP

I have a script which retrieves data from the Magento API If I run the following code:
$category_tree = $client->catalogCategoryTree($session_id);
print_r($category_tree);
The output is as follows:
stdClass Object
(
[category_id] => 1
[parent_id] => 0
[name] => Root Catalog
[position] => 0
[level] => 0
[children] => Array
(
[0] => stdClass Object
(
[category_id] => 2
[parent_id] => 1
[name] => Root Category
[is_active] => 1
[position] => 1
[level] => 1
[children] => Array
(
[0] => stdClass Object
(
[category_id] => 8
[parent_id] => 2
[name] => Designer
[is_active] => 1
[position] => 1
[level] => 2
[children] => Array
(
[0] => stdClass Object
(
[category_id] => 12
[parent_id] => 8
[name] => Chanel
[is_active] => 1
[position] => 1
[level] => 3
[children] => Array
(
)
)
[1] => stdClass Object
(
[category_id] => 13
[parent_id] => 8
[name] => Chanel
[is_active] => 1
[position] => 2
[level] => 3
[children] => Array
(
)
)
[2] => stdClass Object
(
[category_id] => 14
[parent_id] => 8
[name] => Chanel
[is_active] => 1
[position] => 3
[level] => 3
[children] => Array
(
)
)
[3] => stdClass Object
(
[category_id] => 15
[parent_id] => 8
[name] => Chanel
[is_active] => 1
[position] => 4
[level] => 3
[children] => Array
(
)
)
[4] => stdClass Object
(
[category_id] => 16
[parent_id] => 8
[name] => Chanel
[is_active] => 1
[position] => 5
[level] => 3
[children] => Array
(
)
)
)
)
[1] => stdClass Object
(
[category_id] => 7
[parent_id] => 2
[name] => Shop Bags
[is_active] => 1
[position] => 2
[level] => 2
[children] => Array
(
)
)
[2] => stdClass Object
(
[category_id] => 5
[parent_id] => 2
[name] => DifferentCategory
[is_active] => 1
[position] => 3
[level] => 2
[children] => Array
(
[0] => stdClass Object
(
[category_id] => 6
[parent_id] => 5
[name] => 1
[is_active] => 1
[position] => 1
[level] => 3
[children] => Array
(
)
)
)
)
[3] => stdClass Object
(
[category_id] => 4
[parent_id] => 2
[name] => Sample Category
[is_active] => 1
[position] => 6
[level] => 2
[children] => Array
(
)
)
)
)
)
)
When attempting to convert this to an array whilst storing the name values, I receive the following errors upon executing the print_r() function:
Notice: Trying to get property of non-object in C:\xampp\htdocs\create_products.php on line 151
Notice: Trying to get property of non-object in C:\xampp\htdocs\create_products.php on line 151
Notice: Trying to get property of non-object in C:\xampp\htdocs\create_products.php on line 151
Notice: Trying to get property of non-object in C:\xampp\htdocs\create_products.php on line 151
Notice: Trying to get property of non-object in C:\xampp\htdocs\create_products.php on line 151
Notice: Trying to get property of non-object in C:\xampp\htdocs\create_products.php on line 151
Array ( [0] => [1] => [2] => [3] => [4] => [5] => )
My code is below:
$result = [];
foreach ($category_tree as $designer_category) {
$result[] = $designer_category->name;
}
print_r($result);
I need to be able to store the values for name as an array. I've tried typecasting $category_tree to an array with no luck.
Could anybody please shed some light on where I'm going wrong here? Thank you very much in advance for any insight that you can offer.
It will not work, because you have object, not array.
You must go deeper and deeper through all childrens.
Try use recursion, it isn't ideal example, but shows idea:
function goDeeper($object, &$results) {
if (!empty($object->name)) {
$results[] = $object->name;
}
if (!empty($object->children)) {
foreach ($object->children as $child) {
goDeeper($child, $results);
}
}
}
$results = [];
$category_tree = $client->catalogCategoryTree($session_id);
goDeeper($category_tree, $results);
print_r($results);

Error to print tree array with 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>'

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>";
...
} }
}}
}
}

How to search for value in array tree?

this is my array
Array
(
[category_id] => 4
[parent_id] => 3
[name] => Default Category
[is_active] => 1
[position] => 4
[level] => 2
[children] => Array
(
[0] => Array
(
[category_id] => 122
[parent_id] => 4
[name] => Root
[is_active] => 1
[position] => 1
[level] => 3
[children] => Array
(
[0] => Array
(
[category_id] => 123
[parent_id] => 122
[name] => Clothing
[is_active] => 1
[position] => 1
[level] => 4
[children] => Array
(
[0] => Array
(
[category_id] => 124
[parent_id] => 123
[name] => Men Clothing
[is_active] => 1
[position] => 1
[level] => 5
[children] => Array
(
[0] => Array
(
[category_id] => 125
[parent_id] => 124
[name] => Polos & Tees
[is_active] => 1
[position] => 1
[level] => 6
[children] => Array
(
)
)
)
)
)
)
[1] => Array
(
[category_id] => 126
[parent_id] => 122
[name] => Fashion
[is_active] => 1
[position] => 2
[level] => 4
[children] => Array
(
[0] => Array
(
[category_id] => 127
[parent_id] => 126
[name] => Footwear
[is_active] => 1
[position] => 1
[level] => 5
[children] => Array
(
[0] => Array
(
[category_id] => 128
[parent_id] => 127
[name] => Women
[is_active] => 1
[position] => 1
[level] => 6
[children] => Array
(
[0] => Array
(
[category_id] => 129
[parent_id] => 128
[name] => Flats
[is_active] => 1
[position] => 1
[level] => 7
[children] => Array
(
)
)
)
)
)
)
)
)
)
)
)
)
and
what I want to do is that :
write a function
foo($find,$array){
//do some coding to search in the array
return $category_id;//category id of the coresponding matched name in array
}
for example :
foo("Clothing",$array); will return 18
foo("Men Clothing",$array) will return 19
and so on
You can use recursion. An example here..
function getId($arr, $val){
if(is_array($arr)){
if(isset($arr['name']) && trim($arr['name']) == trim($val)){
return isset($arr['category_id']) ? $arr['category_id'] : 'Not found';
}
foreach($arr as $values){
if(is_array($values)){
return getId($values, $val);
}
}
}
}
$val = getId($arr, 'Polos & Tees');
echo $val; //output 20
This is a solution with a recursion:
function foo($find, $array) {
if( $array['name'] == $find ) {
return $array['category_id'];
}
if( empty($array['children']) ) {
return null;
}
foreach($array['children'] as $child) {
$result = foo($find, $child);
if( $result !== null ) {
return $result;
}
}
return null;
}
echo foo('Default Category', $array), "\n"; // 4
echo foo('Root', $array), "\n"; // 122
echo foo('Clothing', $array), "\n"; // 123
echo foo('Men Clothing', $array), "\n"; // 124
echo foo('Polos & Tees', $array), "\n"; // 125
echo foo('Fashion', $array), "\n"; // 126
echo foo('Footwear', $array), "\n"; // 127
echo foo('Women', $array), "\n"; // 128
echo foo('Flats', $array), "\n"; // 129

PHP: Sort an array

I've got an array with data from a MySQL table in nested set model I'd like to get sorted, not only alphabetical but also with the child nodes directly after the parent node.
Example - array to be sorted (before the sorting):
Array
(
[0] => Array
(
[id] => 1
[name] => Kompetenser
[parent] => 0
[depth] => 0
)
[1] => Array
(
[id] => 2
[name] => Administration
[parent] => 1
[depth] => 1
)
[2] => Array
(
[id] => 11
[name] => Organisation
[parent] => 2
[depth] => 2
)
[3] => Array
(
[id] => 4
[name] => Arbetsledning
[parent] => 2
[depth] => 2
)
[4] => Array
(
[id] => 17
[name] => Planering
[parent] => 2
[depth] => 2
)
[5] => Array
(
[id] => 9
[name] => Hantverke
[parent] => 1
[depth] => 1
)
[6] => Array
(
[id] => 10
[name] => Snickeri
[parent] => 9
[depth] => 2
)
[7] => Array
(
[id] => 12
[name] => Språk
[parent] => 1
[depth] => 1
)
[8] => Array
(
[id] => 13
[name] => Tolk
[parent] => 12
[depth] => 2
)
[9] => Array
(
[id] => 15
[name] => Arabiska
[parent] => 13
[depth] => 3
)
[10] => Array
(
[id] => 14
[name] => Persiska
[parent] => 13
[depth] => 3
)
[11] => Array
(
[id] => 16
[name] => Polska
[parent] => 13
[depth] => 3
)
[12] => Array
(
[id] => 18
[name] => Apotekare
[parent] => 1
[depth] => 1
)
[13] => Array
(
[id] => 19
[name] => Dotkorand
[parent] => 1
[depth] => 1
)
[14] => Array
(
[id] => 21
[name] => Atomfysik
[parent] => 19
[depth] => 2
)
[15] => Array
(
[id] => 20
[name] => Fysik
[parent] => 19
[depth] => 2
)
[16] => Array
(
[id] => 22
[name] => Ekonom
[parent] => 1
[depth] => 1
)
[17] => Array
(
[id] => 23
[name] => Industriell ekonomi
[parent] => 22
[depth] => 2
)
[18] => Array
(
[id] => 24
[name] => Filosofi
[parent] => 1
[depth] => 1
)
)
I want the array this way (after the sorting):
Array
(
[0] => Array
(
[id] => 1
[name] => Kompetenser
[parent] => 0
[depth] => 0
)
[1] => Array
(
[id] => 2
[name] => Administration
[parent] => 1
[depth] => 1
)
[3] => Array
(
[id] => 4
[name] => Arbetsledning
[parent] => 2
[depth] => 2
)
[2] => Array
(
[id] => 11
[name] => Organisation
[parent] => 2
[depth] => 2
)
[4] => Array
(
[id] => 17
[name] => Planering
[parent] => 2
[depth] => 2
)
[12] => Array
(
[id] => 18
[name] => Apotekare
[parent] => 1
[depth] => 1
)
[13] => Array
(
[id] => 19
[name] => Dotkorand
[parent] => 1
[depth] => 1
)
[14] => Array
(
[id] => 21
[name] => Atomfysik
[parent] => 19
[depth] => 2
)
[15] => Array
(
[id] => 20
[name] => Fysik
[parent] => 19
[depth] => 2
)
[16] => Array
(
[id] => 22
[name] => Ekonom
[parent] => 1
[depth] => 1
)
[17] => Array
(
[id] => 23
[name] => Industriell ekonomi
[parent] => 22
[depth] => 2
)
[18] => Array
(
[id] => 24
[name] => Filosofi
[parent] => 1
[depth] => 1
)
[5] => Array
(
[id] => 9
[name] => Hantverke
[parent] => 1
[depth] => 1
)
[6] => Array
(
[id] => 10
[name] => Snickeri
[parent] => 9
[depth] => 2
)
[7] => Array
(
[id] => 12
[name] => Språk
[parent] => 1
[depth] => 1
)
[8] => Array
(
[id] => 13
[name] => Tolk
[parent] => 12
[depth] => 2
)
[9] => Array
(
[id] => 15
[name] => Arabiska
[parent] => 13
[depth] => 3
)
[10] => Array
(
[id] => 14
[name] => Persiska
[parent] => 13
[depth] => 3
)
[11] => Array
(
[id] => 16
[name] => Polska
[parent] => 13
[depth] => 3
)
)
As you might see, I want all posts with parent 2 directly after the post with id 2, and so on.
Any help would be highly appreciated.
Thank you in advance.
Don't do this in PHP!
The MySQL server is specificly designed to query AND sort data, read up on the MySQL "ORDER BY" syntax. Doing this on the MySQL server will save run-time, CPU Load and Memory Consumption.
Use php's uasort() function to define your own comparison function.
But using MySQL's sorting capabilities would be more appropriate, if it's possible in your case.
You want to sort it like it is in the DB, multilayered. I don't think uasort can help with this problem because you want to attach the children to the parent.
foreach ($arr as &$val) {
$arr2[$val['id']] = &$val;
}
ksort($arr2);
foreach ($arr2 as $id => &$val) {
$parent = $val['parent'];
if ($parent == 0) {
continue;
}
$arr2[$parent]['children'][$id] = &$val;
}
function flattenArrayByChildren($arr) {
foreach ($arr as $id => $val) {
if (isset($val['children'])) {
$temp = flattenArrayByChildren($val['children']);
unset($val['children']);
$out[$id] = $val;
$out = $out + $temp;
} else {
$out[$id] = $val;
}
}
return $out;
}
$arr2 = array(1 => $arr2[1]);
$out = flattenArrayByChildren($arr2);
var_dump($out);
If you absolutely want to save the key, you can just add that to the $val in the first foreach, and retrieve it in the recursive function flattenArrayByChildren and use as key.
Problem solved - I made two simple functions. I hope other people could have use of this as well:
class data_comp
{
var $fetched_tree = array();
function tree_fetch($parent = 0)
{
$query = 'SELECT node.id, node.name, node.parent, (COUNT(parent.name) - 1) AS depth FROM test_competence AS node, test_competence AS parent WHERE node.lft BETWEEN parent.lft AND parent.rgt GROUP BY node.name ORDER BY node.name';
$result = mysql_query($query) or die(mysql_error());
$tree = array();
while($data = mysql_fetch_assoc($result))
{
$tree[$data['parent']][$data['id']] = array('name' => $data['name'], 'depth' => $data['depth']);
}
$this->tree_print($tree, $parent);
}
function tree_print($tree, $parent)
{
foreach($tree[$parent] as $id => $value)
{
$this->fetched_tree[] = array('id' => $id, 'name' => $value['name'], 'depth' => $value['depth']);
if(isset($tree[$id]) && is_array($tree[$id]))
{
$this->tree_print($tree, $id);
}
}
}
}
Thank you for your time. Any improvements are more than welcome.

Categories