我有一個 PHP 數組,我試圖將其分成 2 個不同的數組到該特定數組中。我試圖提取每個“目的地”和“平衡”的任何值。這是我得到的數組,Array( [destination_1] => 1 [balance_1] => 1 [destination_2] => 2 [balance_2] => 2 [destination_3] => 3 [balance_3] => 3)我需要像這樣的輸出,Array ( [0] => Array ( [destination_1] => 1 [balance_1] => 1 ) [1] => Array ( [destination_2] => 2 [balance_1] => 2 ) [2] => Array ( [destination_3] => 3 [balance_3] => 3 ) )
1 回答

慕仙森
TA貢獻1827條經驗 獲得超8個贊
你需要的是array_chunk()
$arr = [
? ? "destination_1" => 1,
? ? "balance_1" => 1,
? ? "destination_2" => 2,
? ? "balance_2" => 2,
? ? "destination_3" => 3,
? ? "balance_3" => 3
];
$result =? array_chunk($arr, 2, true);?
輸出:
Array
(
? ? [0] => Array
? ? ? ? (
? ? ? ? ? ? [destination_1] => 1
? ? ? ? ? ? [balance_1] => 1
? ? ? ? )
? ? [1] => Array
? ? ? ? (
? ? ? ? ? ? [destination_2] => 2
? ? ? ? ? ? [balance_2] => 2
? ? ? ? )
? ? [2] => Array
? ? ? ? (
? ? ? ? ? ? [destination_3] => 3
? ? ? ? ? ? [balance_3] => 3
? ? ? ? )
)
- 1 回答
- 0 關注
- 129 瀏覽
添加回答
舉報
0/150
提交
取消