Maison >développement back-end >tutoriel php >数组排序 - php用系统函数将一个数组按另一个数组的值进行排序
有如下两个数组:
<code>array (size=6) 0 => string 'id' (length=2) 1 => string 'name' (length=4) 2 => string 'identityId' (length=10) 3 => string 'phone' (length=5) 4 => string 'email' (length=5) 5 => string 'schoolId' (length=8) array (size=6) 'id' => string '唯一标识' (length=12) 'identityId' => string '身份证' (length=9) 'phone' => string '手机号' (length=9) 'email' => string '邮箱' (length=6) 'name' => string '姓名' (length=6) 'schoolId' => string '学校' (length=6)</code>
如何让第二个数组按第一个数组的键值排序,即第二个数组变成id,name,identityId····
使用系统函数,谢谢诸位
有如下两个数组:
<code>array (size=6) 0 => string 'id' (length=2) 1 => string 'name' (length=4) 2 => string 'identityId' (length=10) 3 => string 'phone' (length=5) 4 => string 'email' (length=5) 5 => string 'schoolId' (length=8) array (size=6) 'id' => string '唯一标识' (length=12) 'identityId' => string '身份证' (length=9) 'phone' => string '手机号' (length=9) 'email' => string '邮箱' (length=6) 'name' => string '姓名' (length=6) 'schoolId' => string '学校' (length=6)</code>
如何让第二个数组按第一个数组的键值排序,即第二个数组变成id,name,identityId····
使用系统函数,谢谢诸位
<code><?php $a = [ 'id', 'name', 'identityId', 'phone', 'email', 'schoolId' ]; $b = [ 'id' => '唯一标识', 'identityId' => '身份证', 'phone' => '手机号', 'email' => '邮箱', 'name' => '姓名', 'schoolId' => '学校' ]; var_dump(array_merge(array_flip($a), $b));</code>
使用系统函数 array_muiltsort
<code class="php">$arr1 = array( 'id', 'name', 'identityId', 'phone', 'email', 'schoolId' ); $arr2 = array( 'id' => '唯一标识', 'identityId' => '身份证', 'phone' => '手机号', 'email' => '邮箱', 'name' => '姓名', 'schoolId' => '学校', ); array_multisort($arr1,SORT_DESC,$arr2); print_r($arr2); // 结果为: Array ( [schoolId] => 学校 [email] => 邮箱 [identityId] => 身份证 [phone] => 手机号 [id] => 唯一标识 [name] => 姓名 ) </code>
$a = ['id','name','identityId','phone','email','schoolid'];
$b = ......;
foreach($a as $v){
<code>$c[$v] = $b[$v];</code>
}
$c即为你想要的数组;
<code>$c = array(); foreach ($a as $value) $c[$value] = $b[$value]; print_r($c);</code>