I have the 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 to sort this array in a way that the sum of any number of back to back "grids" is equals to 12.
Example: The values of the "grids" in the above array are : 6,3,6,3,3,12,3
(6+6=12), (3+3+3+3=12),(12=12) so the new order should be 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 the array the new array should look like 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 and found these functions: sort,uasort, uksort, usort but I couldn't figure out how to use them.
Could you please tell me how to achieve this using PHP ?
Update
The value of grid will always be 3 or 6 or 12 (these three numbers only )
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"),
);