嗨,伙计们,我正在尝试计算地区的密度和时间短,任何线索我的方法有什么问题? 我使用区域中的最高人口来着色结果,但它在前三个数组$ key =&gt; $值中给出了空值,最初我使用了数据库中的结果总数,但结果完全错误。< / p>
$totalDistricts =count($districts);
$totalPop=0;
$maxArr=array();
// get max value in the districts data['Total_'] field
foreach ($districts as &$key)
{
$maxArr[]=(int)$key['Total_'];
// var_dump($key['Total_']);
}
$totalPop=max($maxArr);
// print($totalPop.' '. $totalDistricts); exit();
// split the totalPop variable into percentage based ranges
$ranges =array
(
array((int)floor($totalPop*0.8),$totalPop-1),
array((int)floor($totalPop*0.5),(int)floor($totalPop*0.8)-1),
array((int)floor($totalPop*0.2),(int)floor($totalPop*0.5)-1),
array(0,floor($totalPop*0.2)-1),
);
// var_dump($ranges); exit();
$rankings =array(array(),array(),array(),array());
$sorted =array();
for ($i=0; $i<$totalDistricts; $i++)
{
$sorted[$i]=$districts[$i]['Total_'];
}
sort($sorted);
// a rankings array to group the data based on
// the total_ column if the total falls within the range,
// put them into the matching key=>val range
//I have figured part of the problem out, it lies in the foreach loop, the $districts[$key]<- part is wrong
foreach ($sorted as $key=>$val)
{
if ($key>=$ranges[0][0] && $key<=$ranges[0][1])
{
$rankings[0][]=$districts[$key];
}
if ($key>=$ranges[1][0] && $key<=$ranges[1][1])
{
$rankings[1][]=$districts[$key];
}
if ($key>=$ranges[2][0] && $key<=$ranges[2][1])
{
$rankings[2][]=$districts[$key];
}
if ($key>=$ranges[3][0] && $key<=$ranges[3][1])
{
$rankings[3][]=$districts[$key];
}
}
这给了我排名数组中的前三个值为空数组,任何线索我做错了什么?