Custom sorting array of objects in PHP

I have the following array:

$arr = [ [ 'user_id' => 1, 'product_id' => 1 ], [ 'user_id' => 1, 'product_id' => 2 ], [ 'user_id' => 1, 'product_id' => 3 ], [ 'user_id' => 2, 'product_id' => 1 ], [ 'user_id' => 2, 'product_id' => 2 ], [ 'user_id' => 3, 'product_id' => 1 ] ]; 

And I want to sort it so that it looks like this:

 $arr = [ [ 'user_id' => 1, 'product_id' => 1 ], [ 'user_id' => 2, 'product_id' => 1 ], [ 'user_id' => 3, 'product_id' => 1 ], [ 'user_id' => 1, 'product_id' => 2 ], [ 'user_id' => 2, 'product_id' => 2 ], [ 'user_id' => 1, 'product_id' => 3 ] ]; 

So basically, I need to order product_id and user_id so that it selects a smaller number of product_id from each user before moving on to the next.

I tried to use usort , but I could not get it to work.

 usort($campaigns, function($a, $b){ if($a['product_id'] == $b['product_id']){ return 0; } if($a['product_id'] < $b['product_id']){ if($a['user_id'] == $b['user_id']){ return 1; } if($a['user_id'] < $a['user_id']){ return 0; } return -1; }else{ if($a['user_id'] == $a['user_id']){ return -1; } if($a['user_id'] < $a['user_id']){ return 0; } return 1; } }); 

I also tried array_multisort , but all I could do was order using the same order that I was already extracting from the database.

+7
sorting php
source share
3 answers

Assuming your values ​​are integers:

 usort($campaigns, function($a, $b){ if($a['product_id'] == $b['product_id']){ return $a['user_id'] - $b['user_id']; } else { return $a['product_id'] - $b['product_id']; } }); 

You can also use database ordering with the ORDER BY product_id, user_id .

+3
source share

Solution using the array_multisort function with an "array of columns" (several sorting options):

 $userIds = $productIds = []; foreach ($arr as $k => $v) { $userIds[$k] = $v['user_id']; $productIds[$k] = $v['product_id']; } array_multisort($productIds, SORT_ASC, $userIds, SORT_ASC, $arr); print_r($arr); 

Exit:

 Array ( [0] => Array ( [user_id] => 1 [product_id] => 1 ) [1] => Array ( [user_id] => 2 [product_id] => 1 ) [2] => Array ( [user_id] => 3 [product_id] => 1 ) [3] => Array ( [user_id] => 1 [product_id] => 2 ) [4] => Array ( [user_id] => 2 [product_id] => 2 ) [5] => Array ( [user_id] => 1 [product_id] => 3 ) ) 
+2
source share
 $arrTags = [ [ 'user_id' => 1, 'product_id' => 1 ], [ 'user_id' => 1, 'product_id' => 2 ], [ 'user_id' => 1, 'product_id' => 3 ], [ 'user_id' => 2, 'product_id' => 1 ], [ 'user_id' => 2, 'product_id' => 2 ], [ 'user_id' => 3, 'product_id' => 1 ] ]; foreach($arrTags as $key => $row){ $userArray[$key] = $row['user_id']; $productArray[$key] = $row['product_id']; } array_multisort($productArray, SORT_ASC, $userArray, SORT_ASC, $arrTags); print_r($arrTags); 

Exit

  Array ( [0] => Array ( [user_id] => 1 [product_id] => 1 ) [1] => Array ( [user_id] => 2 [product_id] => 1 ) [2] => Array ( [user_id] => 3 [product_id] => 1 ) [3] => Array ( [user_id] => 1 [product_id] => 2 ) [4] => Array ( [user_id] => 2 [product_id] => 2 ) [5] => Array ( [user_id] => 1 [product_id] => 3 ) ) 

You can also check out the online editor. Click here

+1
source share

All Articles