php - Sorting an array based on a condition -


i have following array

$records = array(      array("postid"=>"1","grid"=>"6"),     array("postid"=>"2","grid"=>"3"),     array("postid"=>"3","grid"=>"6"),     array("postid"=>"4","grid"=>"3"),     array("postid"=>"5","grid"=>"3"),     array("postid"=>"6","grid"=>"12"),     array("postid"=>"7","grid"=>"3"),  ); 

i want sort array in way sum of number of back "grids" equals 12.

example: values of "grids" in above array : 6,3,6,3,3,12,3

(6+6=12), (3+3+3+3=12),(12=12) new order should 6,6,3,3,3,3,12 or 3,3,3,3,12,6,6 or 6,3,3,6,3,3,12

so after sorting array new array should following:

$records=array(      array("postid"=>"1","grid"=>"6"),     array("postid"=>"3","grid"=>"6"),     array("postid"=>"2","grid"=>"3"),            array("postid"=>"4","grid"=>"3"),     array("postid"=>"5","grid"=>"3"),     array("postid"=>"7","grid"=>"3"),     array("postid"=>"6","grid"=>"12"),  ); 

i searched in php manual , found these functions: sort,uasort, uksort, usort couldn't figure out how use them.

could please tell me how achieve using php ?

update

the value of grid 3 or 6 or 12 (these 3 numbers )

problem

  $records = array(      array("postid"=>"1","grid"=>"3"),     array("postid"=>"2","grid"=>"6"),         array("postid"=>"3","grid"=>"3"),          array("postid"=>"4","grid"=>"3"),     array("postid"=>"5","grid"=>"6"),     array("postid"=>"6","grid"=>"6"),         array("postid"=>"7","grid"=>"3"),     array("postid"=>"8","grid"=>"6"),   ); 

so not sorting, reordering create sequence. imagine trying layout of bricks fixed height, , need have reordered fill each row , leave rest @ end. given fixed variants of 12,6,3 can done sorting in descending order - odd number of sixes filled smaller threes. such order produce boring layout - have more interesting need reorder posts. need create temporary container , merge when sum of grids equal 12. if left temporary containers, merge them 1 , sort descending before merging grouped.

code illustrating concept:

//auxiliary function calculate sum of grids in given temporary container     function reduc($a) {     return array_reduce($a, function ($result, $item) {         return $result . $item['grid'] . ',';     }, ''); }  function regroup($records, $group_sum = 12) {     $temp = array();     $grouped = array();      foreach ($records $r) {         if ($r['grid'] == $group_sum) {             $grouped[] = $r;         } else {             if (!$temp) {                 $temp[] = array($r);             } else {                 $was_grouped = false;                 foreach ($temp $idx => $container) {                     $current_sum = sum_collection($container);                     if ($current_sum + $r['grid'] <= $group_sum) {                         $temp[$idx][] = $r;                         if ($current_sum + $r['grid'] == $group_sum) {                             $grouped = array_merge($grouped, $temp[$idx]);                             unset($temp[$idx]);                         }                         $was_grouped = true;                         break;                     }                 }                 if (!$was_grouped) {                     $temp[] = array($r);                 }             }         }     }      if ($temp) {         //sort descending, biggest ones filled first smalller         $rest = call_user_func_array('array_merge', $temp);         usort($rest, function($a, $b) {             return $b['grid'] - $a['grid'];         });         $grouped = array_merge($grouped, $rest);     }      return $grouped; } 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -