简体   繁体   English

PHP 根据另一个数组对一个数组进行排序

[英]PHP sort an array based on another array

I have the following scenario我有以下情况

$elementsInPairs = ["xyz","xxx","yyy","zzz"];
$valuesInPair =[4,2,3,1];

and i need to sort the second array which would give me我需要对第二个数组进行排序,这会给我

[1,2,3,4]

which i can do by我能做到的

sort($valuesInPair)

but i need the same to happen in first array as well, based on the sorting of the second array但我也需要在第一个数组中发生同样的情况,基于第二个数组的排序

["zzz","xxx","yyy","xyz"];

EDIT As i had to fix it urgently, i guess i wasn't able to share more information, and the question seemed vague编辑因为我不得不紧急修复它,我想我无法分享更多信息,而且这个问题似乎很模糊

basically the idea is that i have two sets of arrays, same number of elements in all cases, and they are linked to each other, The second array is a set of order IDs.基本上我的想法是我有两组 arrays,在所有情况下元素数量相同,并且它们相互链接,第二个数组是一组订单 ID。 and first array is set of names, so in the above example, i have第一个数组是一组名称,所以在上面的例子中,我有

4 relates to xyz
2 relates to xxx
3 relates to yyy
1 relates to zzz

So i need to sort based on IDs, and have the same order reflect in the first array as well,所以我需要根据 ID 进行排序,并且在第一个数组中也反映相同的顺序,

so the final result would be,所以最终的结果是,

["zzz","xxx","yyy","xyz"];

which is sorted based on这是根据

[1, 2, 3, 4];

Hopefully this clears things above希望这可以清除上面的内容

Hi please combine two array and sort嗨,请结合两个数组并排序

$newArray =array_combine($valuesInPair,$elementsInPairs);

then sort($newArray);然后sort($newArray);

Use this below code, this does exactly what you're looking for.使用下面的代码,这正是您正在寻找的。

$elementsInPairs = ["xyz","xxx","yyy","zzz"];
$valuesInPair =[4,2,3,1];

$data = array_combine($elementsInPairs,$valuesInPair);
asort($data);

$dumpdata = [];

foreach($data as $x => $x_value) {
    $dumpdata[] = $x;
}

print_r($dumpdata);

I hope this helps you.我希望这可以帮助你。

You kan do like this:你可以这样做:

<?php

$elementsInPairs = ["xyz","xxx","yyy","zzz"];
$valuesInPair =[4,2,3,1];
//use [asort][1] - Sort an array in reverse order and maintain index association
asort($valuesInPair);
// and make a new array to sort elementsInPairs
$newelementsInPairs = array();
foreach($valuesInPair as $key=>$val){
    $newelementsInPairs[] = $elementsInPairs[$key];
}
print_r(implode(",",$valuesInPair)."\n");
print_r(implode(",",$newelementsInPairs));
/** Output

1,2,3,4
zzz,xxx,yyy,xyz

 **/

You can use array_combine() , ksort() and array_values() :您可以使用array_combine() 、 ksort( )array_values()

<?php

$elementsInPairs = ["xyz","xxx","yyy","zzz"];
$valuesInPair = [4,2,3,1];

$newArray = array_combine($valuesInPair, $elementsInPairs);
ksort($newArray);
$sortedElements = array_values($newArray);

print_r($sortedElements);

will output将 output

Array
(
    [0] => zzz
    [1] => xxx
    [2] => yyy
    [3] => xyz
)

You can achieve this by using array_multisort method.您可以通过使用 array_multisort 方法来实现这一点。

array_multisort($valuesInPair, SORT_ASC, $elementsInPair);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM