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!
simple_xml, it’s not clear why the json encode / decode used here will simply die. - teran