【问题标题】:JSON Decode changing values in arrayJSON解码数组中的更改值
【发布时间】:2016-03-17 03:10:34
【问题描述】:

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

大括号存储 15 个匹配项 下括号存储 14 场比赛 决赛有3场比赛

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

回显 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);
    }
}


?>

【问题讨论】:

  • 不要创建自己的 json。只需使用json_encode(),因为 php 会解析它。

标签: php arrays json foreach decode


【解决方案1】:

这段代码有一些问题。

您在外循环和内循环中使用 $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";

【讨论】:

  • 谢谢这工作得很好!我只需要将 $matchNumberInBlock 更改为 $matchNumberInBlock = -1;为低级和决赛。
  • 我看到了问题。您可以在 if 语句之后移动 $matchNumberInBlock++。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-22
  • 1970-01-01
相关资源
最近更新 更多