JSON文件中的重复数据

时间:2018-09-25 10:42:02

标签: php arrays json arraylist

我想重复JSON文件中的unitpcs部分。

例如:

$rows[] = array(
    'unit' => 'Example',
    'pcs' =>
        array('1 for Example', '2 for Example'),
);

结果:

[
 {"unit":"Example","pcs":["1 for Example","2 for Example"]}
]

如何进行这种类型的JSON

目标:

[
 {"unit":"Example",
  "pcs":
      ["1 for Example","2 for Example"]},
 {"unit":"ExampleSecond",
  "pcs":
      ["1 for ExampleSecond","2 for ExampleSecond"]}
]

1 个答案:

答案 0 :(得分:0)

简单:只需创建一个(嵌套的)PHP数组,然后对其调用json_encode。数字数组转换为JSON列表([]),关联数组和PHP对象转换为对象({})。示例:

$a = array(
    array('foo' => 'bar'),
    array('foo' => 'baz'));
$json = json_encode($a);

给你:

[{"foo":"bar"},{"foo":"baz"}]