fork download
  1. <?php
  2.  
  3. $result= <<<XML
  4. <?xml version='1.0' encoding='WINDOWS-1251'?>
  5. <categories>
  6.   <category id="20001" >Сетевое оборудование</category>
  7.   <category id="20002" >Контроль доступа</category>
  8.   <category id="20003" >Видеонаблюдение</category>
  9.   <category id="20004" >Источники питания</category>
  10.   <category id="20011" parentId="20004" >Аккумуляторы</category>
  11.   <category id="20012" parentId="20003" >Аналоговые видеокамеры</category>
  12.   <category id="20013" parentId="20002" >Считыватели</category>
  13.   <category id="21247" parentId="20001" >GOAL city</category>
  14.   <category id="20326" parentId="20013" >Komkom</category>
  15.   <category id="20400" parentId="20012" >HikVision</category>
  16.   <category id="20618" parentId="20011" >ITV</category>
  17.   <category id="20619" parentId="20011" >ISS</category>
  18.   <category id="20620" parentId="20011" >DSSL</category>
  19. </categories>
  20. XML;
  21.  
  22. $text = iconv('UTF-8', 'CP1251', $result);
  23.  
  24. $xml = simplexml_load_string($text);
  25.  
  26. $tree = [];
  27. $count = count($xml);
  28.  
  29. /********************************************
  30. * Функции для помощи в построениии крошек
  31. *******************************************/
  32. function haveChild($tree, $id) {
  33. foreach ($tree as $item) {
  34. if (isset($item['parentId']) && $item['parentId'] == $id) {
  35. return true;
  36. }
  37. }
  38.  
  39. return false;
  40. }
  41.  
  42.  
  43. function buildBreadcrumbs($tree, $elem, $level) {
  44.  
  45. $breadcrumbs = $elem['text'];
  46. if (isset($elem['parentId'])) {
  47. $item = [];
  48. for ($i = 0; $i < count($tree); ++$i) {
  49. if ($elem['parentId'] == $tree[$i]['id']) {
  50. $item = $tree[$i];
  51. break;
  52. }
  53. }
  54.  
  55. $tmp = $breadcrumbs;
  56. $arrow = ($level == 0 ? '' : ' > ' );
  57. $level = $level + 1;
  58. $breadcrumbs = buildBreadcrumbs($tree, $item, $level).$tmp.$arrow;
  59. $level = $level - 1;
  60. }
  61.  
  62. return $breadcrumbs;
  63. }
  64.  
  65. /**********************************************/
  66.  
  67.  
  68.  
  69. // формируем массив из xml структуры
  70. for ($i = 0; $i < $count; ++$i) {
  71. $arr = [];
  72. $attr = $xml->category[$i]->attributes();
  73.  
  74. $arr['id'] = (int)$attr->id;
  75. $arr['parentId'] = (int)$attr->parentId;
  76. $arr['text'] = (string)$xml->category[$i];
  77.  
  78. $tree[$i] = $arr;
  79. }
  80.  
  81. $breadcrumbsArray = [];
  82. foreach ($tree as $item) {
  83. // Если у узла нет потомка, значит это крайний узел.
  84. // Будем строить путь до него
  85. if (!haveChild($tree, $item['id'])) {
  86. $breadcrumbsArray[$item['id']] = buildBreadcrumbs($tree, $item, 0);
  87. }
  88. }
  89.  
  90. echo '<pre>';
  91. print_r($breadcrumbsArray);
  92. //print_r($tree);
  93. echo '</pre>';
Success #stdin #stdout #stderr 0.02s 52472KB
stdin
Standard input is empty
stdout
<pre>Array
(
    [21247] => Сетевое оборудование > GOAL city
    [20326] => Контроль доступа > Считыватели > Komkom
    [20400] => Видеонаблюдение > Аналоговые видеокамеры > HikVision
    [20618] => Источники питания > Аккумуляторы > ITV
    [20619] => Источники питания > Аккумуляторы > ISS
    [20620] => Источники питания > Аккумуляторы > DSSL
)
</pre>
stderr
PHP Notice:  Undefined index: text in /home/y0jDMA/prog.php on line 45
PHP Notice:  Undefined index: text in /home/y0jDMA/prog.php on line 45
PHP Notice:  Undefined index: text in /home/y0jDMA/prog.php on line 45
PHP Notice:  Undefined index: text in /home/y0jDMA/prog.php on line 45
PHP Notice:  Undefined index: text in /home/y0jDMA/prog.php on line 45
PHP Notice:  Undefined index: text in /home/y0jDMA/prog.php on line 45