4 回答

TA貢獻(xiàn)2039條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可以通過(guò)獲取舊變量的大小并使用 for 循環(huán)來(lái)創(chuàng)建一個(gè)新數(shù)組
試試這個(gè)
<?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);
?>
順便說(shuō)一句,這是我的第一個(gè)答案......呵呵

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超4個(gè)贊
這可以通過(guò)使用從原始數(shù)組中提取所有鍵array_keys(),然后使用從foreach()原始數(shù)組中獲取每個(gè)元素的數(shù)據(jù)來(lái)實(shí)現(xiàn):
<?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貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超6個(gè)贊
嘗試做這樣的事情
$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貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超3個(gè)贊
另一種方法是通過(guò)簡(jiǎn)單的foreach()循環(huán)來(lái)迭代多維數(shù)組,并根據(jù)您的要求將內(nèi)部第一個(gè)和第二個(gè)元素值推送到新數(shù)組。
$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 關(guān)注
- 223 瀏覽
添加回答
舉報(bào)