在json中将多维数组转换为逗号分隔的组

时间:2016-04-18 18:46:29

标签: php arrays json multidimensional-array

我有这个数组:

array(7) {
  ["Locate"]=>
  array(2) {
    [0]=>
    array(3) {
      [0]=>
      string(16) "Self Set - Short"
      [1]=>
      string(6) "Locate"
      [2]=>
      string(4) "1701"
    }
    [1]=>
    array(3) {
      [0]=>
      string(15) "Self Set - Long"
      [1]=>
      string(6) "Locate"
      [2]=>
      string(4) "1521"
    }
  }
  ["Step 1 - Campus"]=>
  array(2) {
    [0]=>
    array(3) {
      [0]=>
      string(16) "Self Set - Short"
      [1]=>
      string(15) "Step 1 - Campus"
      [2]=>
      string(4) "1471"
    }
    [1]=>
    array(3) {
      [0]=>
      string(15) "Self Set - Long"
      [1]=>
      string(15) "Step 1 - Campus"
      [2]=>
      string(4) "1239"
    }
  }
  ["Step 2 - Date"]=>
  array(2) {
    [0]=>
    array(3) {
      [0]=>
      string(16) "Self Set - Short"
      [1]=>
      string(13) "Step 2 - Date"
      [2]=>
      string(3) "717"
    }
    [1]=>
    array(3) {
      [0]=>
      string(15) "Self Set - Long"
      [1]=>
      string(13) "Step 2 - Date"
      [2]=>
      string(3) "607"
    }
  }
  ["Step 3 - Time"]=>
  array(2) {
    [0]=>
    array(3) {
      [0]=>
      string(16) "Self Set - Short"
      [1]=>
      string(13) "Step 3 - Time"
      [2]=>
      string(3) "508"
    }
    [1]=>
    array(3) {
      [0]=>
      string(15) "Self Set - Long"
      [1]=>
      string(13) "Step 3 - Time"
      [2]=>
      string(3) "436"
    }
  }
}

我使用它将其导出为json:

$data = array();
foreach( $group as $key => $value ) {
    foreach($value as $key2) {
        $data[] = array(
            'event' => $key,
            $key2[0] => $key2[2],
        );
    }
}

echo json_encode( $data );

我目前的输出是:

[{"event":"Locate","Self Set - Short":"1698"},{"event":"Locate","Self Set - Long":"1515"},{"event":"Step 1 - Campus","Self Set - Short":"1469"},{"event":"Step 1 - Campus","Self Set - Long":"1230"},{"event":"Step 2 - Date","Self Set - Short":"717"},{"event":"Step 2 - Date","Self Set - Long":"603"},{"event":"Step 3 - Time","Self Set - Short":"508"},{"event":"Step 3 - Time","Self Set - Long":"433"},{"event":"Submit Button","Self Set - Short":"343"},{"event":"Submit Button","Self Set - Long":"309"},{"event":"Reschedule - New","Self Set - Short":"18"},{"event":"Reschedule - New","Self Set - Long":"21"},{"event":"Reschedule - Keep","Self Set - Short":"3"},{"event":"Reschedule - Keep","Self Set - Long":"3"}]

我需要它像这样导出:

[{"event":"Locate","Self Set - Short":"1698", "Self Set - Long":"1515"},{"event":"Step 1 - Campus","Self Set - Short":"1469","Self Set - Long":"1230"},{"event":"Step 2 - Date","Self Set - Short":"717","Self Set - Long":"603"},{"event":"Step 3 - Time","Self Set - Short":"508","Self Set - Long":"433"},{"event":"Submit Button","Self Set - Short":"343","Self Set - Long":"309"},{"event":"Reschedule - New","Self Set - Short":"18","Self Set - Long":"21"},{"event":"Reschedule - Keep","Self Set - Short":"3","Self Set - Long":"3"}]

最终结果需要使用两个子数组和逗号将它们与主数组键分开,而不是创建我需要的每组的组1.任何帮助都会很棒!

