有如下两个数组:
<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
立即学习“PHP免费学习笔记(深入)”;
<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>
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号