doudong3570 2013-08-28 10:22
浏览 40

麻烦转换php数组到json格式有点错误

I'm having trouble getting the correct json format from the following php array

Array
(
    [0] => Array
        (
            [name] => name1
            [data] => Array
                (
                    [0] => Array
                        (
                            [5] => 2013-15-6,9
                            [6] => 2013-15-7,9
                            [7] => 2013-15-8,9
                            [8] => 2013-15-9,9
                            [9] => 2013-15-10,9
                            [10] => 2013-15-11,9
                            [11] => 2013-15-12,9
                        )

                )

        )

)

Encoding it to json outputs

[
    {
        "name": "name1",
        "data": [
            {
                "5": "2013-15-6,9",
                "6": "2013-15-7,9",
                "7": "2013-15-8,9",
                "8": "2013-15-9,9",
                "9": "2013-15-10,9",
                "10": "2013-15-11,9",
                "11": "2013-15-12,9"
            }
        ]
    }
]

What I need the json output to look like is below, basically removing the curly brackets inside the data array and remove the index numbers thought I don't think that's possible.

[
    {
        "name": "name1",
        "data": [
            "2013-15-11,9",
            "2013-15-12,9"
        ]
    }
]

I've tried different combinations of array() around variables which got me close enough by adding the bracket inside the data array but I can't get rid of the curly brackets. Would really appreciate some help, have been working on this for a few hours. Thanks

  • 写回答

3条回答 默认 最新

  • dongniuxia8650 2013-08-28 10:31
    关注

    I took the json output that you are expecting to get and did json_decode on it, that's the result:

    Array
    (
        [0] => stdClass Object
            (
               [name] => name1
               [data] => Array
                    (
                        [0] => 2013-15-11,9
                        [1] => 2013-15-12,9
                    )
    
            )
    
    )
    

    So that's how your PHP structure should look like before sending it to json_encode.

    P.S. I checked it with json_encode (reversed the process) and it returns exactly what you need.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配