【问题标题】:JSON Decode into php array [closed]JSON解码成php数组[关闭]
【发布时间】:2016-02-08 01:46:43
【问题描述】:

我正在使用 JSON 方括号,并希望将其解码为一个二维多维数组。

这是 JSON:

"results" : [[ /* WINNER BRACKET */
    [[3,5], [2,4], [6,3], [2,3], [1,5], [5,3], [7,2], [1,2]],
    [[1,2], [3,4], [5,6], [7,8]],
    [[9,1], [8,2]],
    [[1,3]]
  ], [         /* LOSER BRACKET */
    [[5,1], [1,2], [3,2], [6,9]],
    [[8,2], [1,2], [6,2], [1,3]],
    [[1,2], [3,1]],
    [[3,0], [1,9]],
    [[3,2]],
    [[4,2]]
  ], [         /* FINALS */
    [[3,8], [1,2]],
    [[2,1]]
  ]]

我希望将上面的内容解码为这种类型的 PHP 数组,如下所示:

$winner_results = array
  (
  array("match1",3,5),
  array("match2",2,4),
  array("match3",6,3),
  array("match4",2,3),
  array("match5",1,5),
  array("match6",5,3),
  array("match7",7,2),
  array("match8",1,2),
  array("match9",1,12),
  array("match10",3,4),
  array("match11",5,6),
  array("match12",7,8),
  array("match13",9,1),
  array("match14",8,2),
  array("match15",1,3)
  );

$loser_results = array
  (
  array("match16",5,1),
  array("match17",1,2),
  array("match18",3,2),
  array("match19",6,9),
  array("match20",8,2),
  array("match21",1,2),
  array("match22",6,2),
  array("match23",1,3),
  array("match24",1,2),
  array("match25",3,1),
  array("match26",3,0),
  array("match27",1,9),
  array("match28",3,2),
  array("match29",4,2)
  );

$finals_results = array
  (
  array("match30",3,8),
  array("match31",1,2),
  array("match32",2,1)
  );

是否可以将上述 PHP 数组编码为与所示完全相同的 JSON 格式?

非常感谢您的帮助!

【问题讨论】:

  • 你有没有尝试过?提示:查找json_decode()
  • 是的,我试过 json_decode() 但返回 NULL。我正在使用方括号,所以我不记得了。
  • Edit 您对您尝试过的代码的问题。另外,您如何获取/读取/接收这些数据? “我不记得了”是什么意思?
  • "对上面的 PHP 数组进行编码" -- 这些实际上不是多个数组吗?

标签: php arrays json decode encode


【解决方案1】:

这里有一个解决方案。

注意:我使用动态变量名称 ($$varname) 为每个组(获胜者、失败者、决赛)创建自己的数组。

<?php
$results = '[[
  [[3,5], [2,4], [6,3], [2,3], [1,5], [5,3], [7,2], [1,2]],
  [[1,2], [3,4], [5,6], [7,8]],
  [[9,1], [8,2]],
  [[1,3]]
], [
  [[5,1], [1,2], [3,2], [6,9]],
  [[8,2], [1,2], [6,2], [1,3]],
  [[1,2], [3,1]],
  [[3,0], [1,9]],
  [[3,2]],
  [[4,2]]
], [
  [[3,8], [1,2]],
  [[2,1]]
]]';

// These are the group names
$names  = array('winner_results','looser_results','final_results');

// First we make a PHP array out of the JSON (object) string
$resarr = json_decode($results);

// Then we create a new array for each group, re-organizing the input array $resarr
$cnt = 0;  // The counter we use for qualifying the 'match' identifiers
foreach($resarr as $groupix => $group) {
    $arrname = $names[$groupix];
    $$arrname = array();  // Create an empty array for the group
    foreach($group as $items) {
        foreach($items as $item) {
            // Add data (array) to the group array
            $cnt++;
            array_push($$arrname,array('match'.$cnt,$item[0],$item[1]));
        }
    }
}

// And finally we display the created arrays
foreach ($names as $arr) {
    echo '<h3>'.$arr.'</h3><pre>'; print_r($$arr); echo '</pre>';
}
?>

【讨论】:

    【解决方案2】:

    这是一个array_merge' of the 'sub-arrays' of each of thewinner、loser、final` 数组。

    代码:

    $allResults = json_decode($json, true);
    $results = current($allResults); // get the three sub-arrays
    
    $winner = call_user_func_array('array_merge', $results[0]);
    $loser  = call_user_func_array('array_merge', $results[1]);
    $finals = call_user_func_array('array_merge', $results[2]);    
    

    测试数据(转换为有效的 JSON)

    $json = '{"results": [[
      [[3,5], [2,4], [6,3], [2,3], [1,5], [5,3], [7,2], [1,2]],
      [[1,2], [3,4], [5,6], [7,8]],
      [[9,1], [8,2]],
      [[1,3]]
    ], [
      [[5,1], [1,2], [3,2], [6,9]],
      [[8,2], [1,2], [6,2], [1,3]],
      [[1,2], [3,1]],
      [[3,0], [1,9]],
      [[3,2]],
      [[4,2]]
    ], [
      [[3,8], [1,2]],
      [[2,1]]
    ]]}';
    

    【讨论】:

    • 非常感谢!
    猜你喜欢
    • 2013-02-16
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 2011-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    相关资源
    最近更新 更多