简体   繁体   English

PHP找不到由字符串标记的数组值

[英]PHP not finding array values labeled by strings

So I have a while loop that generates google maps markers: 所以我有一个while循环,它会生成谷歌地图标记:

<?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. 我想知道为什么当关联的数组组件是整数时,它可以工作,但是当它是字符串(即“ male”或“ female”)时,却出现未捕获的引用错误:未定义male。

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'
            }

        };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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