Home > OS >  PHP ?Nested Array
PHP ?Nested Array

Time:12-15

I have an array returned from Google Maps, that looks like this:

array(4) { 
    ["destination_addresses"]=> array(4) { 
        [0]=> string(19) "Walsall WS2 9PS, UK" 
        [1]=> string(19) "Walsall WS2 9PS, UK" 
        [2]=> string(19) "Walsall WS2 9PS, UK" 
        [3]=> string(26) "Wolverhampton WV10 0QP, UK" 
        } 
    ["origin_addresses"]=> array(1) { 
        [0]=> string(18) "Stone ST15 0FL, UK" 
        }   
    ["rows"]=> array(1) { 
        [0]=> array(1) { 
            ["elements"]=> array(4) { 
                [0]=> array(3) { 
                    ["distance"]=> array(2) { 
                        ["text"]=> string(7) "41.9 km" 
                        ["value"]=> int(41947) 
                    } 
                    ["duration"]=> array(2) { 
                        ["text"]=> string(7) "36 mins" 
                        ["value"]=> int(2134) 
                    } 
                    ["status"]=> string(2) "OK" 
                } 
                [1]=> array(3) { 
                    ["distance"]=> array(2) { 
                        ["text"]=> string(7) "41.9 km" 
                        ["value"]=> int(41947) 
                    } 
                    ["duration"]=> array(2) { 
                        ["text"]=> string(7) "36 mins" 
                        ["value"]=> int(2134) 
                    }
                    ["status"]=> string(2) "OK"
                 } 
                [2]=> array(3) { 
                    ["distance"]=> array(2) { 
                        ["text"]=> string(7) "41.9 km" 
                        ["value"]=> int(41947)
                    } 
                    ["duration"]=> array(2) { 
                        ["text"]=> string(7) "36 mins" 
                        ["value"]=> int(2134) 
                    } 
                    ["status"]=> string(2) "OK"
                } 
                [3]=> array(3) { 
                    ["distance"]=> array(2) { 
                        ["text"]=> string(7) "40.9 km" 
                        ["value"]=> int(40924) 
                    } 
                    ["duration"]=> array(2) { 
                        ["text"]=> string(7) "41 mins" 
                        ["value"]=> int(2458) 
                    } 
                    ["status"]=> string(2) "OK" 
                } 
            } 
        } 
    } 
    ["status"]=> string(2) "OK" 
}

I want to be able to run through the array, and print the destination address and the distance and time values.

This prints the destination addresses ok:

    $length=count($properties);
    for($a=0;$a<$length;$a  )
    {
        echo '</br>Index is ' . $a . ' | id is ' . $index[$a] . ' | destination is ' . $properties["destination_addresses"][$a] . ' | time is ';

    }

But I can't figure out how to print the rest. I've been banging my head against the wall all night!

Thank you in anticipation.

CodePudding user response:

To loop over all these journeys and print the distance and time for each potential journey, create a foreach loop and then use the $idx to address the subordinate parts of the muleiple arrays

foreach( $destination['destination_addresses'] as $idx => $to ) {
    if ( $destination['rows'][0]['elements'][$idx]['status'] == 'OK' ) {
        echo 'Going from ' . $destination['origin_addresses'][0];
        echo ' To ' . $to;
        echo ' Distance ' . $destination['rows'][0]['elements'][$idx]['distance']['text'];
        echo ' Duration ' . $destination['rows'][0]['elements'][$idx]['duration']['text'];
    } else {
        //whatever you do if the status is not OK
    }
}

  • Related