Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php多维字符串排序_Php_Sorting - Fatal编程技术网

php多维字符串排序

php多维字符串排序,php,sorting,Php,Sorting,我想使用php按降序对数组排序 像“XXS”、“XS”、“S”、“M”、“L”、“XL”、“2X”、“3X”、“4X”、“5X”、“6”、“7”、“8”、“9”、“10”、“11” $attributesData['attributes'][$key]['options'][$search]这会将以下数组值及其存储返回到$data['attributes'][$key]此数组的存储之前,我想在$data['attributes'][$key]数组中按标签进行排序 Array ( [id]

我想使用php按降序对数组排序

“XXS”、“XS”、“S”、“M”、“L”、“XL”、“2X”、“3X”、“4X”、“5X”、“6”、“7”、“8”、“9”、“10”、“11”

$attributesData['attributes'][$key]['options'][$search]
这会将以下数组值及其存储返回到
$data['attributes'][$key]
此数组的存储之前,我想在
$data['attributes'][$key]
数组中按
标签进行排序

Array
(
    [id] => 68
    [label] => 2X
)
Array
(
    [id] => 69
    [label] => 3X

)
Array
(
    [id] => 72
    [label] => L
)
Array
(
    [id] => 73
    [label] => M
)
我尝试了很多方法,但都没有找到合适的解决方案。

您可以使用以下方法:

$size=['XXS'=>16,'XS'=>15,'S'=>14,'M'=>13,'L'=>12,'XL'=>11,'2X'=>10,'3X'=>9,'4X'=>8,'5X'=>7,'6'=>6,'7'=>5,'8'=>4,'9'=>3,'10'=>2,'11'=>1];
usort($myArray,function($a,$b)use($size){
如果($a['label']=$b['label'])){
返回0;
}
返回$size[$a['label']]<$size[$b['label']]?-1:1;
});
变量转储($myArray);

您可以试试这个。它应该
数组中定义的任何顺序进行排序

// Array of labels in order
$order = ['XXS','XS','S','M','L','XL','2X','3X','4X','5X','6','7','8','9','10','11'];
// values
$array =[
[
    'id' => 68,
    'label' => '2X',
],
[
    'id' => 69,
    'label' => '3X',

],
[
    'id' => 72,
    'label' => 'L',
],
[
    'id' => 73,
    'label' => 'M',
]];

$new = [];
foreach($order as $o) {
   // Get the index of the label in values
   $pos = array_search($o, array_column($array, 'label'));
   // If found store in new array
   if($pos !== false) {
      $new[] = $array[$pos];
   }
}

var_dump($new);
输出

array(4) {
  [0]=>
  array(2) {
    ["id"]=>
    int(73)
    ["label"]=>
    string(1) "M"
  }
  [1]=>
  array(2) {
    ["id"]=>
    int(72)
    ["label"]=>
    string(1) "L"
  }
  [2]=>
  array(2) {
    ["id"]=>
    int(68)
    ["label"]=>
    string(2) "2X"
  }
  [3]=>
  array(2) {
    ["id"]=>
    int(69)
    ["label"]=>
    string(2) "3X"
  }
}

使用usort对它们进行排序

$size = ['XXS','XS','S','M','L','XL','2X','3X','4X','5X','6','7','8','9','10','11'];
$weight = array_flip($size);
$result = usort($sortSize, function($a, $b) use($weight){return $weight[$b['label']] > $weights[$a['label']];});
我认为这可能有效

    <?php

$a = Array(
    1 => Array(
         'id' => 68,
         'label' => '2X'
    ),
    0 => Array(
         'id' => 69,
         'label' => '3X'
    ),
    2 => Array(
         'id' => 70,
         'label' => 'L'
    ),
     3 => Array(
         'id' => 71,
         'label' => 'XXS'
    ),
    4 => Array(
         'id' => 72,
         'label' => '11'
    ),
    5 => Array(
         'id' => 72,
         'label' => 'M'
    ),
);
function compareByName($a, $b) {
  return strcmp($a["label"], $b["label"]);
}
usort($a, 'compareByName');
print_r(array_reverse($a)); ?>


不应该是
…'5X’、‘4X’、‘3X’、‘2X’、…
?如果我们讨论降序是的,你是正确的,但我有这样的数组数,这是一个好问题。你有你的答案;现在开始实施,而不是要求红地毯。错过
usort
:)
    <?php

$a = Array(
    1 => Array(
         'id' => 68,
         'label' => '2X'
    ),
    0 => Array(
         'id' => 69,
         'label' => '3X'
    ),
    2 => Array(
         'id' => 70,
         'label' => 'L'
    ),
     3 => Array(
         'id' => 71,
         'label' => 'XXS'
    ),
    4 => Array(
         'id' => 72,
         'label' => '11'
    ),
    5 => Array(
         'id' => 72,
         'label' => 'M'
    ),
);
function compareByName($a, $b) {
  return strcmp($a["label"], $b["label"]);
}
usort($a, 'compareByName');
print_r(array_reverse($a)); ?>