Warning: Cannot use a scalar value as an array in /home/admin/public_html/forum/include/fm.class.php on line 757

Warning: Invalid argument supplied for foreach() in /home/admin/public_html/forum/include/fm.class.php on line 770

Warning: Invalid argument supplied for foreach() in /home/admin/public_html/forum/topic.php on line 737
Форумы портала PHP.SU :: Помогите с парсингом массива

 PHP.SU

Программирование на PHP, MySQL и другие веб-технологии
PHP.SU Портал     На главную страницу форума Главная     Помощь Помощь     Поиск Поиск     Поиск Яндекс Поиск Яндекс     Вакансии  Пользователи Пользователи


 Страниц (1): [1]   

> Без описания
Exponat
Отправлено: 15 Сентября, 2015 - 10:32:35
Post Id



Частый гость


Покинул форум
Сообщений всего: 196
Дата рег-ции: Янв. 2013  


Помог: 0 раз(а)




При var_dum($result) получаю вывод ниже. Как его распрарсить? Мне нужно вытянуть в каждой выборке такие поля: ["AppendFieldValue"]=> string(23) "пр. Кирова, 135" ["AppendFieldValue"]=> string(28) "Днепропетровск" ["AppendFieldValue"]=> string(1) "8" ["AppendFieldValue"]=> string(2) "30".

CODE (php):
скопировать код в буфер обмена
  1. object(stdClass)#2 (1) {
  2.  ["return"]=>
  3.   object(stdClass)#3 (2) {
  4.    ["InterfaceState"]=>
  5.     string(2) "OK"
  6.     ["ListCatalog"]=>
  7.     object(stdClass)#4 (2) {
  8.      ["Catalog"]=>
  9.       array(495) {
  10.         [0]=>
  11.         object(stdClass)#5 (4) {
  12.          ["CatalogNameEng"]=>
  13.           string(11) "Departments"
  14.           ["Code"]=>
  15.           string(4) "0408"
  16.           ["Name"]=>
  17.           string(41) "Днепропетровск Титова"
  18.           ["AppendField"]=>
  19.           array(16) {
  20.             [0]=>
  21.             object(stdClass)#6 (2) {
  22.              ["AppendFieldName"]=>
  23.               string(6) "Adress"
  24.               ["AppendFieldValue"]=>
  25.               string(23) "пр. Кирова, 135"
  26.             }
  27.             [3]=>
  28.             object(stdClass)#9 (2) {
  29.              ["AppendFieldName"]=>
  30.               string(4) "City"
  31.               ["AppendFieldValue"]=>
  32.               string(28) "Днепропетровск"
  33.             }
  34.             [7]=>
  35.             object(stdClass)#13 (2) {
  36.              ["AppendFieldName"]=>
  37.               string(21) "WarehouseNumberInCity"
  38.               ["AppendFieldValue"]=>
  39.               string(1) "8"
  40.             }
  41.             [9]=>
  42.             object(stdClass)#15 (2) {
  43.              ["AppendFieldName"]=>
  44.               string(11) "WeightLimit"
  45.               ["AppendFieldValue"]=>
  46.               string(2) "30"
  47.             }
  48.           }
  49.         }
  50.         [1]=>
  51.         object(stdClass)#22 (4) {
  52.          ["CatalogNameEng"]=>
  53.           string(11) "Departments"
  54.           ["Code"]=>
  55.           string(4) "2113"
  56.           ["Name"]=>
  57.           string(48) "Харьков Академика Павлова"
  58.           ["AppendField"]=>
  59.           array(16) {
  60.             [0]=>
  61.             object(stdClass)#23 (2) {
  62.              ["AppendFieldName"]=>
  63.               string(6) "Adress"
  64.               ["AppendFieldValue"]=>
  65.               string(45) "ул. Академика Павлова, 88/7"
  66.             }
  67.             [3]=>
  68.             object(stdClass)#26 (2) {
  69.              ["AppendFieldName"]=>
  70.               string(4) "City"
  71.               ["AppendFieldValue"]=>
  72.               string(14) "Харьков"
  73.             }
  74.             [7]=>
  75.             object(stdClass)#30 (2) {
  76.              ["AppendFieldName"]=>
  77.               string(21) "WarehouseNumberInCity"
  78.               ["AppendFieldValue"]=>
  79.               string(2) "13"
  80.             }
  81.             [9]=>
  82.             object(stdClass)#32 (2) {
  83.              ["AppendFieldName"]=>
  84.               string(11) "WeightLimit"
  85.               ["AppendFieldValue"]=>
  86.               string(2) "30"
  87.             }
  88.           }
  89.         }
  90.         [2]=>
  91.         object(stdClass)#39 (4) {
  92.          ["CatalogNameEng"]=>
  93.           string(11) "Departments"
  94.           ["Code"]=>
  95.           string(4) "1636"
  96.           ["Name"]=>
  97.           string(12) "Сарата"
  98.           ["AppendField"]=>
  99.           array(16) {
  100.             [0]=>
  101.             object(stdClass)#40 (2) {
  102.              ["AppendFieldName"]=>
  103.               string(6) "Adress"
  104.               ["AppendFieldValue"]=>
  105.               string(33) "ул. Котовского, 46-а"
  106.             }
  107.             [3]=>
  108.             object(stdClass)#43 (2) {
  109.              ["AppendFieldName"]=>
  110.               string(4) "City"
  111.               ["AppendFieldValue"]=>
  112.               string(12) "Сарата"
  113.             }
  114.             [7]=>
  115.             object(stdClass)#47 (2) {
  116.              ["AppendFieldName"]=>
  117.               string(21) "WarehouseNumberInCity"
  118.               ["AppendFieldValue"]=>
  119.               string(1) "1"
  120.             }
  121.             [9]=>
  122.             object(stdClass)#49 (2) {
  123.              ["AppendFieldName"]=>
  124.               string(11) "WeightLimit"
  125.               ["AppendFieldValue"]=>
  126.               string(0) ""
  127.             }
  128.           }
  129.         }
  130.         [3]=>
  131.         object(stdClass)#56 (4) {
  132.          ["CatalogNameEng"]=>
  133.           string(11) "Departments"
  134.           ["Code"]=>
  135.           string(4) "2424"
  136.           ["Name"]=>
  137.           string(14) "Каменка"
  138.           ["AppendField"]=>
  139.           array(16) {
  140.             [0]=>
  141.             object(stdClass)#57 (2) {
  142.              ["AppendFieldName"]=>
  143.               string(6) "Adress"
  144.               ["AppendFieldValue"]=>
  145.               string(33) "пер. Гали Кудри, 1-д"
  146.             }
  147.             [3]=>
  148.             object(stdClass)#60 (2) {
  149.              ["AppendFieldName"]=>
  150.               string(4) "City"
  151.               ["AppendFieldValue"]=>
  152.               string(14) "Каменка"
  153.             }
  154.             [7]=>
  155.             object(stdClass)#64 (2) {
  156.              ["AppendFieldName"]=>
  157.               string(21) "WarehouseNumberInCity"
  158.               ["AppendFieldValue"]=>
  159.               string(1) "1"
  160.             }
  161.             [9]=>
  162.             object(stdClass)#66 (2) {
  163.              ["AppendFieldName"]=>
  164.               string(11) "WeightLimit"
  165.               ["AppendFieldValue"]=>
  166.               string(0) ""
  167.             }
  168.           }
  169.         }
  170.         [4]=>
  171.         object(stdClass)#73 (4) {
  172.          ["CatalogNameEng"]=>
  173.           string(11) "Departments"
  174.           ["Code"]=>
  175.           string(4) "2009"
  176.           ["Name"]=>
  177.           string(12) "Почаев"
  178.           ["AppendField"]=>
  179.           array(16) {
  180.             [0]=>
  181.             object(stdClass)#74 (2) {
  182.              ["AppendFieldName"]=>
  183.               string(6) "Adress"
  184.               ["AppendFieldValue"]=>
  185.               string(25) "ул. Шевченко, 6"
  186.             }
  187.             [3]=>
  188.             object(stdClass)#77 (2) {
  189.              ["AppendFieldName"]=>
  190.               string(4) "City"
  191.               ["AppendFieldValue"]=>
  192.               string(12) "Почаев"
  193.             }
  194.             [7]=>
  195.             object(stdClass)#81 (2) {
  196.              ["AppendFieldName"]=>
  197.               string(21) "WarehouseNumberInCity"
  198.               ["AppendFieldValue"]=>
  199.               string(1) "1"
  200.             }
  201.             [9]=>
  202.             object(stdClass)#83 (2) {
  203.              ["AppendFieldName"]=>
  204.               string(11) "WeightLimit"
  205.               ["AppendFieldValue"]=>
  206.               string(0) ""
  207.             }
  208.           }
  209.         }
  210.       }
  211.       ["CatalogState"]=>
  212.       string(4) "ОК"
  213.     }
  214.   }
  215. }


Тут подойдет <?foreach($xml->xpath('//stdClass Object') as $item):?> ?\

Или может перед самим выводом поменять логику чтоб было легче распарсить?
PHP:
скопировать код в буфер обмена
  1. $options = array(
  2.                 'soap_version'=>SOAP_1_2,
  3.                 'exceptions'=>true,
  4.                 'trace'=>1,
  5.                 'cache_wsdl'=>WSDL_CACHE_NONE
  6.             );
  7.  
  8. $api_id = '######';
  9. $api_key = '######################';
  10. $api_auth = array ('ID' => $api_id, 'KEY' => $api_key);
  11. $api_client2 = new SoapClient('https://ws.intime.ua/API/ws/API20/?wsdl', $options);
  12. $catalog = 'Departments';
  13.  
  14.  
  15.     $params = array (
  16.         'AuthData' => $api_auth,
  17.         'CatalogNameEng' => $catalog
  18.     );
  19.     $result = $api_client2->CatalogList(array('CatalogListRequest' => $params));
  20.  
  21. echo '<pre>';
  22. var_dump($result);  
  23. echo '<pre>';

