doutui9606 2018-03-21 05:41
浏览 97
已采纳

从php数组修改json

I have an array where i fetch from php with variable $allresults :

Array
(
[1] => Array
    (
        [0] => 16/03/2018
        [1] => Friday
        [2] => 21.00
        [3] => Friendly Match
        [4] => Italy vs France
        [5] => Truesports HD
    )
[2] => Array
    (
        [0] => 17/03/2018
        [1] => Saturday
        [2] => 15.30
        [3] => Friendly Match
        [4] => Italy vs Pakistan
        [5] => Truesports HD
    )

[3] => Array
    (
        [0] => 17/03/2018
        [1] => Saturday
        [2] => 19.00
        [3] => Friendly Match
        [4] => Spain vs USA
        [5] => Truesports
    )
)

I have to convert it into json for other production use, so i use json_encode() with this code below

echo json_encode($allresults, JSON_PRETTY_PRINT);

and the result become

[
    "16\/03\/2018",
    "Friday",
    "21.00",
    "Friendly Match",
    "Italy vs France",
    "Truesports HD"
],
[
    "17\/03\/2018",
    "Saturday",
    "15.30",
    "Friendly Match",
    "Italy vs Pakistan",
    "Truesports HD"
],
[
    "17\/03\/2018",
    "Saturday",
    "19.00",
    "Friendly Match",
    "Spain vs USA",
    "Truesports"
]

My point is how can i change this into something like this in my json.

[
    "date":"17\/03\/2018",
    "day":"Saturday",
    "time":"19.00",
    "type":"Friendly Match",
    "value":"Spain vs USA",
    "tv":"Truesports"
]

Should i change this from php array or i could change this from json file directly?

  • 写回答

3条回答 默认 最新

  • doufusi2013 2018-03-21 05:49
    关注

    You have to update your array with key. Or second option is you can create new array with your expected output key:

    $array = Array
    (
    "1" => Array
        (
            "0" => "16/03/2018",
            "1" => "Friday",
            "2" => "21.00",
            "3" => "Friendly Match",
            "4" => "Italy vs France",
            "5" => "Truesports HD",
        ),
    "2" => Array
        (
            "0" => "17/03/2018",
            "1" => "Saturday",
            "2" => "15.30",
            "3" => "Friendly Match",
            "4" => "Italy vs Pakistan",
            "5" => "Truesports HD",
        ),
    
    "3" => Array
        (
            "0" => "17/03/2018",
            "1" => "Saturday",
            "2" => "19.00",
            "3" => "Friendly Match",
            "4" => "Spain vs USA",
            "5" => "Truesports"
        )
    );
    $array_keys= array("date","day","time","type","value","tv");
    $new_array = array();
    foreach ($array as $key => $value) {
      $tmp_array = array();
      foreach ($value as $key1 => $value1) {
        if(isset($array_keys[$key1]))
            $tmp_array[$array_keys[$key1]] = $value1;
      }
      $new_array[] =$tmp_array;
    }
    
    echo json_encode($new_array, JSON_PRETTY_PRINT);
    

    DEMO

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?