Answer the question
In order to leave comments, you need to log in
How to recurse through an array with categories and get the elements ["id"], ["parent"], ["name"] in each category?
Good evening. I have the following array:
Each category ["category"] has attributes ["@attributes"] that have ["id"], ["parent"], ["name"] and so on.
The task is to make a recursion through the entire array and for each category display id, parent and name, and in child categories (with nesting - 1) we do not take into account and do not display the element ["parent"]. How can I do that?
Answer the question
In order to leave comments, you need to log in
You would copy a piece of structure as text, not as a picture. Who wants to understand?
If the structure is like this...
multimap($arr);
function multimap($array, $level = 0) {
$result = [];
foreach ($array as $key => $data) {
foreach ($data as $inner) {
$result[$key] = [
'id' => $inner['@attributes']['id'],
'name' => $inner['@attributes']['name']
];
if ($level == 0) {
$result[$key]['parent'] = $inner['@attributes']['parent'];
}
unset($inner['@attributes']);
$result = \array_merge($result, multimap($inner, $level+1));
}
}
return $result;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question