Hello. I have categories:

array( 'Кузов/Двери/Дверь багажника', 'Кузов/Двери/Ручка двери наружная', 'Кузов/Двери/Дверь', ); 

They have common parents. How can you do this to get one of these categories with nesting? (you can just explain the logic, without an example)

 Кузов Двери Дверь багажника Ручка двери наружная Дверь 
  • And where do you keep these categories and how? - n.osennij
  • nowhere. I need to form this list (the list will be longer). I think explode will first act - Sarkis Allahverdian
  • and store where you will be then? What is this for? Can you describe in more detail? - n.osennij
  • I will not store anywhere. Nowhere is applied. I need to understand the logic of actions simply. It is necessary to make nesting from the list of this - Sarkis Allahverdian
  • Resort the array to a new one and display the data. - Ruslan Semenov

2 answers 2

You can assemble an array, and then output as you please, works with any nesting:

 $ar = array( 'Кузов/Двери/Дверь багажника', 'Кузов/Двери/Ручка двери наружная', 'Другой/Двери/Дверь', 'Другой/Двери/Красные/Красная', ); $result = []; foreach($ar as $section) { $tmp = &$result; foreach(explode('/', $section) as $subsection) { $tmp = &$tmp[$subsection]; $last = $subsection; } $tmp = $subsection; } print_r($result); 
  • thank. What you need! - Sarkis Allahverdian

Here is an option:

 $ar = array( 'Кузов/Двери/Дверь багажника', 'Кузов/Двери/Ручка двери наружная', 'Кузов/Лудки/Ручка двери наружная', 'Кузов/Двери/Ручка двери наружная', 'Другой/Двери/Дверь', ); foreach ($ar as $value) { $exp = explode("/", $value); $new_ar[$exp[0]][$exp[1]][] = $exp; } foreach ($new_ar as $key => $value) { echo "<h1>$key</h1>"; echo "<pre>"; var_dump($value); echo "</pre>"; } 

  • Hello countrymen :) Unfortunately, I didn’t get the result I wanted. There must be nesting of arrays, as I suppose, in order to build chains of categories. Perhaps something is wrong to you - Sarkis Allahverdian