Peyman Mohamadpour
Peyman Mohamadpour

Reputation: 17944

SimpleXML object array only return the first value in PHP

Question summary

Why does SimpleXML object array only return the first value, and not all array elements.


Question Explanation

XML structure (simplified)

<states>
    <state>
        <name>California</name>
        <cities>
            <city>
                <name>LosAngeles</name>
            </city>
            <city>
                <name>SanFrancisco</name>
            </city>
        </cities>
    </state>
    <state>
        <name>Nevada</name>
        <cities>
            <city>
                <name>LasVegas</name>
            </city>
        </cities>
    </state>
</states>

Method (simplified)

I have getCityList method in my City class:

public function getCityList( $givenState = false ){
    $records = array();
    $states = $this->states->state;

    [var_dump( $states->asXML() )]
    [var_dump( $states[0]->asXML() )]
    [var_dump( $states[1]->asXML() )]

    foreach( $states as $state ){
        if( empty( $givenState ) || ( $state->name == $givenState ) ){
            $cities = $state->cities->city;
            foreach( $cities as $city ){
                $records[ trim( $state->name ) ][] = trim( $city->name );   
            }
        }else{
            return false;   
        }
    }
    return $records;
}

for both $givenState = 'California' and $givenState = 'Nevada', var_dump( $states->asXML() ) gives:

<state>
    <name>California</name>
    <cities>
        <city>
            <name>LosAngeles</name>
        </city>
        <city>
            <name>SanFrancisco</name>
        </city>
    </cities>
</state>

while var_dump( $states[0]->asXML() ) gives:

<state>
    <name>California</name>
    <cities>
        <city>
            <name>LosAngeles</name>
        </city>
        <city>
            <name>SanFrancisco</name>
        </city>
    </cities>
</state>

and var_dump( $states[1]->asXML() ) gives:

<state>
    <name>Nevada</name>
    <cities>
        <city>
            <name>LasVegas</name>
        </city>
    </cities>
</state>

The getCityList method returns correct result on $givenState = 'California' and false on $givenState = 'Nevada'.

Why this array has this behavior?

How can I get it to work so that Nevada results are also returned?

Upvotes: 1

Views: 363

Answers (1)

Armen
Armen

Reputation: 4202

You are doing return false; after first mismatch in if in your foreach( $states as $state ) that if why you are getting false for second check, you need to remove return false and let loop check all items and return $records or false at the end outside loop, like this

....
if( empty($givenState){
    return false;
}

foreach( $states as $state ){
    if( $state->name == $givenState ){
        $cities = $state->cities->city;
        foreach( $cities as $city ){
            $records[ trim( $state->name ) ][] = trim( $city->name );   
        }
    }
}

return count($records) ? $records:false;

also i moved empty($givenState) above loop

Upvotes: 1

Related Questions