帮助尝试生成棘手的多维数组格式

发布于 2024-09-25 19:31:26 字数 1049 浏览 0 评论 0原文

我在尝试在 PHP 中构建一个数组时遇到问题,该数组将以我正在寻找的 JSON 格式输出。我将向您展示我想要实现的目标以及到目前为止我已经达到的目标:

[
    {"data":[{"x":3,"y":0},{"x":10,"y":0}]},
    {"data":[{"x":11,"y":0},{"x":13,"y":0}]},
    {"data":[{"x":12,"y":1},{"x":17,"y":1}]}
]

我正在循环数据库结果并尝试构建数组来输出上面的 json,我的 php 看起来像这样(这显然还不正确):

//build the data
                    $data = array(
                                array(
                                    'x' => $age_start, 
                                    'y' => $ill_type
                                ),
                                array(
                                    'x' => $age_end, 
                                    'y' => $ill_type
                                )
                            );

                    $illnesses[] = $data;  

此代码输出以下 json:

[
   {
     [
        [{"x":2,"y":6},{"x":2,"y":6}],
        [{"x":2,"y":6},{"x":5,"y":6}],
        [{"x":4,"y":6},{"x":4,"y":6}]
    ]
  }
]

任何关于此的指针都很棒!

I'm having trouble trying to build an array in PHP which will output in the JSON format I am looking for. I will show you what I am trying to achieve and where I have got to so far:

[
    {"data":[{"x":3,"y":0},{"x":10,"y":0}]},
    {"data":[{"x":11,"y":0},{"x":13,"y":0}]},
    {"data":[{"x":12,"y":1},{"x":17,"y":1}]}
]

I am looping through db results and trying to build arrays to output the above json, my php looks like this (which is obviously not right yet):

//build the data
                    $data = array(
                                array(
                                    'x' => $age_start, 
                                    'y' => $ill_type
                                ),
                                array(
                                    'x' => $age_end, 
                                    'y' => $ill_type
                                )
                            );

                    $illnesses[] = $data;  

This code outputs the following json:

[
   {
     [
        [{"x":2,"y":6},{"x":2,"y":6}],
        [{"x":2,"y":6},{"x":5,"y":6}],
        [{"x":4,"y":6},{"x":4,"y":6}]
    ]
  }
]

Any pointers on this would be great!

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

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

发布评论

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

评论(3

你曾走过我的故事 2024-10-02 19:31:26

基本上,如果您已经知道所需的 JSON 输出,则只需 json_decode 它以获取它在 PHP 中的表示形式。 var_export 函数打印结构以可解析的格式。您也可以使用 print_rvar_dump 来转储结构。

$json = <<< JSON
[
    {"data":[{"x":3,"y":0},{"x":10,"y":0}]},
    {"data":[{"x":11,"y":0},{"x":13,"y":0}]},
    {"data":[{"x":12,"y":1},{"x":17,"y":1}]}
]
JSON;

var_export( json_decode($json) );

上述方法是通用的。只需解码并转储结构即可。然后汇编代码以创建此结构并进行编码。

Basically, if you know your desired JSON output already, you can simply json_decode it to get it's representation in PHP. The var_export function prints the structure in parseable format. You can also use print_r or var_dump to dump the structure though.

$json = <<< JSON
[
    {"data":[{"x":3,"y":0},{"x":10,"y":0}]},
    {"data":[{"x":11,"y":0},{"x":13,"y":0}]},
    {"data":[{"x":12,"y":1},{"x":17,"y":1}]}
]
JSON;

var_export( json_decode($json) );

The above approach is universal. Just decode and dump the structure. Then assemble your code to create this structure and encode.

沉鱼一梦 2024-10-02 19:31:26

这样做:

$data['data'] = array(
        array(
                'x' => $age_start,
                'y' => $ill_type
        ),
        array(
                'x' => $age_end,
                'y' => $ill_type
        )
);

do this:

$data['data'] = array(
        array(
                'x' => $age_start,
                'y' => $ill_type
        ),
        array(
                'x' => $age_end,
                'y' => $ill_type
        )
);
盗梦空间 2024-10-02 19:31:26

查看 JSON 字符串,您可以看到:

  • 它是一个数组(由 [] 包围),
  • 每个元素都是一个对象(由 { 包围)}
  • 对象有一个元素 data,它本身就是一个数组
  • ,该数组由两个带有 x 和 a 的对象组成y 属性

重要的是要知道 JSON 对象在 PHP 中由关联数组表示(当 json_encode()'ing 时,json_decode() 有一个特定的参数来使用 stdClass 或 assoc 数组。
所以 php 结构如下所示:

$data = array(
  array('data' => array(array('x' => 3, 'y' => 0), array('x' => 10, 'y' => 0))
  ,array('data' => array(array('x' => 11, 'y' => 0), array('x' => 13, 'y' => 0))
  ,array('data' => array(array('x' => 12, 'y' => 1), array('x' => 17, 'y' => 1))
);

Looking at the JSON string, you can see that:

  • it is an array (it is surrounded by [ and ])
  • each element is an object (surrounded by { and })
  • the objects have an element data that is itself an array
  • that array consists of two objects with an x and a y property

It is important to know that a JSON object is represented in PHP by an associative array (when json_encode()'ing, json_decode() has a specific parameter to use either a stdClass or an assoc. array).
So the php structure looks like this:

$data = array(
  array('data' => array(array('x' => 3, 'y' => 0), array('x' => 10, 'y' => 0))
  ,array('data' => array(array('x' => 11, 'y' => 0), array('x' => 13, 'y' => 0))
  ,array('data' => array(array('x' => 12, 'y' => 1), array('x' => 17, 'y' => 1))
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文