0

So I have this result from database:

object(Illuminate\Support\Collection)#538 (1) {
  ["items":protected]=>
    array(3) {
        [0]=>
        object(stdClass)#536 (3) {
           ["col_header"]=>
           string(7) "other_1"
           ["col_order"]=>
           int(12)
           ["data"]=>
           string(13) "asdgasdgfasdg"
        }
        [1]=>
        object(stdClass)#545 (3) {
           ["col_header"]=>
           string(7) "other_2"
           ["col_order"]=>
           int(10)
           ["data"]=>
           string(10) "dfhgsdhgsd"
       }
       [2]=>
       object(stdClass)#533 (3) {
           ["col_header"]=>
           string(7) "other_3"
           ["col_order"]=>
           int(11)
           ["data"]=>
           string(1) "s"
       }
}

Now, how can I sort its result based on value of col_order?The result should be like:

object(Illuminate\Support\Collection)#538 (1) {
  ["items":protected]=>
    array(3) {
        [0]=>
        object(stdClass)#545 (3) {
           ["col_header"]=>
           string(7) "other_2"
           ["col_order"]=>
           int(10)
           ["data"]=>
           string(10) "dfhgsdhgsd"
        }
        [1]=>
        object(stdClass)#533 (3) {
           ["col_header"]=>
           string(7) "other_3"
           ["col_order"]=>
           int(11)
           ["data"]=>
           string(1) "s"
        }
        [2]=>
        object(stdClass)#536 (3) {
           ["col_header"]=>
           string(7) "other_1"
           ["col_order"]=>
           int(12)
           ["data"]=>
           string(13) "asdgasdgfasdg"
        }
}

I've tried using asort() here but it seems it only supports associative array. Is there any way I can sort this one out?

3
  • 5
    You want php.net/manual/en/function.usort.php Commented Dec 17, 2018 at 10:56
  • 2
    Why you are not using orderBy on your query? As I thought you were using laravel elquent so you can use orderBy(''col_order) on your query Commented Dec 17, 2018 at 11:04
  • @MD.JubairMizan Thanks for that one. I haven't thought of it! It solves my problem, will use the answers on my future scenario, Commented Dec 17, 2018 at 11:11

2 Answers 2

5

You can either use usort()

$test = array(array("col_header" => "other_1",
                    "col_order" => 12,
                    "data" => "asdgasdgfasdg"),
              array("col_header" => "other_2",
                    "col_order" => 10,
                    "data" => "dfhgsdhgsd"),
              array("col_header" => "other_3",
                    "col_order" => 11,
                    "data" => "s"));

usort($test, function($a, $b)
             {
                 if ($a["col_order"] == $b["col_order"])
                     return (0);
                 return (($a["col_order"] < $b["col_order"]) ? -1 : 1);
             });

var_dump($test);

Output :

array (size=3)
  0 => 
    array (size=3)
      'col_header' => string 'other_2' (length=7)
      'col_order' => int 10
      'data' => string 'dfhgsdhgsd' (length=10)
  1 => 
    array (size=3)
      'col_header' => string 'other_3' (length=7)
      'col_order' => int 11
      'data' => string 's' (length=1)
  2 => 
    array (size=3)
      'col_header' => string 'other_1' (length=7)
      'col_order' => int 12
      'data' => string 'asdgasdgfasdg' (length=13)

But I suggest you to sort your results directly from the SQL query :

SELECT *
FROM yourTable
...
ORDER BY col_order ASC
Sign up to request clarification or add additional context in comments.

Comments

3

The answer is http://php.net/manual/en/function.usort.php - it sorts by a user-defined comparison function. Replace a with col_order in the following snippet:

<?php

$class1 = new stdClass();
$class1->a = 1;
$class2 = new stdClass();
$class2->a = 2;
$class3 = new stdClass();
$class3->a = 3;

$input = [$class3,$class2,$class1];

var_dump($input);

usort($input, function($a, $b) {
      if ($a->a == $b->a) {
        return 0;
    }
    return ($a->a < $b->a) ? -1 : 1;
});

var_dump($input);

https://3v4l.org/9ELKp

As you are using Illuminate you might instead want to look at https://laravel.com/api/5.5/Illuminate/Database/Query/Builder.html#method_orderBy too and use that instead.

Edit from the comments: using PHP 7+ you can make use of the spaceship operator to simplify the comparison function:

usort($input, function($a, $b) {
    return $a->a <=> $b->a;
});

2 Comments

The body of the function could be a oneliner with spaceship operator in php7: return $a->a <=> $b->a;
@FilipHalaxa Thank you, I will add it as addition to my answer.

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.