Задать вопрос
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"
}
  • Вопрос задан
  • 166 просмотров
Подписаться 1 Простой 1 комментарий
Помогут разобраться в теме Все курсы
  • Skillbox
    Веб-разработчик на PHP
    9 месяцев
    Далее
  • Хекслет
    PHP-разработчик
    10 месяцев
    Далее
  • Нетология
    Веб-разработчик с нуля: профессия с выбором специализации
    14 месяцев
    Далее
Решения вопроса 1
Fragster
@Fragster
помогло? отметь решением!
Читать последовательно через https://www.php.net/manual/ru/book.xmlreader.php самому заполняя массив. Готовый код не дам, но там несложно.
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

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

Похожие вопросы
FoodSoul Калининград
от 180 000 до 250 000 ₽
IT-Spirit Москва
от 230 000 до 320 000 ₽
от 200 000 до 290 000 ₽