我有一个 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条经验 获得超7个赞
你需要的是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 关注
- 95 浏览
添加回答
举报
0/150
提交
取消