JSON 解码数组中的变化值

JSON Decode changing values in array

我正在使用 JSON 来存储括号的结果

上括号存储 15 个匹配项 下支架存储 14 根火柴 决赛有3场比赛

我已经解码了 JSON 并希望存储具有给定匹配编号和给定得分结果的新结果。我试过 foreach 循环,唯一的问题是括号没有按轮分组,也没有在每个结果后给出逗号。

echo json_encode($results);

会送出

[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]

当我想要它发出时

[
  [[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]]
]

第二题$match变量循环递增轮数 所以它给出:

1,2,3,4,5,6,7,8,1,2,3,4,1,2,1

当我想要它发出时

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15

感谢任何帮助,谢谢。下面是代码:

<?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);
    }
}


?>

此代码存在一些问题。

你在外循环和内循环中使用 $match。该变量将在内循环中被覆盖。

$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);
    }
}

我不太清楚你想要的输出,但做了一些猜测,请看看这是否符合你的要求。

<?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";