PHP - 无法访问数组值

时间:2016-10-27 16:45:47

标签: php mysql

我正在尝试使用PHP访问MySQL数据库,但我遇到了数据问题。在我的开发机器(PHP版本5.5)上,此代码完美地运行,但在生产服务器(PHP版本5.6)上,代码在处理数组时似乎失败了。基本上,我似乎得到echo下面的代码$parks确实是一个数组,并添加

foreach ($parks as $key => $val) {
    echo $key;
};

将正确回显数组键,但是如果我为echo $key换出echo $val,则没有任何反应,根本没有回声。

<?php
    $dbname = "DATABASE";
    $password = "PASSWORD";
    $servername = "localhost";
    $dbname = "NAME";

    $conn = new mysqli($servername, $username, $password, $dbname);

    $query = "SELECT name, latitude, longitude, description, where_to_go from locations";
    $result = mysqli_query($conn, $query);

    $parks = [];

    while($row = mysqli_fetch_array($result)){                   
        $park = new stdClass();
        $location = new stdClass();
        $location->lat = floatval($row['latitude']);
        $location->lng = floatval($row['longitude']);
        $park->title = $row['name'];
        $park->location = $location;
        $park->description = $row['description'];
        $park->whereToGo = $row['where_to_go'];
        array_push($parks, json_encode($park));
    };
    /*
    foreach ($parks as $key => $val) {
        echo $key; //THIS WILL WORK
    };

    foreach ($parks as $key => $val) {
        echo $val; //THIS WILL NOT RETURN ANYTHING
    };

    echo $parks[0] //SIMILARLY, THIS WILL NOT RETURN ANYTHING
    */

    $conn->close();
?>

为了进一步说明这一点,我的程序的下一步是将$parks数组转换为JavaScript变量......

<script type="text/javascript">
    var locations = <?php echo json_encode($parks); ?>;
    var locations = locations.map(JSON.parse);
    console.log(locations);
</script>

运行所有这些操作将记录[false]

为了测试一下,我在PHP的echo循环中为while的不同属性抛出了一些$park,它们输出了正确的值...所以数组似乎是正确构建的,但我似乎无法从中得到任何东西。

非常感谢任何指导。

3 个答案:

答案 0 :(得分:0)

请试试这个:

while($row = mysqli_fetch_array($result)){                   
    $park = new stdClass();
    $location = new stdClass();
    $location->lat = floatval($row['latitude']);
    $location->lng = floatval($row['longitude']);
    $park->title = $row['name'];
    $park->location = $location;
    $park->description = $row['description'];
    $park->whereToGo = $row['where_to_go'];
    $parks[] = json_encode(get_object_vars($park));
};

或者只使用数组:

while($row = mysqli_fetch_array($result)){                   
    $park = [];
    $location = [];
    $location['lat'] = floatval($row['latitude']);
    $location['lng'] = floatval($row['longitude']);
    $park['title'] = $row['name'];
    $park['location'] = $location;
    $park['description'] = $row['description'];
    $park['whereToGo'] = $row['where_to_go'];
    $parks[] = json_encode($park);
};

答案 1 :(得分:0)

奇怪,但也许尝试创建自己的虚拟索引......

$c = 0;
while($row = mysqli_fetch_array($result)){                   
    $park = new stdClass();
    $location = new stdClass();
    $location->lat = floatval($row['latitude']);
    $location->lng = floatval($row['longitude']);
    $park->title = $row['name'];
    $park->location = $location;
    $park->description = $row['description'];
    $park->whereToGo = $row['where_to_go'];
    $parks[$c] = json_encode($park);
    $c++;
};

问题可能与对象不能是数组键有关。我的意思是一般来说它应该创建一个数字键,但可能是你提到的PHP 5.5和5.6之间的问题。

尝试这种方式应确保您拥有一个可以轻松预测的数值数组。

答案 2 :(得分:0)

你的问题是你(我假设)试图创建一个对象数组,但实际上每次循环都会覆盖数组

这将从您的行

创建一个对象数组

$conn = new mysqli($servername, $username, $password, $dbname);

$query = "SELECT name, latitude, longitude, description, where_to_go from locations";
$result = mysqli_query($conn, $query);

$parks = [];

while($row = mysqli_fetch_array($result)){                   
    $t = new stdClass();

    $t->title = $row['name'];
    $t->location = $location;
    $t->description = $row['description'];
    $t->whereToGo = $row['where_to_go'];

    $l = new stdClass();
    $l->lat = floatval($row['latitude']);
    $l->lng = floatval($row['longitude']);

    $t->location = $l;

    $parks[] = $t
}
$jsonString = json_encode($parks);

现在,您可以使用$jsonString做任何您喜欢的事情,或者使用$parks数组

print_r($parks);