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 python变量和列表之间的相互影响
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)