json_encode 在 arrayObject 与 array() 上的行为不同

发布于 2024-08-30 12:12:20 字数 2025 浏览 7 评论 0原文

我在 php 中有一个函数:

//simple method with array()
$sensors = array();
$query = "select id, x(transform(wkb_geometry,". $epsg . ")) as lon, y(transform(wkb_geometry,". $epsg . ")) as lat from mytable;";
$result = pg_query($query) or die('Query failed: ' . pg_last_error());
while ($row = pg_fetch_assoc($result)) {
    //var_dump($row);
    $mySensor = new sensor($row['id'],$row['lat'],$row['lon']);
    $sensors[] = $mySensor->geoJSON();
}

echo json_encode($sensors);

输出:

"features": [{
    "type": "Feature",
    "id": 1579028,
    "x": 4.85310557823,
    "y": 52.7205622103,
    "geometry": {
      "type": "Point",
      "coordinates": [4.85310557823, 52.7205622103],
      "crs": {
        "type": "OGC",
        "properties": {
          "urn": "urn:ogc:def:crs:OGC:1.3:CRS84"
        }
      }

现在我已经重写了数组,使其成为这样的对象:

//advanced method with arrayObject:
class sensors extends ArrayObject {
    function __construct($epsg){
        $query = "select id, x(transform(wkb_geometry,". $epsg . ")) as lon, y(transform(wkb_geometry,". $epsg . ")) as lat from mytable;";
        $result = pg_query($query) or die('Query failed: ' . pg_last_error());
        while ($row = pg_fetch_assoc($result)) {
            //var_dump($row);
            $mySensor = new sensor($row['id'],$row['lat'],$row['lon']);
                $this[] = $mySensor->geoJSON();
            }
        }
    }
}
$newsensors = new sensors($epsg);
echo echo json_encode($newsensors);

但这将输出更改为:

"features": {
  "0": {
    "type": "Feature",
    "id": 1579028,
    "x": 4.85310557823,
    "y": 52.7205622103,
    "geometry": {
      "type": "Point",
      "coordinates": [4.85310557823, 52.7205622103],
      "crs": {
        "type": "OGC",
        "properties": {
          "urn": "urn:ogc:def:crs:OGC:1.3:CRS84"
        }
      }
    }
  },

这使得它无法用作 OpenLayers 的 geoJSON。为什么 json_encode 函数会这样?我可以关闭索引号的设置吗?这可能是一个小错误吗?

I had a function in php:

//simple method with array()
$sensors = array();
$query = "select id, x(transform(wkb_geometry,". $epsg . ")) as lon, y(transform(wkb_geometry,". $epsg . ")) as lat from mytable;";
$result = pg_query($query) or die('Query failed: ' . pg_last_error());
while ($row = pg_fetch_assoc($result)) {
    //var_dump($row);
    $mySensor = new sensor($row['id'],$row['lat'],$row['lon']);
    $sensors[] = $mySensor->geoJSON();
}

echo json_encode($sensors);

that outputs:

"features": [{
    "type": "Feature",
    "id": 1579028,
    "x": 4.85310557823,
    "y": 52.7205622103,
    "geometry": {
      "type": "Point",
      "coordinates": [4.85310557823, 52.7205622103],
      "crs": {
        "type": "OGC",
        "properties": {
          "urn": "urn:ogc:def:crs:OGC:1.3:CRS84"
        }
      }

Now I have rewritten the array to become an object like this:

//advanced method with arrayObject:
class sensors extends ArrayObject {
    function __construct($epsg){
        $query = "select id, x(transform(wkb_geometry,". $epsg . ")) as lon, y(transform(wkb_geometry,". $epsg . ")) as lat from mytable;";
        $result = pg_query($query) or die('Query failed: ' . pg_last_error());
        while ($row = pg_fetch_assoc($result)) {
            //var_dump($row);
            $mySensor = new sensor($row['id'],$row['lat'],$row['lon']);
                $this[] = $mySensor->geoJSON();
            }
        }
    }
}
$newsensors = new sensors($epsg);
echo echo json_encode($newsensors);

But this changes the output to:

"features": {
  "0": {
    "type": "Feature",
    "id": 1579028,
    "x": 4.85310557823,
    "y": 52.7205622103,
    "geometry": {
      "type": "Point",
      "coordinates": [4.85310557823, 52.7205622103],
      "crs": {
        "type": "OGC",
        "properties": {
          "urn": "urn:ogc:def:crs:OGC:1.3:CRS84"
        }
      }
    }
  },

Which makes it unusable as geoJSON for OpenLayers. Why does the json_encode function behave this way? Can I turn off the setting of the index numbers? Is this a possible little bug?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

つ低調成傷 2024-09-06 12:12:20

json_encode 将对任何对象显示相同的行为,即使是像 ArrayObject 那样实现 ArrayAccess 接口的对象;使用公共属性。

要获得您想要的行为,您应该向其传递一个实际的数组,可以通过调用 ArrayObject::getArrayCopy() 来检索该数组(或者您可以将对象转换为数组)。

echo json_encode($newsensors->getArrayCopy());

json_encode will display the same behaviour with any object, even ones implementing the ArrayAccess interface as ArrayObject does; the public properties are used.

To get the behaviour that you want you should pass it an actual array which can be retrieved by calling ArrayObject::getArrayCopy() (or you can cast the object to an array).

echo json_encode($newsensors->getArrayCopy());
赏烟花じ飞满天 2024-09-06 12:12:20

必须在不同级别转换包含 ArrayObject 的数据树,因此我编写了一段代码来在输出 json 之前处理和转换每个 ArrayObject,希望它有所帮助:
https://github.com/nfroidure/Rest4/blob/master /php/class.Json.php

Had to convert data trees that contain ArrayObject at different level so i wrote a piece of code to handle and convert every ArrayObject before outputting json, hope it helps :
https://github.com/nfroidure/Rest4/blob/master/php/class.Json.php

紫竹語嫣☆ 2024-09-06 12:12:20

使用 JsonSerialized 接口(PHP 5 >= 5.4.0、PHP 7) 你应该是安全的:

public function jsonSerialize()
{
    return $this->getArrayCopy();
}

Use the JsonSerializable Interface (PHP 5 >= 5.4.0, PHP 7) and you should be safe:

public function jsonSerialize()
{
    return $this->getArrayCopy();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文