2 个答案:

答案 0 :(得分:0)

根据你的初始数组,我写了这段代码: -

<?php

$data = array(
  "Locate"=>array(
        0=>array(
          0=>"Self Set - Short",
          1=>"Locate",
          2=>"1701"
        ),
        1=>array(
          0=>"Self Set - Long",
          1=>"Locate",
          2=>"1521"
        )
    ),
    "Step 1 - Campus"=>array(
        0=>array(
          0=>"Self Set - Short",
          1=>"Step 1 - Campus",
          2=>"1471"
        ),
        1=>array(
          0=>"Self Set - Long",
          1=>"Step 1 - Campus",
          2=>"1239"
        )
    ),
    "Step 2 - Date"=>array(
        0=>array(
          0=>"Self Set - Short",
          1=>"Step 2 - Date",
          2=>"717"
        ),
        1=>array(
          0=>"Self Set - Long",
          1=>"Step 2 - Date",
          2=>"607"
        )
    ),
    "Step 3 - Time"=>array(
        0=>array(
          0=>"Self Set - Short",
          1=>"Step 3 - Time",
          2=>"508"
        ),
        1=>array(
          0=>"Self Set - Long",
          1=>"Step 3 - Time",
          2=>"436"
        )
    )
);
  echo "<pre/>";print_r($data);
  $final_data = array();
  foreach ($data as $dat){
      foreach($dat as $key=>$value){
          $final_data[$value[1]]['event'] = $value[1];
          $final_data[$value[1]][$value[0]] = $value[2];
      }

  }
 $final_data = json_encode(array_values($final_data));
  echo ($final_data);
?>

输出: - https://eval.in/555678

注意: -

<强> 1。您想要的json中的值是否与原始数组相同?

<强> 2。最终的json也有一些更多的值,这在初始数组中是不存在的。

检查自己并纠正。感谢

答案 1 :(得分:0)

迟到总比不到好...

这是处理数据的更直接的方法,不需要利用临时关联密钥(稍后将删除)。

  • 使用foreach()循环并声明groupname和双行set数据。
  • 将一维数组放入结果数组($data)中,该数组由具有值event的硬编码的第一个元素groupname和动态生成的关联元素(具有派生的键)组成0列值中的值和2列值中的值)。
  • +只是array_merge()的简洁/无功能版本,可以按预期在关联元素上使用。
  • 这是干净而巧妙的方法,因为它使用array_column()是因为它已添加到PHP中,并且一旦循环结束,输出数组就可以立即准备好json_encode()
  • 此外,这种方法不会在内部循环中覆盖相同的event数据。并且即使您向$set添加更多行,该方法也将按预期运行。

代码(Demo

$group = [
    "Locate" => [
        ["Self Set - Short", "Locate", "1701"],
        ["Self Set - Long", "Locate", "1521"]
    ],
    "Step 1 - Campus" => [
        ["Self Set - Short", "Step 1 - Campus", "1471"],
        ["Self Set - Long", "Step 1 - Campus", "1239"]
    ],
    "Step 2 - Date" => [
        ["Self Set - Short", "Step 2 - Date", "717"],
        ["Self Set - Long", "Step 2 - Date", "607"]
    ],
    "Step 3 - Time" => [
        ["Self Set - Short", "Step 3 - Time", "508"],
        ["Self Set - Long", "Step 3 - Time", "436"]
    ]
];

foreach ($group as $groupname => $set) {
    $data[] = ['event' => $groupname] + array_column($set, 2, 0);
}
//var_export($data);  
echo json_encode($data);

输出:

[{"event":"Locate","Self Set - Short":"1701","Self Set - Long":"1521"},{"event":"Step 1 - Campus","Self Set - Short":"1471","Self Set - Long":"1239"},{"event":"Step 2 - Date","Self Set - Short":"717","Self Set - Long":"607"},{"event":"Step 3 - Time","Self Set - Short":"508","Self Set - Long":"436"}]