1

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?

1 Answer 1

1

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

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

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.