E
E
EvgenyJozef30002019-05-29 15:25:35
PHP
EvgenyJozef3000, 2019-05-29 15:25:35

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.
88a88dd0ab.png
And so we do with each section. How to implement it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
catrinblaidd, 2019-05-29
@EvgenyJozef3000

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

You would write already any class under this structure.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question