drcb19700 2015-10-01 07:31
浏览 89

从php数组创建json数组

I've following array in php

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] => 
            [team2_score] => 
            [round_number] => 3
            [teamtitle1] => 
            [teamtitle2] => 
        )
)

I want to generate tournament brackets from this array. I'd found jquery plugin for creating tournament brackets. For generating the tournament brackets I've to form json array round wise. for round 1 it should give me 3 records for round 2 1 records and for round 3 1 record. I've tried the following code :-

<script type="text/javascript">
            (function (win, doc, $) {
                win.TestData<?php echo $cnt; ?> = [
<?php
foreach ($roundmatcharr as $mt => $matchval) {
    if (isset($matchval["teamtitle1"]) && $matchval["teamtitle1"] != "") {
        $team1 = $matchval["teamtitle1"];
    } else {
        $team1 = "Team 1";
    }

    if (isset($matchval["teamtitle2"]) && $matchval["teamtitle2"] != "") {
        $team2 = $matchval["teamtitle2"];
    } else {
        $team2 = "Team 2";
    }

    if (isset($matchval["team1_score"]) && $matchval["team1_score"] != "") {
        $team1score = $matchval["team1_score"];
    } else {
        $team1score = 0;
        ;
    }

    if (isset($matchval["team2_score"]) && $matchval["team2_score"] != "") {
        $team2score = $matchval["team2_score"];
    } else {
        $team2score = 0;
        ;
    }
    ?>
                        [
                                [{"name": "<?php echo $team1; ?>", "id": "<?php echo $team1; ?>", "seed": 1, "score": "<?php echo $team1score; ?>"}, {"name": "<?php echo $team2; ?>", "id": "<?php echo $team2; ?>", "seed": 2, "score": "<?php echo $team2score; ?>"}],
                        ],
<?php } ?>
                ];
                $(".my_gracket").gracket({src: win.TestData<?php echo $cnt; ?>});
            })(window, document, jQuery);
</script>

It is giving me output like this:-

win.TestData = [
[
[ {"name" : "Chennai super kings", "id" : "Chennai super kings", "seed" : 1, "score" : "10" }, {"name" : "Spartans", "id" : "Spartans", "seed" : 2, "score" : "5"} ],
], 
[
[ {"name" : "Lions11", "id" : "Lions11", "seed" : 1, "score" : "15" }, {"name" : "Kings Xl Punjab", "id" : "Kings Xl Punjab", "seed" : 2, "score" : "10"} ],
], 
[
[ {"name" : "Zimbabwe", "id" : "Zimbabwe", "seed" : 1, "score" : "15" }, {"name" : "Red Steel", "id" : "Red Steel", "seed" : 2, "score" : "5"} ],
], 
[
[ {"name" : "Zimbabwe", "id" : "Zimbabwe", "seed" : 1, "score" : "10" }, {"name" : "Chennai super kings", "id" : "Chennai super kings", "seed" : 2, "score" : "15"} ],
], 
[
[ {"name" : "Team 1", "id" : "Team 1", "seed" : 1, "score" : "0" }, {"name" : "Team 2", "id" : "Team 2", "seed" : 2, "score" : "0"} ],
], 
];
which is not correct. The output should be like this:-

win.TestData = [
[
[ {"name" : "Chennai super kings", "id" : "Chennai super kings", "seed" : 1, "score" : "10" }, {"name" : "Spartans", "id" : "Spartans", "seed" : 2, "score" : "5"} ],
[ {"name" : "Lions11", "id" : "Lions11", "seed" : 1, "score" : "15" }, {"name" : "Kings Xl Punjab", "id" : "Kings Xl Punjab", "seed" : 2, "score" : "10"} ],
[ {"name" : "Zimbabwe", "id" : "Zimbabwe", "seed" : 1, "score" : "15" }, {"name" : "Red Steel", "id" : "Red Steel", "seed" : 2, "score" : "5"} ],
], 
[
[ {"name" : "Zimbabwe", "id" : "Zimbabwe", "seed" : 1, "score" : "10" }, {"name" : "Chennai super kings", "id" : "Chennai super kings", "seed" : 2, "score" : "15"} ],
], 
[
[ {"name" : "Team 1", "id" : "Team 1", "seed" : 1, "score" : "0" }, {"name" : "Team 2", "id" : "Team 2", "seed" : 2, "score" : "0"} ],
], 
];

Round 1 matches should be in first array then second and the so on.

  • 写回答

1条回答 默认 最新

  • dream_high1026 2015-10-01 09:59
    关注

    One approach is if you first build the data into the right structure in PHP, which makes it easier to manipulate. Then you can output it as JSON using json_encode.

    <?php
    $rounds = [];
    $output = [];
    
    // Format data and sort by round
    foreach ($roundmatcharr as $matchval) {
    
        $match1 = new stdClass();
        $match1->name = $matchval['teamtitle1'];
        $match1->id = $matchval['teamtitle1'];
        $match1->seed = 1;
        $match1->score = $matchval['team1_score'];
    
        $match2 = new stdClass();
        $match2->name = $matchval['teamtitle2'];
        $match2->id = $matchval['teamtitle2'];
        $match2->seed = 2;
        $match2->score = $matchval['team2_score'];
    
        $rounds[$matchval['round_number']][] = [$match1, $match2];
    
    }
    
    // Extra loop to clean up round numbers
    foreach ($rounds as $round) {
        $output[] = $round;
    }
    
    ?>
    
    <script type="text/javascript">
        (function (win, doc, $) {
            win.TestData<?php echo $cnt; ?> = <?php echo json_encode($output); ?>;
            $(".my_gracket").gracket({src: win.TestData<?php echo $cnt; ?>});
        })(window, document, jQuery);
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入