Last active
May 1, 2019 01:33
-
-
Save BAHC/93028bebbe4fd35defaa985c243dee88 to your computer and use it in GitHub Desktop.
ERP Category
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$xml = file_get_contents("erp_category.xml"); | |
$xml = str_replace(PHP_EOL, '', $xml); | |
$xml = str_replace( "\t", '', $xml); | |
$xml = str_replace(' ', ' ', $xml); | |
$xml = str_replace('<Категория_1', "\n\n".'<Категория_1', $xml); | |
$xml = str_replace('</Категория_1>', '</Категория_1>'."\n\n", $xml); | |
preg_match_all('/(<Категория_1.*\/Категория_1>)\n/', $xml, $matches); | |
$parsed = []; | |
foreach($matches[0] as $category) | |
{ | |
$xml = (array) simplexml_load_string($category); | |
$top = $xml['@attributes']; | |
$top['Родитель'] = 0; | |
$parent_id = $top['Код']; | |
$parsed[$parent_id] = $top; | |
$categories = (array) $xml['Категории_1']; | |
foreach($categories as $category) | |
{ | |
$subcats = (array) $category; | |
foreach ($subcats as $subcat) | |
{ | |
$subcat = (array) $subcat; | |
$sub = $subcat['@attributes']; | |
$sub['Родитель'] = $parent_id; | |
$parsed[$sub['Код']] = $sub; | |
$parent_id = $sub['Код']; | |
$goods = (array) $subcat['Категории_2']; | |
$goods = $goods['Категория']; | |
foreach($goods as $good) | |
{ | |
$good = (array) $good; | |
$good = $good['@attributes']; | |
$good['Родитель'] = $parent_id; | |
$parsed[$good['Код']] = $good; | |
} | |
} | |
} | |
} | |
print_r($parsed); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment