Добрый день! Есть некий xml документ, одно из значений я получаю так Код (Text): $index = $xml->AllData->TripInformation->StopSequence->StopPoint[1]->DisplayContent->LineInformation->LineName->Value; echo 'Route nuber '.$index.'<BR>'; Как мне получить вот такое значение, которое покажу на нерабочем примере Код (Text): $x=14; $index = $xml->AllData->TripInformation->StopSequence->StopPoint[$x]->DisplayContent->LineInformation->LineName->Value; echo 'Route nuber '.$index.'<BR>'; т.е StopPoint должно быть StopPoint[14]
Код (PHP): var_dump($xml->AllData->TripInformation->StopSequence->StopPoint[14]->DisplayContent->LineInformation->LineName->Value->__toString()); у меня и твои два примера работают
Вот весь код. Что делаю не так Код (Text): <?php $url='http://192.168.1.188:6001/services/ibis-ip/CustomerInformationService/GetAllData'; $xml = simpleXML_load_file($url,"SimpleXMLElement",LIBXML_NOCDATA); $idx = $xml->AllData->CurrentStopIndex->Value; $index = $xml->AllData->TripInformation->StopSequence->StopPoint[$idx]->DisplayContent->LineInformation->LineName->Value; echo 'Current stops '.$index.'<BR>';
Вот скажи, зачем ты пишешь загадками? Проще покажи весь xml или кусок, так будет понятнее... Когда же вы научитесь уже составлять полноценные вопросы, а не я делаю, а не работает, что делаешь, где делаешь, не черта не понятно, сиди гадай! Кому оно надо?
Запустил под PHP Version 5.4.25, всё нормально Код (PHP): $xml = new SimpleXMLElement(file_get_contents('GetAllData.xml')); $x = 14; var_dump($xml->AllData->TripInformation->StopSequence->StopPoint[$x]->DisplayContent->LineInformation->LineName->Value->__toString()); результат: string(2) "14"
спасибо, так работает, а если я получаю Код (Text): $x = $xml->AllData->CurrentStopIndex->Value; (переменая $х в данном случае число) а потом подставляю в var_dump, то ругается Код (Text): Notice: Trying to get property of non-object in C:\xampp\htdocs\ivu\get_data.php on line 48 Notice: Trying to get property of non-object in C:\xampp\htdocs\ivu\get_data.php on line 48 Notice: Trying to get property of non-object in C:\xampp\htdocs\ivu\get_data.php on line 48 Notice: Trying to get property of non-object in C:\xampp\htdocs\ivu\get_data.php on line 48 Fatal error: Call to a member function __toString() on a non-object in C:\xampp\htdocs\ivu\get_data.php on line 48
Чёт к вечеру совсем голова не варит Мне необходимо от значения Код (Text): $x = $xml->AllData->CurrentStopIndex->Value; в другую переменную получить значение Код (Text): $xml->AllData->TripInformation->StopSequence->StopPoint[$x]->DisplayContent->LineInformation->LineName->Value я так понимаю придётся использовать var_export
Оказалось всё намного проще: использовал приведение типов Код (Text): $idx = (integer)$xml->AllData->CurrentStopIndex->Value; а потом Код (Text): $v = var_export($xml->AllData->TripInformation->StopSequence->StopPoint[$idx]->StopName->Value->__toString(), true); и всё заработало! Всем спасибо, вопрос закрыт!