Hello!

There is an XML file in which there are groups of products and the goods themselves. I need to parse it all in PHP. Actually there is no problem with parsing. But there is a problem associated with the different nesting of these very elements. In my case, the maximum depth of nesting is 5. But in the future they can be caught as deeper XML, there are less deep. I beg you to help me, since I have been sitting for 3 weeks with this task and can not solve it in any way. Shoveled a bunch of information on the Internet, but everywhere show children's examples of parsing. Nowhere is there information about working with multi-level nesting. There is a link like this: Parsing XML (CommerceML) in php .

There the guy asks the same question, he was given the answers. But none is suitable.

Here is an example of my govnokoda:

$xml = simplexml_load_file('uploads/update1c/'.$tar.'/import.xml'); $xml = json_encode($xml); $xml_i = json_decode($xml, true); $array_push = array();// пустой массив для заполнения //Первый уровень вложенности массива foreach($xml_i['Классификатор']['Группы'] as $el_1) { //Второй уровень вложенности массива if (count($el_1) >= 3) { foreach($el_1['Группы']['Группа'] as $el_2) { array_push($array_push, array(2, $el_2['Ид'], $el_2['Наименование'])); //Третий уровень вложенности массива if (count($el_2) >= 3) { foreach($el_2['Группы']['Группа'] as $el_3) { array_push($array_push, array(3, $el_3['Ид'], $el_3['Наименование'])); //Четвёртый уровень вложенности массива if (count($el_3) >= 3) { $target = ''; if (count($el_3['Группы']['Группа']) > 2) { $target = $el_3['Группы']['Группа']; } else { $target = $el_3['Группы']; } foreach($target as $el_4) { array_push($array_push, array(4, $el_4['Ид'], $el_4['Наименование'])); //Пятый уровень вложенности массива if (count($el_4) >= 3) { $target = ''; if (count($el_4['Группы']['Группа']) > 2) { $target = $el_4['Группы']['Группа']; } else { $target = $el_4['Группы']; } foreach($target as $el_5) { array_push($array_push, array(5, $el_5['Ид'], $el_5['Наименование'])); } } } } } } } } } deb($array_push); 

Here in this code: " array_push($array_push, array(2, $el_2['Ид'], $el_2['Наименование'])); ", the number 2 is the nesting level. In each cycle, I increase it manually.

Please help with writing the code. Maybe someone has a more adequate code. Thank!

  • 2
    Apparently, it's time to find out what recursion is. Then the code will be shorter, and nesting will not be terrible. - KoVadim
  • Yes, I know what recursion is. - LexXy
  • Tried it like this: $ iterator = new RecursiveIteratorIterator (new RecursiveArrayIterator ($ xml_i ['Classifier'] ['Groups']), RecursiveIteratorIterator :: SELF_FIRST); foreach ($ iterator as $ key => $ value) {if (is_array ($ value)) {array_push ($ array_push, array ($ value ['Id'], $ value ['Name']))); }} - LexXy
  • and also add an example of your XML file, and think about what the size of your file will be, for with a file size of already a couple of hundred megabytes of simple_xml , it’s not clear why the json encode / decode used here will simply die. - teran
  • Without json encode / decode, the code does not work at all. - LexXy

1 answer 1

Everything, the question is closed. Found a solution to the problem - iterators.