【问题标题】:How to judge last key in multi dimentional array?如何判断多维数组中的最后一个键?
【发布时间】: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


    【解决方案1】:

    这不需要创建新数组,而只需从数组中获取所需的内容 - 假设您只需要最后一个 IOS 和 android 密钥。

    $ios_key = $ad_key = NULL;  // If one of them is not present key will be NULL
    
    // Reverse the array to put the last key at the top
    array_reverse($huge_arr);
    
    foreach($huge_arr as $k => $v)
    {
        // Fill key with ios key value if not already set
        if($v['os'] === "iOS" AND $ios_key === NULL) 
        {
            $ios_key = $k;
        }
        // Fill key with first android key value if not already set
        else if($v['os'] === "Android" AND $ad_key === NULL)
        {
            $ad_key = $k;
        }
    
        // Stop looping when we found both keys
        if($ios_key !== NULL AND $ad_key !== NULL)
            break;
     }
    
     // Put the array back in original order
     array_reverse($huge_array);
    
     // Do whatever you want with the array now
    

    关于你问题的另一部分do the same thing when there aren't IOS ....

    if($i % $limit ===0 )
    

    进入

    if($i % $limit === 0 OR $k === $ios_key) // $ios_key should be the last key
    

    【讨论】:

    • 谢谢。但是我想在判断最后一个键作为每个限制计数之后在同一个地方编写相同的代码。
    • 我不太明白你的意思。你能说得更具体点吗?
    • 非常感谢!是的,我只需要最后一个“IOS”键和“Android”键。你的建议和代码对我来说很简单。感谢您写好评论。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-16
    • 2012-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多