XML in to a PHP does not display an array -
i need display array php of data file xml (https://pogoda.yandex.ru/static/cities.xml). want in array "id". me find error. thanks
<?php $data_file_city="https://pogoda.yandex.ru/static/cities.xml"; $xml_city = simplexml_load_file($data_file_city); foreach($xml_city->country $key=>$value){ foreach ($value->city $key1=>$value1) { $id = array("$value1[country]"); echo $id; } }
every $value1
in foreach
simplexmlelement object attributes.
to attributes of such object use attributes
function:
$ids = array(); $countries = array(); foreach($xml_city->country $key=>$value){ foreach ($value->city $key1=>$value1) { $attrs = $value1->attributes(); // use `strval` function cast attribute value `string` type // `id` attribute $id = strval($attrs['id']); // `country` attribute $country = strval($attrs['country']); echo $id, '<br />', $country; // add array $ids[] = $id; $countries[] = $country; } }
Comments
Post a Comment