根据另一个数组的键值对数组进行排序。

时间:2022-04-19 22:06:59

I have one array:

我有一个数组:

 $array_sorter = [  
                    'XXS' => 1,   
                    'XS' => 2,  
                    'S' => 3,   
                    'M' => 4,   
                    'L' => 5,   
                    'XL' => 6,   
                    'XXL' => 7  
                 ];

and another one that could be like this:

另一个可能是这样的:

 $array_to_sort = ('XS','M','XL','S','L','XXS')

how can I do to sort the second one based upon the first one?

我要怎么做才能把第二个基于第一个呢?

I mean:

我的意思是:

$array_to_sort are sizes but they are random inside this array

$array_to_sort是大小,但是它们在这个数组中是随机的。

I need to print the list of available sizes ($array_to_sort) but in the order of $array_sorter

我需要打印可用大小的列表($array_to_sort),但要按$array_sorter的顺序打印。

3 个解决方案

#1


0  

As stated by @Jakumi, usort would be the way to go. However this way might be easier to understand if you are a beginner :

正如@Jakumi所言,usort将是前进的道路。然而,如果你是一个初学者,这种方法可能更容易理解:

$array_sorter = [
    'XXS' => 1,
    'XS' => 2,
    'S' => 3,
    'M' => 4,
    'L' => 5,
    'XL' => 6,
    'XXL' => 7,
];

$array_to_sort = array('XS', 'M', 'XL', 'S', 'L', 'XXS');

$array_sorted = array();

foreach($array_to_sort as $item){
    $k = $array_sorter[$item];
    $array_sorted[$k] = $item;
}
ksort($array_sorted);

#2


0  

Simply use the first array as a "weight"-provider. I mean the first array have a weight for every entry of your second array (e.g. M get the weight 4).

简单地使用第一个数组作为“权重”提供程序。我的意思是,第一个数组对于第二个数组的每个条目都有一个权重(例如,M的权重为4)。

loop over the second array and create a thrid one: $weightArray like this:

对第二个数组进行循环并创建一个thrid: $加权像这样:

$weightArray = array();
foreach($array_to_sort as $value){
  $weight = $array_sorter[$value];
  if(!isset($weightArray[$weight])){
    $weightArray[$weight] = array();
  }
  $weightArray[$weight][] = $value;
}

Now you have an array like this: (2=>'XS', 4=>'M', 5=>'XL', 3=>'S', 5=>'L', 1=>'XXS')

现在你有这样一个数组:(2 = > ' x ',4 = > ' M ',5 = > XL,3 = > ' S ',5 = > ' L ',1 = > ' XXS ')

now you can sort it by key ksort() and copy it back to your source array like this:

现在您可以通过密钥ksort()对它进行排序,并将其复制到您的源数组中:

$array_to_sort = array_values(ksort($weightArray));

PS: if you have something like $array_to_sort = ('XS','M','M','M','L','XXS') it will also work => $array_to_sort = ('XXS','XS','M','M','M','L')

PS:如果你有像$ array_to_sort =(‘x’,‘米’,‘米’,‘米’,‘L’,‘XXS’)它还将工作= > $ array_to_sort =(‘XXS’,‘x’,‘米’,‘米’,‘米’,‘L’)

#3


0  

Thanks to all for you suggestion

谢谢你的建议。

I found alone this solution:

我独自找到了这个解决方案:

my mistake was focusing on the array to be sorted ($arrat_to_sort)

我的错误是将焦点集中在要排序的数组上($arrat_to_sort)

instead I turned the problem:

相反,我转向了这个问题:

as $array_sorter always contains all possible values of $arrat_to_sort in the right order, I used the function array_intersect($ array_sorter, $ arrat_to_sort)

$array_sorter总是包含$arrat_to_sort的所有可能值,我使用了函数array_intersect($ array_sorter, $arrat_to_sort)

and immediately I have an array with the values of $ arrat_to_sort in the $array_sorter position

我马上就有了一个数组,它的值是$ arrat_to_sort在$array_sorter的位置。

#1


0  

As stated by @Jakumi, usort would be the way to go. However this way might be easier to understand if you are a beginner :

正如@Jakumi所言,usort将是前进的道路。然而,如果你是一个初学者,这种方法可能更容易理解:

$array_sorter = [
    'XXS' => 1,
    'XS' => 2,
    'S' => 3,
    'M' => 4,
    'L' => 5,
    'XL' => 6,
    'XXL' => 7,
];

$array_to_sort = array('XS', 'M', 'XL', 'S', 'L', 'XXS');

$array_sorted = array();

foreach($array_to_sort as $item){
    $k = $array_sorter[$item];
    $array_sorted[$k] = $item;
}
ksort($array_sorted);

#2


0  

Simply use the first array as a "weight"-provider. I mean the first array have a weight for every entry of your second array (e.g. M get the weight 4).

简单地使用第一个数组作为“权重”提供程序。我的意思是,第一个数组对于第二个数组的每个条目都有一个权重(例如,M的权重为4)。

loop over the second array and create a thrid one: $weightArray like this:

对第二个数组进行循环并创建一个thrid: $加权像这样:

$weightArray = array();
foreach($array_to_sort as $value){
  $weight = $array_sorter[$value];
  if(!isset($weightArray[$weight])){
    $weightArray[$weight] = array();
  }
  $weightArray[$weight][] = $value;
}

Now you have an array like this: (2=>'XS', 4=>'M', 5=>'XL', 3=>'S', 5=>'L', 1=>'XXS')

现在你有这样一个数组:(2 = > ' x ',4 = > ' M ',5 = > XL,3 = > ' S ',5 = > ' L ',1 = > ' XXS ')

now you can sort it by key ksort() and copy it back to your source array like this:

现在您可以通过密钥ksort()对它进行排序,并将其复制到您的源数组中:

$array_to_sort = array_values(ksort($weightArray));

PS: if you have something like $array_to_sort = ('XS','M','M','M','L','XXS') it will also work => $array_to_sort = ('XXS','XS','M','M','M','L')

PS:如果你有像$ array_to_sort =(‘x’,‘米’,‘米’,‘米’,‘L’,‘XXS’)它还将工作= > $ array_to_sort =(‘XXS’,‘x’,‘米’,‘米’,‘米’,‘L’)

#3


0  

Thanks to all for you suggestion

谢谢你的建议。

I found alone this solution:

我独自找到了这个解决方案:

my mistake was focusing on the array to be sorted ($arrat_to_sort)

我的错误是将焦点集中在要排序的数组上($arrat_to_sort)

instead I turned the problem:

相反,我转向了这个问题:

as $array_sorter always contains all possible values of $arrat_to_sort in the right order, I used the function array_intersect($ array_sorter, $ arrat_to_sort)

$array_sorter总是包含$arrat_to_sort的所有可能值,我使用了函数array_intersect($ array_sorter, $arrat_to_sort)

and immediately I have an array with the values of $ arrat_to_sort in the $array_sorter position

我马上就有了一个数组,它的值是$ arrat_to_sort在$array_sorter的位置。