[英]JSON Decode changing values in array
I am using JSON to store results for brackets 我正在使用JSON存储括号的结果
Upper bracket stores 15 matches Lower bracket stores 14 matches Finals has 3 matches 大括号存放15场比赛小括号存放14场比赛决赛有3场比赛
I have decoded the JSON and want to be store a new result with given match number and given score results. 我已经解码了JSON并想要存储一个给定匹配数和给定分数结果的新结果。 I have tried with the foreach loops, the only problem is the the brackets are not grouping by rounds and not giving the commas after each result. 我已经尝试过foreach循环,唯一的问题是括号没有按回合分组,并且每个结果之后都没有给出逗号。
echo json_encode($results); echo json_encode($ results);
will give out 会放弃
[0,0][0,0][0,0][0,0][3,5][0,0][0,0][0,0]
[0,0][0,0][0,0][0,0]
[0,0][0,0]
[0,0]
when i want it to give out 当我想要它给出
[
[[0,0],[0,0],[0,0],[0,0],[3,5],[0,0],[0,0],[0,0]],
[[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0]],
[[0,0]]
]
Second question $match variable loops increments by rounds so it give out: 第二个问题$ match变量循环循环递增,因此得出:
1,2,3,4,5,6,7,8,1,2,3,4,1,2,1
when I want it to give out 当我想要它发出时
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
Any help is appreciated thank you. 任何帮助表示赞赏谢谢。 Below is the code: 以下是代码:
<?php
$upper_bracket_results = "
[
[[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0]],
[[0,0]]
]
";
$lower_bracket_results = "
[
[[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0]],
[[0,0],[0,0]],
[[0,0]],
[[0,0]]
]
";
$final_bracket_results = "
[
[[0,0],[0,0]],
[[0,0]]
]
";
$json = "{\"results\" : [ {$upper_bracket_results} , {$lower_bracket_results} , {$final_bracket_results} ]}";
$allResults = json_decode($json, true);
$results = current($allResults);
$upper = $results[0];
$lower = $results[1];
$final = $results[2];
$all = array_merge($results[0], $results[1], $results[2]);
$matchno = 2;
$score1 = 3;
$score2 = 5;
if($matchno <= 15)
{
$bracket = $upper;
}
elseif($matchno >= 16 && $matchno <= 29)
{
$bracket = $lower;
}
elseif($matchno >= 30 && $matchno <= 32)
{
$bracket = $final;
}
foreach($bracket as $match => $result[0])
{
foreach($result[0] as $match => $result)
{
$match += 1;
if($match == $matchno)
{
$result[0] = $score1;
$result[1] = $score2;
}
echo json_encode($result);
}
}
?>
There are some problems with this code. 这段代码存在一些问题。
You use $match in the outer loop and the inner loop. 您在外部循环和内部循环中使用$ match。 This variable will get overwritten in the inner loop. 该变量将在内循环中被覆盖。
$result is used outside the loops and also as the value in both the loops. $ result在循环外使用,也用作两个循环中的值。
foreach($bracket as $match => $result[0])
{
foreach($result[0] as $match => $result)
{
$match += 1;
if($match == $matchno)
{
$result[0] = $score1;
$result[1] = $score2;
}
echo json_encode($result);
}
}
I don't clearly understand your desired output but having made some guesses, please see if this does what you want. 我不清楚您想要的输出,但已经做出了一些猜测,请查看这是否满足您的要求。
<?php
$upper =
[
[[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0]],
[[0,0]]
];
$lower =
[
[[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0]],
[[0,0],[0,0]],
[[0,0]],
[[0,0]]
];
$final =
[
[[0,0],[0,0]],
[[0,0]]
];
$matchno = 2;
$score1 = 3;
$score2 = 5;
if($matchno <= 15)
{
$description = "Upper";
$bracket = $upper;
$offset = 0;
}
elseif($matchno >= 16 && $matchno <= 29)
{
$description = "Lower";
$bracket = $lower;
$offset = 16;
}
elseif($matchno >= 30 && $matchno <= 32)
{
$description = "Final";
$bracket = $final;
$offset = 30;
}
$matchNumberInBlock = 0;
echo "$description\n";
foreach($bracket as $i => $round)
{
foreach($round as $j => $match)
{
$matchNumberInBlock++;
if ( $matchNumberInBlock + $offset == $matchno )
{
$bracket[$i][$j][0] = $score1;
$bracket[$i][$j][1] = $score2;
}
}
}
echo json_encode($bracket);
echo "\n";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.