Answer the question
In order to leave comments, you need to log in
How to create a “key-value” with the code of the child section in a tree-like associative array for each section?
Good afternoon. There is an array of categories - 93.90.220.244/update_catalog/elko.php
Each element of the array with the key ["@attributes"] stores 1 category with the keys [name] and [code].
Now the question is how to add the [code_parent] key for each category with the code value of the child section.
That is, if there is a child category with keys [code] => 1434 and [name] => "Beauty and Health", then each nested category will have a key [code_parent] with a value of 1434, as in the example below.
And so we do with each section. How to implement it?
Answer the question
In order to leave comments, you need to log in
function addParent($array, $parent = null) {
foreach ($array as $key => $value) {
if (\is_null($parent)) {
$array[$key]['category'] = addParent($value['category'], $value['@attributes']['code']);
$array[$key]['@attributes'] = $value['@attributes'];
} else {
$array[$key]['code_parent'] = $parent;
}
}
return $array;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question