2

Ok, so I'm requesting state and city from a service using SOAP and using a 'foreach' loop to output the states, and within each of those iterations, I'm using another 'foreach' loop to output the cities within each state.

First, the SOAP call:

$client = new SoapClient("https://devxml.#####.com/golfService.asmx?wsdl", array(‘features’ => SOAP_SINGLE_ELEMENT_ARRAYS));

$regionList = $client->Areas(
    array(
        'Hdr' => array(
            'ResellerId' => '#####', 
            'SourceCd' => 'A', 
            'UserIp' => '207.58.123.121', 
            'gsDebug' => '1'
          ),
        'Req' => array(
            'CountryID' => 'USA',
            'RegionID' => ''
        )
    )
)

...which returns the following the XML:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<AreasResponse xmlns="http://xml.####.com/">
<AreasResult>
<RetCd>0</RetCd>
<RetMsg />
<Countries>
    <Country>
        <id>USA</id>
        <nm>United States</nm>
        <Regions>
            <Region>
                <id>AZ</id>
                <nm>Arizona</nm>
                <Areas>
                    <Area>
                        <id>Phoenix Northeast</id>
                        <nm>Phoenix Northeast</nm>
                    </Area>
                </Areas>
            </Region>
            <Region>
                <id>FL</id>
                <nm>Florida</nm>
                <Areas>
                    <Area>
                        <id>Ft. Myers/Naples</id>
                        <nm>Ft. Myers/Naples</nm>
                    </Area>
                    <Area>
                        <id>Miami / Ft. Lauderdale</id>
                        <nm>Miami / Ft. Lauderdale</nm>
                    </Area>
                </Areas>
            </Region>
        </Regions>
    </Country>
</Countries>
</AreasResult>
</AreasResponse>
</soap:Body>
</soap:Envelope>

Now, here's how I'm handling this in my PHP:

foreach ($regionList->AreasResult->Countries->Country->Regions->Region as $region):
    echo $region->nm . ": ";
    foreach ($region->Areas->Area as $area):
        echo $area->nm . ", ";
    endforeach;
    echo "<br />";
endforeach;

Ideally, this would output as:

Arizona: Phoenix Northeast,
Florida: Ft. Myers/Naples, Miami / Ft. Lauderdale,

But no! It's showing up like this:

Arizona: , ,
Florida: Ft. Myers/Naples, Miami / Ft. Lauderdale,

The two commas after 'Arizona:' imply there are two records, but there's only one. Even more infuriating is that it won't output the dang Area value. What am I doing wrong? Help me, StackOverflow, you're my only hope!

4
  • 1
    Sorry about that! I accidentally posted the question before I was finished writing it. Commented Mar 19, 2013 at 18:37
  • Shouldn't it be foreach ($region->Areas->Area) ? Commented Mar 19, 2013 at 21:52
  • Yes. That got lost in posting it here but I've corrected it. Commented Mar 20, 2013 at 13:47
  • foreach ($region->Areas as $area) { echo $area->nm }... the areas element is an array, not the area Commented Mar 20, 2013 at 13:54

1 Answer 1

1

This could be related to arrays not being properly cast even though you're using the single array element feature. I used this function in the past that may work in this case:

function forceList($obj, $prop)
{
    $r = &$obj->$prop;
    return isset($r) ? is_array($r) ? $r : array($r) : array();
}

foreach (forceList($region->Areas, 'Area') as $area) { ... }

I know, it makes use of two ternary operators, but I did test it ;-)

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Jack, thanks for your answer. I don't really understand what your code is doing (Why pass '$obj->$prop' by reference?) but I have "solved" it my own way by writing a terribly inelegant kludge that duplicates a bunch of code and uses if statements to see if it's an object or an array.
@user it converts an object to an array and leave existing arrays alone. The reference is used to shorten the code really.

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.