0

I want to rearrange a array by these key values. let me explain. Please check my array values

Array
    (
        [0] => stdClass Object
            (

                [user_id] => 103
                [recipe_id] => 90

            )

        [1] => stdClass Object
            (

                [user_id] => 103
                [recipe_id] => 90

            )

        [2] => stdClass Object
            (

                [user_id] => 103
                [recipe_id] => 64

            )

        [3] => stdClass Object
            (

                [user_id] => 103
                [recipe_id] => 90

            )
        [4] => stdClass Object
            (

                [user_id] => 103
                [recipe_id] => 64

            )

    )

This is my array result and I want it rearrange by key recipe_id. which recipe_id is same they will come together. like that

Array
    (
        [0] => stdClass Object
            (

                [user_id] => 103
                [recipe_id] => 90

            )

        [1] => stdClass Object
            (

                [user_id] => 103
                [recipe_id] => 90

            )

        [2] => stdClass Object
            (

                [user_id] => 103
                [recipe_id] => 90

            )

        [3] => stdClass Object
            (

                [user_id] => 103
                [recipe_id] => 64

            )
        [4] => stdClass Object
            (

                [user_id] => 103
                [recipe_id] => 64

            )

    )

How can i arrange this array values??. I want to show array result like I show above example. is there any way to rearrange my array with same key value.

Thanks in advance

  • 2
    Possible duplicate of [Sort array of objects by object fields](https://stackoverflow.com/questions/4282413/sort-array-of-objects-by-object-fields) – Saad Suri Nov 16 '18 at 07:55
  • You can achieve that result using PHP usort function :: http://php.net/manual/en/function.usort.php – JenuJ Nov 16 '18 at 07:58

3 Answers3

3

try this

$sorted = collect($array)
    ->sortByDesc('recipe_id')
    ->values()
    ->all();

if you want to divide them by recipe_id you can use

$grouped = collect($array)
    ->groupBy('recipe_id')
    ->toArray();
simonecosci
  • 1,194
  • 7
  • 13
  • HI,I have one question can I make same recipe_id value in array like all 90 values will show in array inside this array and all 64 also – Indresh Tayal Nov 16 '18 at 08:27
0

You can use php usort function as follow.

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

$result = usort($arr,$my_sort);
JenuJ
  • 446
  • 5
  • 10
0
  1. First convert object to array form

    json_decode(json_encode(json_decode($arr_value_list)), true);
    // $arr_value_list is  values array
    
  2. Then use following function to sort values

    $key_name = 'user_id';

    usort($arr_value_list, function ($a, $b) use(&$key_name)
            {
                return $a[$name] - $b[$name];
            });
    

Get Output by print_r($arr_value_list);

amba patel
  • 384
  • 1
  • 5
  • 16