PHP:以特定方式从数组生成JSON

时间:2015-12-02 18:15:46

标签: php arrays json

我想以特定的方式从Array创建一个JSON。我的数组在开头看起来像这样:

array(2) {
  [22]=>
  array(8) {
    ["factor"]=>
    array(2) {
      [0]=>
      string(2) "12"
      [1]=>
      string(1) "1"
    }
    ["unit"]=>
    array(2) {
      [0]=>
      string(6) "months"
      [1]=>
      string(5) "times"
    }
    ["value"]=>
    array(2) {
      [0]=>
      string(3) "2.5"
      [1]=>
      string(1) "2"
    }
    ["planid"]=>
    array(2) {
      [0]=>
      string(1) "1"
      [1]=>
      string(1) "1"
    }
    ["position"]=>
    array(2) {
      [0]=>
      string(22) "Test 1"
      [1]=>
      string(21) "Test 2"
    }
    ["vouchervalue"]=>
    array(2) {
      [0]=>
      string(1) "0"
      [1]=>
      string(1) "0"
    }
    ["vouchertype"]=>
    array(2) {
      [0]=>
      string(0) ""
      [1]=>
      string(0) ""
    }
    ["vat"]=>
    array(2) {
      [0]=>
      int(19)
      [1]=>
      int(19)
    }
  }
  [23]=>
  array(8) {
    ["factor"]=>
    array(2) {
      [0]=>
      string(2) "12"
      [1]=>
      string(1) "1"
    }
    ["unit"]=>
    array(2) {
      [0]=>
      string(6) "months"
      [1]=>
      string(5) "times"
    }
    ["value"]=>
    array(2) {
      [0]=>
      string(3) "2.5"
      [1]=>
      string(1) "2"
    }
    ["planid"]=>
    array(2) {
      [0]=>
      string(1) "1"
      [1]=>
      string(1) "1"
    }
    ["position"]=>
    array(2) {
      [0]=>
      string(22) "Test 3"
      [1]=>
      string(21) "Test 4"
    }
    ["vouchervalue"]=>
    array(2) {
      [0]=>
      string(1) "0"
      [1]=>
      string(1) "0"
    }
    ["vouchertype"]=>
    array(2) {
      [0]=>
      string(0) ""
      [1]=>
      string(0) ""
    }
    ["vat"]=>
    array(2) {
      [0]=>
      int(19)
      [1]=>
      int(19)
    }
  }
}

这就是JSON的样子:

string(354) "{"factor":[["12","1"],["12","1"]],"unit":[["months","times"],["months","times"]],"value":[["2.5","2"],["2.5","2"]],"planid":[["1","1"],["1","1"]],"position":[["Test 1","Test 2"],["Test 3","Test 4"]],"vouchervalue":[["0","0"],["0","0"]],"vouchertype":[["",""],["",""]],"vat":[[19,19],[19,19]]}"

但我想这样做:

string(214) "{"factor":["12", "1","12","1"],"unit":["months", "times","months","times"],"value":["2.5","2","2.5", "2"],"planid":["1","1","1","1"],"position":["Test 1","Test 2", "Test 3", "Test 4"],"vouchervalue":["0","0","0","0"],"vouchertype":["","","",""],"vat":[19,19,19,19]}"

这个想法是每个订单都可以包含多个位置,可用于创建可以在应用程序的其余部分中使用的JSON(有一个使用JSON的表)。

好吧,我不知道如何解决这个问题所以我对任何暗示感到高兴: - )

2 个答案:

答案 0 :(得分:0)

我们假设你有数组$ a中的数据。

