2

I want to sort array in descending order using php.

like 'XXS','XS','S','M','L','XL','2X','3X','4X','5X','6','7','8','9','10','11'

$attributesData['attributes'][$key]['options'][$search] this will return following array value and its store to $data['attributes'][$key] this array before its store I want to sort by label in $data['attributes'][$key] array.

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

)
Array
(
    [id] => 72
    [label] => L
)
Array
(
    [id] => 73
    [label] => M
)

I tried many way but not getting proper solution.

3
  • shouldn't it be ... '5X','4X','3X','2X', ... ? if we talk about descending order Commented Jan 4, 2017 at 7:34
  • yes you are rigth but I have numbers of array like this Commented Jan 4, 2017 at 7:36
  • It was a good question. You have your answer; Now start implementing instead of requesting the red carpet. Commented Jan 4, 2017 at 8:23

4 Answers 4

3

You can do it using usort:

$sizes = ['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 ($sizes) {
    if ($a['label'] == $b['label']) {
        return 0;
    }

    return $sizes[$a['label']] < $sizes[$b['label']] ? -1 : 1;
});

var_dump($myArray);
Sign up to request clarification or add additional context in comments.

1 Comment

Missed usort. :)
1

You can try this. It should sort in any order define in the array.

// 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);

Output

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"
  }
}

Demo

Comments

1

use the usort to sort them.

$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']];});

Comments

0

Think this may work

    <?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)); ?>

DEMO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.