dongzha3058 2016-08-12 13:58
浏览 42
已采纳

在PHP中将大对象数组声明为javascript参数

I need to pass a PHP array to a javascript function where the array has the form

var Waypoints = [{"Geometry":{"Latitude":52.34,"Longitude":55.35}},{"Geometry":{"Latitude":55.34,"Longitude":56.35}}]

Here is a code snippet:

class Geometry
{
  public $Latitude;
  public $Longitude;
}
class WayPoint
{
  public $Geometry;
}
$Geometry = new Geometry();

$wp = new WayPoint();
$wp->Geometry->Latitude = 52.34;
$wp->Geometry->Longitude = 55.35;

$wp2 = new WayPoint();
$wp2->Geometry->Latitude = 55.34;
$wp2->Geometry->Longitude = 56.35;

$php_data = [$wp,$wp2 ];
echo json_encode($php_data);

which produces this:

[{"Geometry":{"Latitude":52.34,"Longitude":55.35}},{"Geometry":{"Latitude":55.34,"Longitude":56.35}}]

This is the correct array structure, however I am asking two questions:

  1. Is this the best way of declaring the object classes with regarding to filling the array with a very large number of co-ordinates, and
  2. Why is PHP giving a warning for the code on lines, 13 and 16 regarding empty values, i.e.

Warning: Creating default object from empty value in for $wp->Geometry->Latitude = 52.34; and $wp2->Geometry->Latitude = 55.34;

From what I can discern from the documentation, this is the correct way of declaring an object, and why is it just the first variable is being flagged?

  • 写回答

2条回答 默认 最新

  • 普通网友 2016-08-12 14:11
    关注

    The error is because you dont assign the Geometry property of the Waypoint objects.

    It still works because php will create a default object (called a stdClass) when you assign a property to a non existent variable.

    There is a much simpler solution - json_encode will take an asociative array as input:

    echo json_encode(
        [
            [
                'Geometry'=>
                    [
                        'Latitude'  =>  52.34,
                        'Longtitude'=>  55.35,
                    ]
            ],
            [
                'Geometry'=>
                    [
                        'Latitude'  =>  52.65,
                        'Longtitude'=>  56.35,
                    ]
            ],
        ]
    );
    

    Obviously you can build this array in a loop.

    If you do want to use named classes, for other reasons, then the correct code would be:

    class Geometry
    {
      public $Latitude;
      public $Longitude;
    }
    class WayPoint
    {
      public $Geometry;
    }
    
    $data=[];
    
    
    $wp = new WayPoint();
    $gm = new Geometry();
    $gm->Latitude = 52.34;
    $gm->Longitude = 55.35;
    
    $wp->Geometry = $gm;
    
    $data[]=$wp;
    
    
    echo json_encode($data);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 通过下面文章的方法求手机号段
  • ¥15 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体