Converting an Object to an Array with PHP - 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);

Related

convert this array format to single array in php

I want to convert this array in a single dimensional flat array without losing the sort order.
Array
(
[0] => Array
(
[id] => 1
[title] => Computer
[parent_id] => 0
[children] => Array
(
[0] => Array
(
[id] => 4
[title] => keyboard
[parent_id] => 1
[children] => Array
(
[0] => Array
(
[id] => 6
[title] => Mouse
[parent_id] => 4
[children] => Array
(
[0] => Array
(
[id] => 7
[title] => webcam
[parent_id] => 6
)
)
)
)
)
)
)
[1] => Array
(
[id] => 43
[title] => Mobile
[parent_id] => 0
[children] => Array
(
[0] => Array
(
[id] => 5
[title] => bar phones
[parent_id] => 43
)
[1] => Array
(
[id] => 47
[title] => Touchscreen
[parent_id] => 43
[children] => Array
(
[0] => Array
(
[id] => 41
[title] => Samsung
[parent_id] => 47
)
[1] => Array
(
[id] => 44
[title] => Micromax
[parent_id] => 47
)
[2] => Array
(
[id] => 45
[title] => Huawei
[parent_id] => 47
)
)
)
)
)
[2] => Array
(
[id] => 46
[title] => Camera
[parent_id] => 0
)
[3] => Array
(
[id] => 42
[title] => Heater
[parent_id] => 0
)
)
Give it try with below function:
function makeOneDimensionArray(array $array, &$res = array())
{
foreach($array as $arr)
{
$res[] = array(
'id' => $arr['id'],
'title' => $arr['title'],
'parent_id' => $arr['parent_id']
);
if(isset($arr['children']))
{
makeOneDimensionArray($arr['children'], $res);
}
}
return $res;
}
$finalArr = makeOneDimensionArray($your_array);
print_r($finalArr);

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

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.

Value not getting assigned to a variable

I have an array of categories where id is the id of the category, parent denotes the parent id of the category (id 0 denotes the top most parent node) and value is the title of the array. The path is initially set to the id of the category. The array is as follows:
Array
(
[0] => Array
(
[id] => 1
[parent] => 0
[value] => Corporate Files
[path] => 1
)
[1] => Array
(
[id] => 2
[parent] => 0
[value] => Products Files
[path] => 2
)
[2] => Array
(
[id] => 3
[parent] => 1
[value] => Communications Materials
[path] => 3
)
[3] => Array
(
[id] => 4
[parent] => 1
[value] => Group Technical
[path] => 4
)
[4] => Array
(
[id] => 5
[parent] => 1
[value] => New Projects
[path] => 5
)
[5] => Array
(
[id] => 6
[parent] => 2
[value] => Product Range
[path] => 6
)
[6] => Array
(
[id] => 7
[parent] => 2
[value] => WL4
[path] => 7
)
);
I want to generate the paths of categories in the array. so the output should be
Array
(
[0] => Array
(
[id] => 1
[parent] => 0
[value] => Corporate Files
[path] => 1
)
[1] => Array
(
[id] => 2
[parent] => 0
[value] => Products Files
[path] => 2
)
[2] => Array
(
[id] => 3
[parent] => 1
[value] => Communications Materials
[path] => 1,3
)
[3] => Array
(
[id] => 4
[parent] => 1
[value] => Group Technical
[path] => 1,4
)
[4] => Array
(
[id] => 5
[parent] => 1
[value] => New Projects
[path] => 1,5
)
[5] => Array
(
[id] => 6
[parent] => 2
[value] => Product Range
[path] => 2,6
)
[6] => Array
(
[id] => 7
[parent] => 2
[value] => WL4
[path] => 2,7
)
);
I wrote the following function.
function findparent($id,$path){
global $categories;
global $catcnt;
if($id==0){
echo $path."<br />"; //this outputs path currently
return $path;
}
for($i=0;$i<$catcnt;$i++){
if($id==$categories[$i]['id']){
$path=$id.",".$path;
findparent($categories[$i]['parent'],$path);
}
}
}
for($i=0;$i<count($categories);$i++){
$categories[$i]['path']=(string)findparent($categories[$i]['parent'],$categories[$i]['id']); //this doesnt assign it currectly
}
and the output is:
Array
(
[0] => Array
(
[id] => 1
[parent] => 0
[value] => Corporate Files
[path] =>
)
[1] => Array
(
[id] => 2
[parent] => 0
[value] => Products Files
[path] =>
)
[2] => Array
(
[id] => 3
[parent] => 1
[value] => Communications Materials
[path] =>
)
[3] => Array
(
[id] => 4
[parent] => 1
[value] => Group Technical
[path] =>
)
[4] => Array
(
[id] => 5
[parent] => 1
[value] => New Projects
[path] =>
)
[5] => Array
(
[id] => 6
[parent] => 2
[value] => Product Range
[path] =>
)
[6] => Array
(
[id] => 7
[parent] => 2
[value] => WL4
[path] =>
)
);
Where am I going wrong?
findparent only returns if the id is zero.
You need a second return statement, before the recursive findparent call.
As you don't need to deal with multiple levels, a function of it's own is a bit of an overhead as you could work straight on the array itself with a simple foreach:
foreach ($array as &$node)
{
if ($node['parent'])
{
$node['path'] = $node['parent'] . ',' . $node['path'];
}
}
unset($node);
However, you can put this into a function of it's own as well, but you won't need any global variables as far as I can see.
What you see here is simple string concatenation, this makes the array as you wrote you wanted it to have. My first comment was more meant to manage the structure with an n-depth not a 1-depth. Demo

Categories