QLiu
QLiu

Reputation: 271

PHP loop through a custom array

I have a complicated Array which is converting from SOAP XML. I would like to loop through array and get ENV:BODY part and display. Is there a way to do that ? I am trying to foreach however, it is a bit complicated since the index name is different ? Anyone would suggestion? thank you

Here is the output of my array

array (
  'SOAPENV:ENVELOPE' => 
  array (
    'ENV:HEADER' => 
    array (
      'WSA:MESSAGEID' => 'urn:960ddd8d-87f0-11e5-a38e-00144ff9d767',
      'WSA:REPLYTO' => 
      array (
        'WSA:ADDRESS' => 'http://www.w3.org/2005/08/addressing/anonymous',
        'WSA:REFERENCEPARAMETERS' => 
        array (
          'INSTRA:TRACKING.ECID' => '52943d5a-ac40-4599-a378-d7c645ea11ca-000ab3ff',
          'INSTRA:TRACKING.FLOWEVENTID' => '86675',
          'INSTRA:TRACKING.FLOWID' => '71161',
          'INSTRA:TRACKING.CORRELATIONFLOWID' => '0000L3nGuO6Fc5GLQyDCid1MBVWK0001VH',
        ),
      ),
      'WSA:FAULTTO' => 
      array (
        'WSA:ADDRESS' => 'http://www.w3.org/2005/08/addressing/anonymous',
      ),
    ),
    'ENV:BODY' => 
    array (
      'NS2:HELLOWORLDRESPONSE' => 
      array (
        'NS2:GREETING' => 'binnenbus Hallo SOA Qing ',
      ),
    ),
  ),
)

It has been created using :

printf("<pre> RESULT: %s</pre>\n",var_export($res,1));

Upvotes: 0

Views: 131

Answers (1)

rjdown
rjdown

Reputation: 9227

No loop is needed here. Just use the keys.

echo $res['SOAPENV:ENVELOPE']['ENV:BODY']['NS2:HELLOWORLDRESPONSE']['NS2:GREETING'];

If you mean that the keys in the ENV:BODY change, you can loop through them like this:

foreach ($res['SOAPENV:ENVELOPE']['ENV:BODY'] as $body_node)
{
    foreach ($body_node as $value)
    {
        echo $value;
    }
}

Upvotes: 1

Related Questions