vasmor
@vasmor
Интересуюсь

Как преобразовать xml в массив php с сохранением атрибутов?

есть xml, полученный по api
в результате преобразования атрибуты тега image в массиве никак не отображаются

Вариант преобразования xml в массив php
$xml_string = '<?xml version="1.0" encoding="utf-8"?>
<realty-feed xmlns="http://webmaster.yandex.ru/schemas/feed/realty/2010-06">
  <offer internal-id="1111111">
    <type>продажа</type>
    <property-type>жилая</property-type>
    <category>квартира</category>
    <advantages>
      <advantage>Центр города</advantage>
      <advantage>Развитая инфраструктура</advantage>
    </advantages>
    <image tag="plan">http://example.ru/photo/pid/BE72ABD7</image>
    <image tag="housemain">http://example.ru/photo/pid/BE72AB01</image>
    <image>http://example.ru/photo/pid/BE72AB05</image>
    <image>http://example.ru/photo/pid/BE72AB06</image>
    <image tag="floorplan">http://example.ru/photo/pid/BE72AB07</image>
  </offer>
  <offer internal-id="2222222">
    <type>продажа</type>
    <property-type>жилая</property-type>
    <category>квартира</category>
    <advantages>
      <advantage>Центр города</advantage>
      <advantage>Развитая инфраструктура</advantage>
    </advantages>
    <image tag="plan">http://example.ru/photo/pid/gE72ABD7</image>
    <image tag="housemain">http://example.ru/photo/pid/gE72AB01</image>
    <image>http://example.ru/photo/pid/gE72AB05</image>
    <image>http://example.ru/photo/pid/gE72AB06</image>
    <image tag="floorplan">http://example.ru/photo/pid/gE72AB07</image>
  </offer>
</realty-feed>';
$xml = simplexml_load_string( $xml_string );
$json = json_encode( $xml );
$data = json_decode( $json, true );

var_dump( $data );

Вывод результата
array(1) {
  ["offer"]=>
  array(2) {
    [0]=>
    array(6) {
      ["@attributes"]=>
      array(1) {
        ["internal-id"]=>
        string(7) "1111111"
      }
      ["type"]=>
      string(14) "продажа"
      ["property-type"]=>
      string(10) "жилая"
      ["category"]=>
      string(16) "квартира"
      ["advantages"]=>
      array(1) {
        ["advantage"]=>
        array(2) {
          [0]=>
          string(23) "Центр города"
          [1]=>
          string(45) "Развитая инфраструктура"
        }
      }
      ["image"]=>
      array(5) {
        [0]=>
        string(36) "http://example.ru/photo/pid/BE72ABD7"
        [1]=>
        string(36) "http://example.ru/photo/pid/BE72AB01"
        [2]=>
        string(36) "http://example.ru/photo/pid/BE72AB05"
        [3]=>
        string(36) "http://example.ru/photo/pid/BE72AB06"
        [4]=>
        string(36) "http://example.ru/photo/pid/BE72AB07"
      }
    }
    [1]=>
    array(6) {
      ["@attributes"]=>
      array(1) {
        ["internal-id"]=>
        string(7) "2222222"
      }
      ["type"]=>
      string(14) "продажа"
      ["property-type"]=>
      string(10) "жилая"
      ["category"]=>
      string(16) "квартира"
      ["advantages"]=>
      array(1) {
        ["advantage"]=>
        array(2) {
          [0]=>
          string(23) "Центр города"
          [1]=>
          string(45) "Развитая инфраструктура"
        }
      }
      ["image"]=>
      array(5) {
        [0]=>
        string(36) "http://example.ru/photo/pid/gE72ABD7"
        [1]=>
        string(36) "http://example.ru/photo/pid/gE72AB01"
        [2]=>
        string(36) "http://example.ru/photo/pid/gE72AB05"
        [3]=>
        string(36) "http://example.ru/photo/pid/gE72AB06"
        [4]=>
        string(36) "http://example.ru/photo/pid/gE72AB07"
      }
    }
  }
}


как реализовать данное преобразование, чтобы в результирующем массиве тег image, имеющий атрибут tag, выглядел примерно так:
array(2) {
	["@attributes"]=>
		array(1) {
			["tag"]=>
			string(4) "plan"
		}
	["@text"]=>
	string(36) "http://example.ru/photo/pid/BE72ABD7"
}
  • Вопрос задан
  • 44 просмотра
Пригласить эксперта
Ответы на вопрос 1
Fragster
@Fragster
помогло? отметь решением!
Читать последовательно через https://www.php.net/manual/ru/book.xmlreader.php самому заполняя массив. Готовый код не дам, но там несложно.
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы