简体   繁体   中英

PHP not finding array values labeled by strings

So I have a while loop that generates google maps markers:

<?php
mysql_data_seek($query, 0);
while ($row = mysql_fetch_array($query)) {
    $lat2 = $row['usrhomelat'];
    $lon2 = $row['usrhomelng'];
    echo 'var icon = customIcons[ ' . $row['gender'] . '];';
    echo 'var miLatLng = new google.maps.LatLng(' . $lat2 . ',' . $lon2 . ');';
    echo 'var marker = new google.maps.Marker({';
    echo 'position: miLatLng,';
    echo 'map: map,';
    echo 'icon: icon.icon';
    echo '});';
}
?>

and grabs the appropriate icon from this array:

var customIcons = {
            male: {
                icon: 'https://5d31037421'
            },
            female: {
                icon: 'https://5d310374214f1
            },
            0: {
                icon: 'https://5d310374214f
            },
            1: {
                icon: 'https://5d310374214f1d0670ef
            },
            2: {
                icon: 'https://5d310374214f1d0670e
            }

        };

I am wondering why when the associated array component is an integer, it works, but when it is a string, ie 'male' or 'female' I get and uncaught reference error: male is not defined.

I could temporarily switch those array values over to integers to solve the problem but will likely need them as strings later, so I am just solving this issue before there are too many to switch over.

Sincere thanks for any help-a

You have to give in single or double quotes:

example:

'male', 'female' etc 

Integer array index, either you may give with our with out quotes, but string index you must give inside the quotes.

so try:

var customIcons = {
            'male': {
                'icon': 'https://5d31037421'
            },
            'female': {
                'icon': 'https://5d310374214f1'
            },
            0: {
                'icon': 'https://5d310374214f'
            },
            1: {
                'icon': 'https://5d310374214f1d0670ef'
            },
            2: {
                'icon': 'https://5d310374214f1d0670e'
            }

        };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM