ChasVircio
ChasVircio

Reputation: 3

foreach shows attributes of first xml node only

I am trying to display attributes foreach global_ivr_variable:

$xml = '
  <response method="switchvox.ivr.globalVariables.getList">
      <result>
           <global_ivr_variables>
               <global_ivr_variable id="1" name="cid_name" value="Smith" />
               <global_ivr_variable id="2" name="Q_ID_Global" value="COS" />
           </global_ivr_variables>
      </result>
  </response>
';

$sxml = simplexml_load_string($xml);

foreach($sxml->result->global_ivr_variables->global_ivr_variable->attributes() as $a => $b)
{
echo $a .'=' . $b . "<br>";
}

All I get is the attributes of the first node:

id="1"
name="cid_name"
value="Smith"

I've also tried the following, which gives me no values at all...

    foreach($sxml->result->global_ivr_variables as $xvar)
    {
     $a = $xvar->global_ivr_variable->id;
     $b = $xvar->global_ivr_variable->name;
     $c = $xvar->global_ivr_variable->value;
     echo 'a='.$a.', b='.$b.', c='.$c.'<br>';
    }

a=, b=, c=

Thank you all who step up to help the needy!

Upvotes: 0

Views: 219

Answers (2)

hakre
hakre

Reputation: 197757

If you make use of PHP variables, those problems normally easily disappear because your code is more readable.

Additionally you can access the attributes in SimpleXML via array-notation:

$variables = $sxml->result->global_ivr_variables->global_ivr_variable;

foreach ($variables as $variable)
{
    printf("%s = %s\n", $variable['name'], $variable['value']);
}

This gives the following output:

cid_name = Smith
Q_ID_Global = COS

As you know upfront for which attributes you're looking for, your code is much more clear and stable by using those names.

However, if you're looking for just all attributes of the global_ivr_variable elements, then it's easier to iterate over them with XPath:

$allAttributes = $sxml->xpath('//global_ivr_variable/@*');

foreach ($allAttributes as $attribute) {
    printf("%s = %s\n", $attribute->getName(), $attribute);
}

This then gives the following output:

id = 1
name = cid_name
value = Smith
id = 2
name = Q_ID_Global
value = COS

Here is the full code-example:

$xml = <<<XML
<response method="switchvox.ivr.globalVariables.getList">
    <result>
        <global_ivr_variables>
           <global_ivr_variable id="1" name="cid_name" value="Smith" />
           <global_ivr_variable id="2" name="Q_ID_Global" value="COS" />
        </global_ivr_variables>
    </result>
</response>
XML;

$sxml = simplexml_load_string($xml);

$variables = $sxml->result->global_ivr_variables->global_ivr_variable;

foreach ($variables as $variable)
{
    printf("%s = %s\n", $variable['name'], $variable['value']);
}

echo "----\n";

$allAttributes = $sxml->xpath('//global_ivr_variable/@*');

foreach ($allAttributes as $attribute) {
    printf("%s = %s\n", $attribute->getName(), $attribute);
}

Upvotes: 0

cOle2
cOle2

Reputation: 4784

You almost had it but you need to iterate through each of the <global_ivr_variable> elements and then pull out the attributes:

foreach($sxml->result->global_ivr_variables->global_ivr_variable as $variable)
{
    foreach($variable->attributes() as $a => $b)
    {
        echo $a .'=' . $b . "<br>";
    }
}

Upvotes: 2

Related Questions