2015-12-02 25 views
1

我想以特定的方式從數組創建JSON。我的陣列看起來像這樣開頭:PHP:以特定方式從數組生成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]}" 

的想法每個訂單都可以包含多於1個可用於創建JSON的位置,這可以在應用程序的其餘部分中使用(有一個使用JSON的表)。

好了,我不知道如何解決這個問題,所以我很高興任何暗示:-)

+4

創建另一個數組和'json_encode'它。 –

+0

嗨,其他數組應該怎麼樣,以便我能夠這樣編碼它? – MyFault

+1

json_encode只會編譯數組,因爲您已經構建了該數組。如果你想'factor'數組包含所有項目,你需要創建一個包含所有因素的數組,而不是兩個數組,每個數組都包含因素......有意義嗎? –

回答

0

讓我們說你在陣列中的數據元。

$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]} 
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]));