$a = array( 
    array( 
        'factor'=> array( '12', '1' ),
        'unit'=> array( 'months', 'times' ),
        'value'=> array( '2.5', '2' ),
        'planid'=> array( '1', '1' ),
        'position'=> array( 'Test 1', 'Test 2' ),
        'vouchervalue'=> array( '0', '0' ),
        'vouchertype'=> array( '', '' ),
        'vat'=> array( 19, 19 ),
    ),
    array( 
        'factor'=> array( '12', '1' ),
        'unit'=> array( 'months', 'times' ),
        'value'=> array( '2.5', '2' ),
        'planid'=> array( '1', '1' ),
        'position'=> array( 'Test 3', 'Test 4' ),
        'vouchervalue'=> array( '0', '0' ),
        'vouchertype'=> array( '', '' ),
        'vat'=> array( 19, 19),
    ),
);

在您的示例中,这包含两个要组合的数组。这是一种方式:

$b = array();
foreach( $a[0] as $k=>$v ) {
    if ( isset( $a[1][$k] ) )  // Add keys in both arrays
        $b[$k] = array_merge( $a[0][$k], $a[1][$k] );
    else                       // Add keys in only first array
        $b[$k] = $a[0][$k];
}
foreach( $a[1] as $k=>$v ) {
    if ( !isset( $a[0][$k] ) ) // Add keys in only second array
        $b[$k] = $a[1][$k];
}
echo json_encode( $b );

这遍历第一个数组。如果键('factor','unit')在两个数组中都可用,它会合并它们,否则它只会添加数组。

其次,它遍历第二个数组并添加第一遍中未添加的数组(如果该键不在第一个数组中)。

在你的情况下,数组似乎有相同的键集,第二遍可能没有必要,但只是为了确保......

结果如下:

{"factor":["12","1","12","1"],"unit":["months","times","months","times"],"value":["2.5","2","2.5","2"],"planid":["1","1","1","1"],"position":["Test 1","Test 2","Test 3","Test 4"],"vouchervalue":["0","0","0","0"],"vouchertype":["","","",""],"vat":[19,19,19,19]}

如果你不想让json中的数字进行字符串编码,比如“12”而不是12,请将jSON_NUMERIC_CHECK添加到json_encode

echo json_encode( $b, JSON_NUMERIC_CHECK );

{"factor":[12,1,12,1],"unit":["months","times","months","times"],"value":[2.5,2,2.5,2],"planid":[1,1,1,1],"position":["Test 1","Test 2","Test 3","Test 4"],"vouchervalue":[0,0,0,0],"vouchertype":["","","",""],"vat":[19,19,19,19]}

答案 1 :(得分:0)

您正在寻找array_merge_recursive()。根据文档:

  

array_merge_recursive()将一个或多个数组的元素合并在一起,以便将一个值的值附加到前一个数组的末尾。它返回结果数组。

     

如果输入数组具有相同的字符串键,则这些键的值将合并到一个数组中,并且这是递归完成的,因此如果其中一个值是数组本身,则该函数将合并它另一个数组中的相应条目。但是,如果数组具有相同的数字键,则后面的值不会覆盖原始值,但会附加。

在答案中使用上面列出 @HasseBjork 的示例输入(基于您的原始输入):

$a = array( 
    array( 
        'factor'=> array( '12', '1' ),
        'unit'=> array( 'months', 'times' ),
        'value'=> array( '2.5', '2' ),
        'planid'=> array( '1', '1' ),
        'position'=> array( 'Test 1', 'Test 2' ),
        'vouchervalue'=> array( '0', '0' ),
        'vouchertype'=> array( '', '' ),
        'vat'=> array( 19, 19 ),
    ),
    array( 
        'factor'=> array( '12', '1' ),
        'unit'=> array( 'months', 'times' ),
        'value'=> array( '2.5', '2' ),
        'planid'=> array( '1', '1' ),
        'position'=> array( 'Test 3', 'Test 4' ),
        'vouchervalue'=> array( '0', '0' ),
        'vouchertype'=> array( '', '' ),
        'vat'=> array( 19, 19),
    ),
);

您需要做的就是:

echo json_encode(array_merge_recursive($a[0], $a[1]));