4 回答
TA贡献2039条经验 获得超7个赞
您可以通过获取旧变量的大小并使用 for 循环来创建一个新数组
试试这个
<?php
// pre_array is your old array and new_array is arranged one...
$i = 0;
$size = sizeof($pre_array);
$new_array = array();
for( $x=0 ; $x<$size ; $x++ ){
$temp_array = array(
'make'=>$pre_array['make'][$x];
'model'=>$pre_array['model'][$x];
'vehicleno'=>$pre_array['vehicleno'][$x];
'reg_state'=>$pre_array['reg_state'][$x];
);
array_push($new_array,$temp_array);
}
print_r($new_array);
?>
顺便说一句,这是我的第一个答案......呵呵
TA贡献1836条经验 获得超4个赞
这可以通过使用从原始数组中提取所有键array_keys(),然后使用从foreach()原始数组中获取每个元素的数据来实现:
<?php
$data = array(
"make" => array("Volvo","VolvoNew"),
"model" => array
( "FH16","123"
),
"vehicleno" => array
("RS95SMB","RS95SMB"
),
"reg_state" => array
(
"QLDS","QLDS"
)
);
print_r($data);
$newArray = array();
$newArrayKeys = array_keys($data); //extract all the keys from the associative array
$newArrayLength = count($data["make"]); //get number of makes which is the number of elements in new array
for($i=0;$i<$newArrayLength;$i++){
$currElement = array();
// loop through all the keys from the original array and grab the data for the current index
foreach($newArrayKeys as $key){
$currElement[$key] = $data[$key][$i];
}
$newArray[$i] = $currElement; //push current element to the newArray
}
print_r($newArray);
?>
TA贡献1876条经验 获得超6个赞
尝试做这样的事情
$array = [
"make" => ['Volvo', 'Volvonew'],
"model" => ['FH16','123'],
"vehicleno" => ['RS95SMB','RS95SMB'],
"reg_state" => ['QLDS', 'QLDS']
];
$list_1 = [];
$list_2 = [];
foreach($array as $key => $value){
$list_1[$key] = $value[0];
$list_2[$key] = $value[1];
}
var_dump($list_1);
var_dump($list_2);
TA贡献1818条经验 获得超3个赞
另一种方法是通过简单的foreach()循环来迭代多维数组,并根据您的要求将内部第一个和第二个元素值推送到新数组。
$i=0;
$return = array();
foreach($array as $key=>$value) {
$return[$i][$key] = $value[$i];
$return[$i+1][$key] = $value[$i+1];
}
print_r(($return));
工作演示: https://3v4l.org/1CBgn
- 4 回答
- 0 关注
- 178 浏览
添加回答
举报