统一排列/分布数组项目
我有一个带有属性的多维关联数组。它看起来像这样:type
$data = array(
array( "name" => "SomeName", "type" => "A"),
array( "name" => "SomeName", "type" => "A"),
array( "name" => "SomeName", "type" => "A"),
array( "name" => "SomeName", "type" => "A"),
array( "name" => "SomeName", "type" => "A"),
array( "name" => "SomeName", "type" => "B"),
array( "name" => "SomeName", "type" => "B"),
array( "name" => "SomeName", "type" => "B"),
array( "name" => "SomeName", "type" => "C"),
array( "name" => "SomeName", "type" => "C")
);
我想重新排列它,使项目分布更均匀(如果可能的话,重复类型最少)。它应该看起来像这样:
array(
array( "name" => "SomeName", "type" => "A"),
array( "name" => "SomeName", "type" => "B"),
array( "name" => "SomeName", "type" => "A"),
array( "name" => "SomeName", "type" => "C"),
array( "name" => "SomeName", "type" => "A"),
array( "name" => "SomeName", "type" => "B"),
array( "name" => "SomeName", "type" => "A"),
array( "name" => "SomeName", "type" => "C"),
array( "name" => "SomeName", "type" => "A"),
array( "name" => "SomeName", "type" => "B")
);
到目前为止,我尝试的是找到每种类型的计数和总数:
$count_a = 5;
$count_b = 3;
$count_c = 2;
$total = 10;
以及每种类型的比率:
$ratio_a = 0.5; //(5/10)
$ratio_b = 0.3; //(3/10)
$ratio_c = 0.2; //(2/10)
我只是被困在这里。我是否应该尝试使用数字创建新属性,然后根据它进行排序?或者也许以某种方式使用模运算符?我还尝试将项目分成3个不同的数组,如果这样会更容易。index