PHP如何按顺序连接数组中的元素
如何按顺序将数组中的单个元素连接到数组中的数组中。假设我有以下数组:PHP如何按顺序连接数组中的元素,php,arrays,laravel,Php,Arrays,Laravel,如何按顺序将数组中的单个元素连接到数组中的数组中。假设我有以下数组: array:6 [ 0 => array:2 [ "id" => 2 "type" => "studio_apartment" ] 1 => array:2 [ "id" => 3 "type" => "one_bedroom"
array:6 [
0 => array:2 [
"id" => 2
"type" => "studio_apartment"
]
1 => array:2 [
"id" => 3
"type" => "one_bedroom"
]
2 => array:2 [
"id" => 10
"type" => "two_bedroom"
]
3 => "20000"
4 => "30000"
5 => "40000"
]
是否可以将数组中的这些孤立元素放入它们上面的数组中?这样,第一个单独的元素将进入第一个数组,依此类推。这样他们就可以形成这样的东西:
array:3 [
0 => array:2 [
"id" => 2
"type" => "studio_apartment"
"rent" => "20000"
]
1 => array:2 [
"id" => 3
"type" => "one_bedroom"
"rent" => "30000"
]
2 => array:2 [
"id" => 10
"type" => "two_bedroom"
"rent" => "40000"
]
]
假设孤立元素总是最后一个,并且孤立元素的数量总是与其他元素相同;您可以删除单独的元素(最后一半),循环它们并使用适当的键添加到剩余的数组中:
foreach(array_splice($array, count($array)/2) as $key => $value) {
$array[$key]['rent'] = $value;
}