将 xml 文件转换为 PHP 并使用多维数组
Conversion of xml file to PHP and use of multi-dimensional array's
我正在开展一个项目,我需要将 XML 文件转换为 PHP 格式,以便我可以使用文件中的值。我使用了以下代码:
$myarray = simplexml_load_file('/*xml file location*/');
XML 文件是:
<?xml version="1.0" encoding="UTF-8"?>
<GeocodeResponse>
<status>OK</status>
<result>
<type>locality</type>
<type>political</type>
<formatted_address>Vijayawada, Andhra Pradesh, India</formatted_address>
<address_component>
<long_name>Vijayawada</long_name>
<short_name>Vijayawada</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Krishna</long_name>
<short_name>Krishna</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Andhra Pradesh</long_name>
<short_name>AP</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>India</long_name>
<short_name>IN</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>
<location>
<lat>16.5061743</lat>
<lng>80.6480153</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>16.4565307</lat>
<lng>80.5572568</lng>
</southwest>
<northeast>
<lat>16.5639733</lat>
<lng>80.7316657</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>16.4565307</lat>
<lng>80.5572568</lng>
</southwest>
<northeast>
<lat>16.5639733</lat>
<lng>80.7316657</lng>
</northeast>
</bounds>
</geometry>
<place_id>ChIJS5QtSPnvNToRZQJKq4R-m5M</place_id>
</result>
</GeocodeResponse>
这给了我一个 PHP 数组输出:
SimpleXMLElement Object ( [status] => OK
[result] => SimpleXMLElement Object (
[type] => Array (
[0] => locality [1] => political
)
[formatted_address] => Vijayawada, Andhra Pradesh, India
[address_component] => Array (
[0] => SimpleXMLElement Object (
[long_name] => Vijayawada
[short_name] => Vijayawada
[type] => Array (
[0] => locality
[1] => political
)
)
[1] => SimpleXMLElement Object (
[long_name] => Krishna
[short_name] => Krishna
[type] => Array (
[0] => administrative_area_level_2
[1] => political
)
)
[2] => SimpleXMLElement Object (
[long_name] => Andhra Pradesh
[short_name] => AP
[type] => Array (
[0] => administrative_area_level_1
[1] => political
)
)
[3] => SimpleXMLElement Object (
[long_name] => India
[short_name] => IN
[type] => Array (
[0] => country
[1] => political
)
)
)
[geometry] => SimpleXMLElement Object (
[location] => SimpleXMLElement Object (
[lat] => 16.5061743
[lng] => 80.6480153
)
[location_type] => APPROXIMATE
[viewport] => SimpleXMLElement Object (
[southwest] => SimpleXMLElement Object (
[lat] => 16.4565307
[lng] => 80.5572568
)
[northeast] => SimpleXMLElement Object (
[lat] => 16.5639733
[lng] => 80.7316657
)
)
[bounds] => SimpleXMLElement Object (
[southwest] => SimpleXMLElement Object (
[lat] => 16.4565307
[lng] => 80.5572568
)
[northeast] => SimpleXMLElement Object (
[lat] => 16.5639733
[lng] => 80.7316657
)
)
)
[place_id] => ChIJS5QtSPnvNToRZQJKq4R-m5M
)
)
现在,我的问题是:
我只需要位置部分的经纬度值,即
[geometry] => SimpleXMLElement Object ( [location] => SimpleXMLElement Object ( [lat] => 16.5061743 [lng] => 80.6480153 )
所以,
由于缺少一些细节,我猜你想检索这些值?在这种情况下,simplexml 不是 return 数组而是对象。
$myarray = simplexml_load_file('/*xml file location*/');
$lat = $myarray->result->geometry->location->lat;
$long = $myarray->result->geometry->location->lng
我正在开展一个项目,我需要将 XML 文件转换为 PHP 格式,以便我可以使用文件中的值。我使用了以下代码:
$myarray = simplexml_load_file('/*xml file location*/');
XML 文件是:
<?xml version="1.0" encoding="UTF-8"?>
<GeocodeResponse>
<status>OK</status>
<result>
<type>locality</type>
<type>political</type>
<formatted_address>Vijayawada, Andhra Pradesh, India</formatted_address>
<address_component>
<long_name>Vijayawada</long_name>
<short_name>Vijayawada</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Krishna</long_name>
<short_name>Krishna</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Andhra Pradesh</long_name>
<short_name>AP</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>India</long_name>
<short_name>IN</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>
<location>
<lat>16.5061743</lat>
<lng>80.6480153</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>16.4565307</lat>
<lng>80.5572568</lng>
</southwest>
<northeast>
<lat>16.5639733</lat>
<lng>80.7316657</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>16.4565307</lat>
<lng>80.5572568</lng>
</southwest>
<northeast>
<lat>16.5639733</lat>
<lng>80.7316657</lng>
</northeast>
</bounds>
</geometry>
<place_id>ChIJS5QtSPnvNToRZQJKq4R-m5M</place_id>
</result>
</GeocodeResponse>
这给了我一个 PHP 数组输出:
SimpleXMLElement Object ( [status] => OK
[result] => SimpleXMLElement Object (
[type] => Array (
[0] => locality [1] => political
)
[formatted_address] => Vijayawada, Andhra Pradesh, India
[address_component] => Array (
[0] => SimpleXMLElement Object (
[long_name] => Vijayawada
[short_name] => Vijayawada
[type] => Array (
[0] => locality
[1] => political
)
)
[1] => SimpleXMLElement Object (
[long_name] => Krishna
[short_name] => Krishna
[type] => Array (
[0] => administrative_area_level_2
[1] => political
)
)
[2] => SimpleXMLElement Object (
[long_name] => Andhra Pradesh
[short_name] => AP
[type] => Array (
[0] => administrative_area_level_1
[1] => political
)
)
[3] => SimpleXMLElement Object (
[long_name] => India
[short_name] => IN
[type] => Array (
[0] => country
[1] => political
)
)
)
[geometry] => SimpleXMLElement Object (
[location] => SimpleXMLElement Object (
[lat] => 16.5061743
[lng] => 80.6480153
)
[location_type] => APPROXIMATE
[viewport] => SimpleXMLElement Object (
[southwest] => SimpleXMLElement Object (
[lat] => 16.4565307
[lng] => 80.5572568
)
[northeast] => SimpleXMLElement Object (
[lat] => 16.5639733
[lng] => 80.7316657
)
)
[bounds] => SimpleXMLElement Object (
[southwest] => SimpleXMLElement Object (
[lat] => 16.4565307
[lng] => 80.5572568
)
[northeast] => SimpleXMLElement Object (
[lat] => 16.5639733
[lng] => 80.7316657
)
)
)
[place_id] => ChIJS5QtSPnvNToRZQJKq4R-m5M
)
)
现在,我的问题是: 我只需要位置部分的经纬度值,即
[geometry] => SimpleXMLElement Object ( [location] => SimpleXMLElement Object ( [lat] => 16.5061743 [lng] => 80.6480153 )
所以,
由于缺少一些细节,我猜你想检索这些值?在这种情况下,simplexml 不是 return 数组而是对象。
$myarray = simplexml_load_file('/*xml file location*/');
$lat = $myarray->result->geometry->location->lat;
$long = $myarray->result->geometry->location->lng