1

i need to convert an xml to array. I get the xml from an online api. My code so far:

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;

Afterdownloaded the xml it looks like this when i echo it with htmlentities($sXML);

<?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>

My problem is that my array is empty after my try to foreach fill the array. What am i doing wrong?

3 Answers 3

1

In your second foreach, you are missing the key group. Also, you could use $oXML->group to iterator over the XML elements:

$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);

Output of $temp:

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

Output of $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
                )

        )

)

Or you could remove "group" in your first array :

$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);
Sign up to request clarification or add additional context in comments.

Comments

1

You could make it more flexible by getting the code to copy across each element within the group, adding an element to the array with the element name. This means that as the XML changes (or if) then the code will still retain all of the data being passed over.

I've also merged the two loops, so that $temp is set in the same loop as the main data.

$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);

Comments

0

new SimpleXMLElement($sXML) creates an object (not an array) of an XML element. So in your case, this $oXML = new SimpleXMLElement($sXML); gives you the distributionGroups element. From there you can access its child elements like foreach($oXML->group as $group), but remember that $group would also be an instance of SimpleXMLElement(). To access the content of the element you actually need to cast the object, i.e. (int) $group->loggedInAgents, to get an integer value. Otherwise $group-> loggedInAgents will actually give you another SimpleXMLElement() object, rather than a variable.

read more in the docs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.