【发布时间】:2014-04-25 11:00:04
【问题描述】:
有大量的多维数组。
我想通过其中一个值来判断多维数组分组中的最后一个键。
所以,...
TO DO:下图#Q区域。
$i=0;
$j=0;
$limit=10;
$huge_arr = array(
array("point" => 20
"os" => "iOS"),
array("point" => 5
"os" => "iOS"),
array("point" => 10
"os" => "Android"),
array("point" => 20
"os" => "Android"),
array("point" => 7
"os" => "iOS"),
array("point" => 3
"os" => "Android"),
/*... tons of array..*/
);
foreach($huge_arr as $k => $v)
{
if($v['os'] === "iOS")
{
$i++;
if($i % $limit ===0 )
{
//Do something By $limit count :#1
}
//TO DO
//#Q:WANT TO DO same thing when there aren't $v['os'] === "iOS" in rest of $array
}
else if($v['os'] === "iOS")
{
$j++;
if($j % $limit ===0 )
{
//Do something By $limit count :#2
}
//TO DO
//#Q:WANT TO Do same thing when there aren't $v['os'] === "Android" in rest of $array
}
}
使用 foreach() 语句将 $huge_arr 排序为新数组正在增加 php 内存。
所以我不想那样做。 这条路是NG。
foreach($huge_arr as $k => $v)
{
if($v['os'] === "iOS")
{
$ios[] = $v["point"];
}else if($v['os'] === "Android")
{
$ad[] = $v["point"];
}
}
$ios_count = count($ios);
$ad_count = count($ad);
foreach($ios as $k => $v)
{
if($k === $ios_count -1)
//do something for last value
}
foreach($ad as $k => $v)
{
if($k === $ad_count -1)
//do something for last value
}
有没有人知道聪明的方法....
【问题讨论】:
标签: php arrays algorithm sorting foreach