PHP将xml解析为数组

时间:2018-03-16 08:21:14

标签: php arrays xml api simplexml

我需要将xml转换为数组。 我从在线API获取xml。 到目前为止我的代码:

function download_page($path){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$path);
curl_setopt($ch, CURLOPT_FAILONERROR,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$retValue = curl_exec($ch);          
curl_close($ch);
return $retValue;
}
$sXML = download_page('https://url/api/user/distgroup/domain/user?t=ticketofuser');
echo "xml start: ". htmlentities($sXML);
$oXML = new SimpleXMLElement($sXML);

echo "xml: ". $oXML;
foreach($oXML["distributionGroups"] as $key=>$value)
{
  $groups[$key]["group"]["id"]=$value["id"];
    $groups[$key]["group"]["domain"]=$value["domain"];
    $groups[$key]["group"]["name"]=$value["name"];
    $groups[$key]["group"]["type"]=$value["type"];
    $groups[$key]["group"]["loggedIn"]=$value["loggedIn"];
    $groups[$key]["group"]["nightMode"]=$value["nightMode"];

    $groups[$key]["group"]["loggedInAgents"]=$value["loggedInAgents"];
    $groups[$key]["group"]["freeAgents"]=$value["freeAgents"];
    $groups[$key]["group"]["callsWaiting"]=$value["callsWaiting"];
}
$temp=array();
    foreach ($groups as $key => $row) {
         $temp[$key]  = $row["id"]; 
}
    array_multisort($temp, SORT_ASC, $groups);
    $_SESSION["groups"]=$groups;

    echo "groups: ". $groups;

当我用htmlentities($ sXML)回显它时,下载了xml它看起来像这样;

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<distributionGroups>
<group>
<id>33247</id>
<domain>soluno.se</domain>
<name>Kamoda Support</name>
<type>ATTENDANT</type>
<loggedIn>true</loggedIn>
<nightMode>false</nightMode>
<loggedInAgents>1</loggedInAgents>
<freeAgents>1</freeAgents>
<callsWaiting>0</callsWaiting>
</group>
<group>
<id>33257</id>
<domain>soluno.se</domain>
<name>Test 5</name>
<type>ATTENDANT</type>
<loggedIn>false</loggedIn>
<nightMode>false</nightMode>
<loggedInAgents>0</loggedInAgents>
<freeAgents>0</freeAgents>
<callsWaiting>0</callsWaiting>
</group>
</distributionGroups>

我的问题是,在我尝试填充数组后,我的数组是空的。 我做错了什么?

3 个答案:

答案 0 :(得分:1)

在你的第二个foreach中,你错过了关键group。此外,您可以使用$oXML->group对XML元素进行迭代:

$oXML = new SimpleXMLElement($sXML);
$groups = [] ;
foreach($oXML->group as $group)
{
    $groups[]["group"] = [
        'id' => (string)$group->id,
        'domain' => (string) $group->domain,
        'name' => (string) $group->name,
        'type' => (string) $group->type,
        'loggedIn' => (string) $group->loggedIn,
        'nightMode' => (string) $group->nightMode,
        'loggedInAgents' => (string) $group->loggedInAgents,
        'freeAgents' => (string) $group->freeAgents,
        'callsWaiting' => (string) $group->callsWaiting,
    ];
}
$temp=array();
foreach ($groups as $key => $row) {
    $temp[$key]  = $row['group']["id"]; // missing 'group' in $row['group']
}
array_multisort($temp, SORT_ASC, $groups);

print_r($temp);
print_r($groups);

$temp的输出:

Array
(
    [0] => 33247
    [1] => 33257
)

$groups的输出:

Array
(
    [0] => Array
        (
            [group] => Array
                (
                    [id] => 33247
                    [domain] => soluno.se
                    [name] => Kamoda Support
                    [type] => ATTENDANT
                    [loggedIn] => true
                    [nightMode] => false
                    [loggedInAgents] => 1
                    [freeAgents] => 1
                    [callsWaiting] => 0
                )

        )

    [1] => Array
        (
            [group] => Array
                (
                    [id] => 33257
                    [domain] => soluno.se
                    [name] => Test 5
                    [type] => ATTENDANT
                    [loggedIn] => false
                    [nightMode] => false
                    [loggedInAgents] => 0
                    [freeAgents] => 0
                    [callsWaiting] => 0
                )

        )

)

或者您可以删除第一个数组中的"group"

$oXML = new SimpleXMLElement($sXML);
$groups = [] ;
foreach($oXML->group as $group)
{
    $groups[] = [
        'id' => (string)$group->id,
        'domain' => (string) $group->domain,
        'name' => (string) $group->name,
        'type' => (string) $group->type,
        'loggedIn' => (string) $group->loggedIn,
        'nightMode' => (string) $group->nightMode,
        'loggedInAgents' => (string) $group->loggedInAgents,
        'freeAgents' => (string) $group->freeAgents,
        'callsWaiting' => (string) $group->callsWaiting,
    ];
}
$temp=array();
foreach ($groups as $key => $row) {
    $temp[$key]  = $row["id"];
}
array_multisort($temp, SORT_ASC, $groups);

答案 1 :(得分:0)

new SimpleXMLElement($sXML)创建XML元素的对象(不是数组)。因此,在您的情况下,此$oXML = new SimpleXMLElement($sXML);为您提供distributionGroups元素。从那里,您可以访问其foreach($oXML->group as $group)之类的子元素,但请记住$group也是SimpleXMLElement()的实例。要访问实际需要转换对象的元素的内容,即(int) $group->loggedInAgents,以获取整数值。否则$group-> loggedInAgents实际上会为您提供另一个SimpleXMLElement()对象,而不是变量。

docs

中阅读更多内容

答案 2 :(得分:0)

通过让代码复制到组中的每个元素,使用元素名称向元素添加元素,可以使其更加灵活。这意味着当XML发生变化(或者如果)时,代码仍将保留所有传递的数据。

我还合并了两个循环,因此$ temp设置在与主数据相同的循环中。

$oXML = new SimpleXMLElement($sXML);
$groups = array();
$temp=array();
foreach ( $oXML->group as $group )  {
    $data = array();
    foreach ( $group as $element )  {
        $data[ $element->getName() ] = (string)$element;
    }
    $groups[]["group"] = $data;
    $temp[] = $data["id"];
}
print_r($temp);
print_r($groups);