我有以下数组
$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"),
);
我想以任意数量的背靠背“网格”之和等于 12 的方式对该数组进行排序。
示例:上述数组中“grids”的值为:6,3,6,3,3,12,3
(6+6=12), (3+3+3+3=12),(12=12) 所以新的顺序应该是6,6,3,3,3,3,12
or 3,3,3,3,12,6,6
or6,3,3,6,3,3,12
因此,在对数组进行排序后,新数组应如下所示:
$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"),
);
我在 php 手册中搜索并找到了这些函数:sort、uasort、uksort、usort,但我不知道如何使用它们。
你能告诉我如何使用 PHP 来实现这一点吗?
更新
grid 的值将始终为 3 或 6 或 12(仅限这三个数字)
问题
$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"),
);