duancoubeng5909 2015-10-01 10:46
浏览 42

用php foreach中的条件打开和关闭花括号

I have an array in php as given below:-

Array
(
    [0] => Array
        (
            [team1_score] => 10
            [team2_score] => 5
            [round_number] => 1
            [teamtitle1] => Chennai super kings
            [teamtitle2] => Spartans
        )

    [1] => Array
        (
            [team1_score] => 15
            [team2_score] => 10
            [round_number] => 1
            [teamtitle1] => Lions11
            [teamtitle2] => Kings Xl Punjab
        )

    [2] => Array
        (
            [team1_score] => 15
            [team2_score] => 5
            [round_number] => 1
            [teamtitle1] => Zimbabwe
            [teamtitle2] => Red Steel
        )

    [3] => Array
        (
            [team1_score] => 10
            [team2_score] => 15
            [round_number] => 2
            [teamtitle1] => Zimbabwe
            [teamtitle2] => Chennai super kings
        )

    [4] => Array
        (
            [team1_score] => 15
            [team2_score] => 7
            [round_number] => 3
            [teamtitle1] => Chennai super kings
            [teamtitle2] => Chennai super kings
        )

)

from this array i want json format array given below:-

[
                        [ {"name" : "Chennai super kings", "score" : 10 }, {"name" : "spartans",  "score" : 5} ],
                        [ {"name" : "Lions11", "score" : 15 }, {"name" : "Kings Xl Punjab",  "score" : 10} ],
                        [ {"name" : "Zimbabwe", "score" : 15 }, {"name" : "Red Steel",  "score" : 5} ],
                    ], 
                    [
                        [ {"name" : "Chennai super kings", "score" : 10 }, {"name" : "Zimbabwe",  "score" : 5} ],
                    ],
                    [
                        [ {"name" : "Chennai super kings", "score" : 10 }, {"name" : "Lions11",  "score" : 5} ],
                    ]

I've tried this but it is not coming in proper format. Please help me guys i got stuck in this. Any help will be appreciated Thanks in Advance

  • 写回答

1条回答 默认 最新

  • doujiku1028 2015-10-01 11:34
    关注

    First you need to modify your data structure so you have separate array for the team1 and team2 combination. Add that to one holding array and then just use json_encode simple as that.

    // $data is your array
    $output = [];
    foreach ($data as $row) {
        $output[] = [
            ['name'=>$row['teamtitle1'], 'score'=>$row['team1_score']], 
            ['name'=>$row['teamtitle2'], 'score'=>$row['team2_score']]
        ];
    }
    
    echo json_encode($output);
    

    Edit: As stated in the comments if you want to add another record all you have to do is obviously first find the winner and after that add it to the data set like this:

    $output[] = ['name'=>'I am the Winner!', 'score'=>'99999'];
    json_encode($output);
    
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