使用键重新排列多维数组值

我有一个多维数组,我想重新排列数组,从每个数组中获取父键并将值分配给每个数组中的该键。


Array

    (

        [make] => Volvo 1,Volvo 2

        [model] => Make 1,Make 2

        [vehicleno] => Reg 1,reg 2

        [reg_state] => QLDS,QLDS

    )

预期结果为


Array(

[0] => Array

        (

            [make] => Volvo

            [model] => FH16

            [vehicleno] => RS95SMB

            [reg_state] => QLDS

        )

[1] => Array

        (

            [make] => Volvonew

            [model] => 123

            [vehicleno] => RS95SMB

            [reg_state] => QLDS

        )

)


郎朗坤
浏览 179回答 4
4回答

largeQ

您可以通过获取旧变量的大小并使用 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++ ){&nbsp;$temp_array = array(&nbsp; &nbsp; 'make'=>$pre_array['make'][$x];&nbsp; &nbsp; 'model'=>$pre_array['model'][$x];&nbsp; &nbsp; 'vehicleno'=>$pre_array['vehicleno'][$x];&nbsp; &nbsp; 'reg_state'=>$pre_array['reg_state'][$x];&nbsp;);&nbsp;array_push($new_array,$temp_array);}print_r($new_array);?>顺便说一句,这是我的第一个答案......呵呵

HUH函数

这可以通过使用从原始数组中提取所有键array_keys(),然后使用从foreach()原始数组中获取每个元素的数据来实现:<?php&nbsp; &nbsp; $data = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "make" => array("Volvo","VolvoNew"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "model" => array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ( "FH16","123"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "vehicleno" => array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ("RS95SMB","RS95SMB"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "reg_state" => array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "QLDS","QLDS"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; print_r($data);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $newArray = array();&nbsp; &nbsp; $newArrayKeys = array_keys($data);&nbsp; &nbsp; &nbsp; //extract all the keys from the associative array&nbsp; &nbsp; $newArrayLength = count($data["make"]); //get number of makes which is the number of elements in new array&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; for($i=0;$i<$newArrayLength;$i++){&nbsp; &nbsp; &nbsp; &nbsp; $currElement = array();&nbsp; &nbsp; &nbsp; &nbsp; // loop through all the keys from the original array and grab the data for the current index&nbsp; &nbsp; &nbsp; &nbsp; foreach($newArrayKeys as $key){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $currElement[$key] = $data[$key][$i];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $newArray[$i] = $currElement;&nbsp; &nbsp;//push current element to the newArray&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; print_r($newArray);?>

HUX布斯

尝试做这样的事情$array = [&nbsp; &nbsp; "make"&nbsp; &nbsp; &nbsp; => ['Volvo', 'Volvonew'],&nbsp; &nbsp; "model"&nbsp; &nbsp; &nbsp;=> ['FH16','123'],&nbsp; &nbsp; "vehicleno" => ['RS95SMB','RS95SMB'],&nbsp; &nbsp; "reg_state" => ['QLDS', 'QLDS']];$list_1 = [];$list_2 = [];foreach($array as $key => $value){&nbsp; &nbsp; $list_1[$key] = $value[0];&nbsp; &nbsp; $list_2[$key] = $value[1];}var_dump($list_1);var_dump($list_2);

泛舟湖上清波郎朗

另一种方法是通过简单的foreach()循环来迭代多维数组,并根据您的要求将内部第一个和第二个元素值推送到新数组。$i=0;$return = array();foreach($array as $key=>$value) {&nbsp; &nbsp; &nbsp;$return[$i][$key] = $value[$i];&nbsp; &nbsp; &nbsp;$return[$i+1][$key] = $value[$i+1];}print_r(($return));工作演示:&nbsp;https://3v4l.org/1CBgn
打开App,查看更多内容
随时随地看视频慕课网APP