(Отредактировано автором: 15 Сентября, 2015 - 10:46:50)

 
 Top
Panoptik
Отправлено: 15 Сентября, 2015 - 10:54:58
Post Id



Постоянный участник


Покинул форум
Сообщений всего: 2493
Дата рег-ции: Нояб. 2011  
Откуда: Одесса, Украина


Помог: 131 раз(а)




Exponat пишет:
xpath('//stdClass Object')
это конечно эпичный бред

можно достучаться по абсолютному пути
PHP:
скопировать код в буфер обмена
  1. forach($xml->return->InterfaceState->ListCatalog->Catalog as $catalogItem) {
  2.    foreach($catalogItem->CatalogNameEng->AppendField as $appendField) {
  3.      echo $appendField->AppendFieldName . ': ' . $appendField->AppendFieldValue . '<br>';
  4.    }
  5. }


или через хпат примерно так
PHP:
скопировать код в буфер обмена
  1. <?foreach($xml->xpath('//ListCatalog/Catalog') as $item):?>
  2. <?foreach($item->xpath('//AppendField') as $appendField):?>


-----
Just do it
 
 Top
Exponat
Отправлено: 15 Сентября, 2015 - 10:59:26
Post Id



Частый гость


Покинул форум
Сообщений всего: 196
Дата рег-ции: Янв. 2013  


Помог: 0 раз(а)




Выдает ошибку Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML'

Ведь по сути это не есть xml. Это просто страница с выводом кода.
Сейчас php код таковой:
PHP:
скопировать код в буфер обмена
  1. <?$str = file_get_contents("http://site.net/deliv_rss_update/in_rss.php");
  2.  
  3.     foreach($str->return->InterfaceState->ListCatalog->Catalog as $catalogItem) {
  4.        foreach($catalogItem->CatalogNameEng->AppendField as $appendField) {
  5.          echo $appendField->AppendFieldName . ': ' . $appendField->AppendFieldValue . '<br>';
  6.        }
  7.     }?>

Но выдает ошибки:
Notice: Trying to get property of non-object in /int_pars_rss_db.php on line 3

Notice: Trying to get property of non-object in /int_pars_rss_db.php on line 3

Notice: Trying to get property of non-object in /int_pars_rss_db.php on line 3

Notice: Trying to get property of non-object in /int_pars_rss_db.php on line 3

Warning: Invalid argument supplied for foreach() in /int_pars_rss_db.php on line 3
(Добавление)
Просто до return выводит
PHP:
скопировать код в буфер обмена
  1. <?foreach($result->return as $catalogItem) {
  2. echo '<pre>';
  3. var_dump($catalogItem);
  4. }
  5. echo '</pre>';?>

Но если дальше входить
PHP:
скопировать код в буфер обмена
  1. <?foreach($result->return->InterfaceState->ListCatalog->Catalog as $catalogItem) {
  2. var_dump($catalogItem);
  3. }?>

То выдает:
Warning: Invalid argument supplied for foreach() in /int_rss.php on line 24

(Отредактировано автором: 15 Сентября, 2015 - 11:28:16)

 
 Top
Panoptik
Отправлено: 15 Сентября, 2015 - 11:30:37
Post Id



Постоянный участник


Покинул форум
Сообщений всего: 2493
Дата рег-ции: Нояб. 2011  
Откуда: Одесса, Украина


Помог: 131 раз(а)




Ламер


-----
Just do it
 
 Top
Exponat
Отправлено: 15 Сентября, 2015 - 11:36:29
Post Id



Частый гость


Покинул форум
Сообщений всего: 196
Дата рег-ции: Янв. 2013  


Помог: 0 раз(а)




Согласен.
Спасибо за помощь, можете не отвечать.
 
 Top
Panoptik
Отправлено: 15 Сентября, 2015 - 11:41:56
Post Id



Постоянный участник


Покинул форум
Сообщений всего: 2493
Дата рег-ции: Нояб. 2011  
Откуда: Одесса, Украина


Помог: 131 раз(а)




ну блин, смотрите что приходит. почему вообще у вас в первом примере был объект а во втором строка. что отдает файл in_rss.php ?
все эти вопросы должны у вас возникать и ответить на них можете только вы

если не знаете что делать. ставьте вардамп и дай после каждой строчки и смещайте до тех пор пока не появится ошибка ну и разбирайтесь с тем почему в определенных местах появляются данные не тех типов, например в форич попадает строка или массив не той структуры

http://phpfaq[dot]ru/debug


-----
Just do it
 
 Top
Страниц (1): [1]
Сейчас эту тему просматривают: 0 (гостей: 0, зарегистрированных: 0)
« Вопросы новичков »


Все гости форума могут просматривать этот раздел.
Только зарегистрированные пользователи могут создавать новые темы в этом разделе.
Только зарегистрированные пользователи могут отвечать на сообщения в этом разделе.
 



Powered by PHP  Powered By MySQL  Powered by Nginx  Valid CSS  RSS

 
Powered by ExBB FM 1.0 RC1. InvisionExBB