dongqi6964 2017-10-01 17:28
浏览 89
已采纳

在PHP中创建一个带有数组的json?

I tried to implement it something like this

private static function getData($datas) {

    $days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
    $days_aliases = ['SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT'];

    $open_rates = array();

    foreach ($datas as $data) {

        $date = Carbon::parse($data->created_at)->format('l');

        for($i = 0; $i < count($days); $i++) {

            if($date == $days[$i]){

                $open_rates['x'][$i] = $days_aliases[$i];
                $open_rates[$data->name][$i] = (int)$data->open_rate;

            } 

        }

    }

    return $open_rates;

}

But the result is like this.

{ "x": { "0": "SUN", "1": "MON", "2": "TUE", "3": "WED", "4": "THU", "5": "FRI", "6": "SAT" } }

How to make an array like this PHP?

json: {
    'Day Pass' : [40, 10, 99, 50],
    'Day Pass' : [40, 10, 99, 50],
    'Day Pass' : [40, 10, 99, 50],
    'Day Pass' : [40, 10, 99, 50],
    'Day Pass' : [40, 10, 99, 50]
    }
  • 写回答

3条回答 默认 最新

  • dongpeiwei8589 2017-10-01 17:35
    关注

    Simply create a numeric-indexed array. Note that you can't have multiple keys with the same name in an array, so you need different names for "Day pass", otherwise you'd be overwriting them:

    <?php
    $json = [
        "json" => [
            "Day pass" => 
                [40, 10, 99, 50],
            "Day pass2" => 
                [40, 10, 99, 50],
            "Day pass3" => 
                [40, 10, 99, 50],
            "Day pass4" => 
                [40, 10, 99, 50],
        ],
    ];
    print_r(json_encode($json));
    

    Gives the correct result:

    {
        "json": {
            "Day pass": [40, 10, 99, 50],
            "Day pass2": [40, 10, 99, 50],
            "Day pass3": [40, 10, 99, 50],
            "Day pass4": [40, 10, 99, 50]
        }
    }
    

    Demo

    Using the same key instead, gives you one item:

    <?php
    $json = [
        "json" => [
            "Day pass" => 
                [40, 10, 99, 50],
            "Day pass" => 
                [40, 10, 99, 50],
            "Day pass" => 
                [40, 10, 99, 50],
            "Day pass" => 
                [40, 10, 99, 50],
        ],
    ];
    print_r(json_encode($json));
    

    Result:

    {
        "json": {
            "Day pass": [40, 10, 99, 50]
        }
    }
    

    Demo


    The problem with your specific code is that, in order to have json_encode() return a JSON array (instead of an object), the PHP array needs to be a numeric sequential 0-indexed array. What you can do is pass the array through array_values() in order to only preserve the values and reset the keys:

    <?php
    $open_rates = [];
    $open_rates["x"]["0"] = "Sun";
    $open_rates["x"]["2"] = "Mon";
    var_dump(json_encode($open_rates)); // JSON object
    $open_rates["x"] = array_values($open_rates["x"]);
    var_dump(json_encode($open_rates)); // JSON array
    

    Demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形