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

在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]
    }

图片转代码服务由CSDN问答提供 功能建议

我试图像这样实现它

  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 $ 数据){
 
 $ date = Carbon :: parse($ data-&gt; created_at) - &gt; format('l'); 
 
 for($ i = 0; $ i&lt; count($ 天;)$ i ++){
 
 if($ date == $ days [$ i]){
 
 $ open_rates ['x'] [$ i] = $ days_aliases [$ i]; 
  $ open_rates [$ data-&gt; name] [$ i] =(int)$ data-&gt; open_rate; 
 
} 
 
} 
 
} 
 
返回$ open_rates; 
  
} 
   
 
 

但结果是这样的。

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

如何创建像这样的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

    已采纳该答案
    打赏 评论
  • dth54864 2017-10-01 17:35

    The example that you have written is wrong. Object cannot have same key repeated. So assuming that key is different it should be something like this in PHP:

    $json = array(‘day pass’ => array(40,10,99,50), ‘day pass2’ => array(20,30,30))
    
    打赏 评论
  • doushao1948 2017-10-01 17:35

    The json you gave is not a valid json, keys shouldn't repeat inside a valid json

    Assuming you meant:

    json: {
        'Day Pass 1' : [40, 10, 99, 50],
        'Day Pass 2' : [40, 10, 99, 50],
        'Day Pass 3' : [40, 10, 99, 50],
        'Day Pass 4' : [40, 10, 99, 50],
        'Day Pass 5' : [40, 10, 99, 50]
    }
    

    It can be written in php as

    <?php
    $json = array(
        "json" => array(
            "Day Pass 1" => array(40, 10, 99, 50),
            "Day Pass 2" => array(40, 10, 99, 50),
            "Day Pass 3" => array(40, 10, 99, 50),
            "Day Pass 4" => array(40, 10, 99, 50),
            "Day Pass 5" => array(40, 10, 99, 50)
        )
    )
    
    打赏 评论

相关推荐 更多相似问题