Php 如何将此xml响应更改为数组?
如何循环使用此xml并仅获取较短的_名称和opted_in属性Php 如何将此xml响应更改为数组?,php,xml,Php,Xml,如何循环使用此xml并仅获取较短的_名称和opted_in属性 <?xml version="1.0"?> <CABS_ProviderOptIn_RS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.leisure.com/Schemas/CABS/1.0/CABS_ProviderO
<?xml version="1.0"?>
<CABS_ProviderOptIn_RS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.leisure.com/Schemas/CABS/1.0/CABS_ProviderOptIn_RS.xsd">
<Status>
<Success xmlns="http://www.leisure.com/Schemas/CABS/1.0/CABS_Common.xsd">success</Success>
</Status>
<Channels>
<Channel id="Blue_Mountains_City_Tourism_Web" xmlns="http://www.leisure.com/Schemas/CABS/1.0/CABS_Common.xsd">
<Providers>
<Provider short_name="adam" content_id="OaksGoldApt" opted_in="true" />
<Provider short_name="ravi" content_id="9002005" opted_in="true" />
<Provider short_name="yoyo" content_id="QuestNthRde" opted_in="true" />
</Providers>
</Channel>
</Channels>
</CABS_ProviderOptIn_RS>
$value){
if($value instanceof simplexmlement){
$array[$key]=空($value)?NULL:toArray($value);
}
}
返回$array;
}
?>
试试这个
<?php
$k = '<?xml version="1.0"?>
<CABS_ProviderOptIn_RS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.leisure.com/Schemas/CABS/1.0/CABS_ProviderOptIn_RS.xsd">
<Status>
<Success xmlns="http://www.leisure.com/Schemas/CABS/1.0/CABS_Common.xsd">success</Success>
</Status>
<Channels>
<Channel id="Blue_Mountains_City_Tourism_Web" xmlns="http://www.leisure.com/Schemas/CABS/1.0/CABS_Common.xsd">
<Providers>
<Provider short_name="adam" content_id="OaksGoldApt" opted_in="true" />
<Provider short_name="ravi" content_id="9002005" opted_in="true" />
<Provider short_name="yoyo" content_id="QuestNthRde" opted_in="true" />
</Providers>
</Channel>
</Channels>
</CABS_ProviderOptIn_RS>';
$xmlpar = simplexml_load_string($k);
$chan = $xmlpar->Channels;
$Channel = $chan->Channel;
$Providers = $Channel->Providers;
$Provider= $Providers->Provider;
foreach($Provider as $pro)
{
$short_name[] = $pro['short_name'];
}
echo "<pre>";
print_r($short_name);
?>
数组“状态”=>数组“成功”=>字符串“成功”(长度=7)“通道”=>数组“通道”=>数组“@attributes”=>数组…”Providers'=>array…它没有返回值,而
print\r
它正在显示此值。你所说的“它不返回值”是什么意思?而且,它仍然需要循环中的许多循环才能获得短名称attributeNo为什么它甚至需要一个循环才能从数组中获得该属性?查看machineaddict提供的url的输出
<?php
$xmlpar = simplexml_load_string('I put this xml here');
var_dump(toArray($xmlpar));
function toArray(SimpleXMLElement $xml) {
$array = (array)$xml;
foreach ( array_slice($array, 0) as $key => $value ) {
if ( $value instanceof SimpleXMLElement ) {
$array[$key] = empty($value) ? NULL : toArray($value);
}
}
return $array;
}
?>
<?php
$k = '<?xml version="1.0"?>
<CABS_ProviderOptIn_RS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.leisure.com/Schemas/CABS/1.0/CABS_ProviderOptIn_RS.xsd">
<Status>
<Success xmlns="http://www.leisure.com/Schemas/CABS/1.0/CABS_Common.xsd">success</Success>
</Status>
<Channels>
<Channel id="Blue_Mountains_City_Tourism_Web" xmlns="http://www.leisure.com/Schemas/CABS/1.0/CABS_Common.xsd">
<Providers>
<Provider short_name="adam" content_id="OaksGoldApt" opted_in="true" />
<Provider short_name="ravi" content_id="9002005" opted_in="true" />
<Provider short_name="yoyo" content_id="QuestNthRde" opted_in="true" />
</Providers>
</Channel>
</Channels>
</CABS_ProviderOptIn_RS>';
$xmlpar = simplexml_load_string($k);
$chan = $xmlpar->Channels;
$Channel = $chan->Channel;
$Providers = $Channel->Providers;
$Provider= $Providers->Provider;
foreach($Provider as $pro)
{
$short_name[] = $pro['short_name'];
}
echo "<pre>";
print_r($short_name);
?>
Array
(
[0] => SimpleXMLElement Object
(
[0] => adam
)
[1] => SimpleXMLElement Object
(
[0] => ravi
)
[2] => SimpleXMLElement Object
(
[0] => yoyo
)
)