Skip to content

Instantly share code, notes, and snippets.

@BAHC
Last active May 1, 2019 01:33
Show Gist options
  • Save BAHC/93028bebbe4fd35defaa985c243dee88 to your computer and use it in GitHub Desktop.
Save BAHC/93028bebbe4fd35defaa985c243dee88 to your computer and use it in GitHub Desktop.
ERP Category
<?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