我在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] =>
)
)
我想从这个数组中生成锦标赛支架。我找到了用于创建锦标赛括号的jquery插件。为了生成锦标赛支架,我必须圆形地形成json阵列。对于第1轮,它应该给我3轮第2轮1记录和第3轮1记录。我尝试过以下代码: -
<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>
它给我这样的输出: -
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"} ],
],
];
第1轮比赛应该在第一个数组中,然后是第二个,依此类推。
答案 0 :(得分:0)
一种方法是,如果您首先在PHP中将数据构建到正确的结构中,这使得操作更容易。然后,您可以使用json_encode
将其输出为JSON。
<?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>