简体   繁体   中英

JSON Decode changing values in array

I am using JSON to store results for brackets

Upper bracket stores 15 matches Lower bracket stores 14 matches Finals has 3 matches

I have decoded the JSON and want to be store a new result with given match number and given score results. 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.

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:

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. This variable will get overwritten in the inner loop.

$result is used outside the loops and also as the value in both the loops.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM