-1

I am having a php array as follows;

$array =Array(
[310] => Array
    (
        [vendorname] => Utsav the vendor
    )
[309] => Array
    (
        [vendorname] => Ashish vendor
        [suggest_order] => 1
    )
[308] => Array
    (
        [vendorname] => praveen rathod vendor
    )
[262] => Array
    (
        [vendorname] => Yash Vendor
        [suggest_order] => 0
    )
[264] => Array
    (
        [vendorname] => amol vendro
        [suggest_order] => 2
    ));

And I want to sort it based on suggest_order key so lowest suggest_order key's value should come first and than higher value and in last their comes all remaining elements which don't even have suggest_order key like;

$array =Array(
[262] => Array
    (
        [vendorname] => Yash Vendor
        [suggest_order] => 0
    )
[309] => Array
    (
        [vendorname] => Ashish vendor
        [suggest_order] => 1
    )
[264] => Array
    (
        [vendorname] => amol vendro
        [suggest_order] => 2
    )
[310] => Array
    (
        [vendorname] => Utsav the vendor
    )

[308] => Array
    (
        [vendorname] => praveen rathod vendor
    ));

I have tried PHP Sort Array By SubArray Value .

function cmp_by_optionNumber($a, $b) {
  return $a["suggest_order"] - $b["suggest_order"];
}
print_r(usort($array, "cmp_by_optionNumber"));

And I have also tried 2nd option in above answer,

$new_array=usort($array, function ($a, $b) {
    return $a['suggest_order'] - $b['suggest_order'];
});
print_r($new_array);

But I am getting "1" in response; Any help will be appreciated.

Ahmed Syed
  • 1,179
  • 2
  • 18
  • 44
  • You can either use null coalesce (??) or check if each side has ['suggest_order'] set and decide how this will affect the test (i.e. what dummy value you want it to have to move it into the right place) – Nigel Ren Apr 18 '18 at 11:02
  • Possible duplicate of [php sort($array) returning 1 instead of sorted array](https://stackoverflow.com/questions/28957927/php-sortarray-returning-1-instead-of-sorted-array) – Philipp Maurer Apr 18 '18 at 11:02
  • from docs `Returns TRUE on success or FALSE on failure` and if you see it takes the array as reference `bool usort ( array &$array , callable $value_compare_func )` that means your given array will be already sorted. – Edwin Apr 18 '18 at 11:04
  • Possible duplicate of [Sort Multi-dimensional Array by Value](https://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value) – Muhammad Abdullah Apr 18 '18 at 11:07
  • @AbdullahMalik He linked a multi dimensional sort question in his question and uses the given answer in his code. The problem he describes has nothing to do with multi dimensional sorting. – Philipp Maurer Apr 18 '18 at 11:10
  • Possible duplicate of [sorting array based on inner-array key-value](https://stackoverflow.com/questions/6530631/sorting-array-based-on-inner-array-key-value) – Nishit Manjarawala Apr 18 '18 at 11:34

2 Answers2

2

All sort methods take the array to sort as reference. So you don't have to care about the return value, as the sorting is done in place.

function cmp_by_optionNumber($a, $b) {
  return $a["suggest_order"] - $b["suggest_order"];
}
usort($array, "cmp_by_optionNumber");
print_r($array);

If you need to do some special handling for suggest_order, you could use isset inside the sort function.

usort($array, function($a, $b) {
    if (isset($a["suggest_order"]) && isset($b["suggest_order"])) {
        return $a["suggest_order"] - $b["suggest_order"];
    }
    if (isset($a["suggest_order"])) {
        return -1;
    }
    if (isset($b["suggest_order"])) {
        return 1;
    }
    return 0;
});
Philipp
  • 15,377
  • 4
  • 35
  • 52
  • 2
    What about the problem when an element doesn't have a `"suggest_order"`? – KIKO Software Apr 18 '18 at 11:02
  • Thanx @Phillipp but I am getting 0,blank,blank,1,2 this kind of sorted array. – Ahmed Syed Apr 18 '18 at 11:05
  • @MujahedAKAS which code did you tried? yours or my code with the suggestion_order handling? – Philipp Apr 18 '18 at 11:06
  • this is wrong, you cannot substract suggest orders as one may be 10 and other 1, you need to order by suggest_order, check my answer, it won't sort array at all – bestestefan Apr 18 '18 at 11:07
  • @stetoc of couse you can - the result don't have to be exactly -1, 0 or 1 - it just have to be positive, negative or zero, which is the case, if you just subtract the values – Philipp Apr 18 '18 at 11:09
  • after thinking for a while it may work, I missthinked it, ik that it has to be > 0 or < 0 or 0 to sort, but I had few cases in my head where it would not order items properly, maybe just my mistake – bestestefan Apr 18 '18 at 11:12
  • If OP is getting the results he says, perhaps swap the -1 and 1 from the dummy return values. This may push the empty ones to the end. – Nigel Ren Apr 18 '18 at 11:13
-1

to compare elements which have 'suggest_order' property with those which doesn't have you can use function like:

function cmp_by_optionNumber($a, $b) {
   $lval = $a["suggest_order"]??-1;
   $rval = $b["suggest_order"]??-1;
    return $lval > $rval ? 1 : $rval > $lval ? -1 : 0;
}

this will properly order items as you want - by suggest_order key

also you do not want to print result of sort function but original array passed to sort function to see if it's sorted

bestestefan
  • 852
  • 6
  • 20