PHP-以相同方式对两个数组进行排序 [英] PHP - Sort Two Arrays The Same Way

查看:111
本文介绍了PHP-以相同方式对两个数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个不同的数组.一个数组,一个,用于列出人员.我的另一个数组b,用于列出他们的年龄.我按数字对b进行排序,然后将其反转以使其按降序排列.我已经做好了这一步.

I have two different arrays. One array, a, for a list of people. My other array, b, for a list of their ages. I go to sort b by number and then reverse it so it goes in descending order. I got to this part okay.

如何对(一个人的名字列表)进行排序,以便仍将相同的值与排序后的列表配对?

How do I sort a (a list of people's names) so that the same values are still paired up with the sorted list?

示例:

a分别包含Bob,Sue,Phil和Jenny

a contains Bob, Sue, Phil, and Jenny respectively

b分别包含15、12、13和13.

b contains 15, 12, 13, and 13 respectively.

我希望我的结果是:

a分别包含Bob,Jenny,Phil和Sue

a contains Bob, Jenny, Phil, and Sue respectively

b分别包含15、13、13和12

b contains 15, 13, 13, and 12 respectively

推荐答案

http://php.net/manual/zh-CN/function.array-multisort.php

在参考文献中使用示例1:

using example #1 in the reference:

$a = array('Bob', 'Sue', 'Phil', 'Jenny');
$b = array(15, 12, 13, 13);
array_multisort($a, $b);
print_r($a);
> Array
 (
 [0] => Bob
 [1] => Jenny
 [2] => Phil
 [3] => Sue
 )
print_r($b);
> Array
 (
 [0] => 15
 [1] => 13
 [2] => 13
 [3] => 12
 )

这篇关于PHP-以相同方式对两个数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