duanaixuan7385 2011-12-27 06:39
浏览 38
已采纳

使用php从数组中检索最后一个值

I have an array name arrayname and have some values in below image.

enter image description here

How can I Get the last value in array arrayname even if the value at arrayname1[7] is empty. This should ignore empty values and gives the end answer.

  • 写回答

5条回答 默认 最新

  • donglao7947 2011-12-27 06:43
    关注

    Filter out the empty values, take the last value:

    $array = array_filter($arrayname1);
    $lastValue = end($array);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • doudong4532 2011-12-27 06:41
    关注

    There is no-way to find the empty values that you needed, but there's an another way to create another array and find the end value.

    Below is a sample example

    <?php
        $fruits = array('apple', 'banana', 'cranberry','', 'fadsf', '', '');
        echo "Count : " . count($fruits);
    
        $k = count($fruits) - 1;
        foreach($fruits as $pos => $v1) {
            if($v1 != "") {
                echo "$v1 <br>";
                $newarray1[] = $v1;
            }
        }
        echo "<br>Last : " . end($newarray1);
    ?>
    
    评论
  • douji2520 2011-12-27 06:43
    关注

    Use "isset()" to see if an element is "empty".

    Use "count()" to determine the length of the array.

    评论
  • doukuizuo1795 2011-12-27 06:56
    关注

    BENCHMARK:

    I redid the benchmark on my own server, as CodePad was giving sporadic results from it's sporadic server load, and it uses an old version of PHP.

    Results:

    (Target: 100003)
    Last Value: 100003
    array_filter took 8.4615960121155 seconds
    Last Value: 100003
    array_flip took 20.312706947327 seconds
    Last Value: 100003
    array_pop took 6.7329788208008 seconds
    

    The benchmark script is:

    $array=array();
    for($run=0; $run<100000; $run++)
    {
    $rand=rand(0,4);
    if ($rand===0) $array[]=''; else $array[]=(string)$rand+$run;
    }
    $save=$array;
    
    echo '(Target: '.end($array).")
    ";
    $time=microtime(true);
    
    for($run=0; $run<1000; $run++)
    {
    $array=$save;
    $array = array_filter($array);
    $lastValue = end($array);
    }
    
    $time=microtime(true)-$time;
    echo "Last Value: $lastValue
    ";
    echo "array_filter took $time seconds
    ";
    unset($array_2);
    $time=microtime(true);
    
    for($run=0; $run<1000; $run++)
    {
    $array=$save;
    $array = array_flip($array);
    unset($array['']);
    $lastValue = array_pop(array_flip($array));
    }
    
    $time=microtime(true)-$time;
    echo "Last Value: $lastValue
    ";
    echo "array_flip took $time seconds
    ";
    unset($array_2);
    $time=microtime(true);
    
    for($run=0; $run<1000; $run++)
    {
    $array=$save;
    $lastValue = array_pop($array);
    while($lastValue==='')
     {
     $lastValue = array_pop($array);
     }
    }
    
    $time=microtime(true)-$time;
    echo "Last Value: $lastValue
    ";
    echo "array_pop took $time seconds
    ";
    

    The winner is:

    function array_last_noempty($array)
    {
    $lastValue = array_pop($array);
    while($lastValue==='')
     {
     $lastValue = array_pop($array);
     }
    return $lastValue;
    }
    
    评论
  • dptsivmg82908 2011-12-27 07:15
    关注

    You can this operation with a function as well:

    <?php
        function getLastElement($data=array())
        {
            for($i=0; $i<count($data); $i++)
            {
                if($i==count($data)-1)
                {
                    $end = $data[$i];
                }
            }
            return $end;
        }
    ?>
    
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 docker无法进入容器内部
  • ¥15 qt https 依赖openssl 静态库
  • ¥15 python flask 报错
  • ¥15 改个密码引发的项目启动问题
  • ¥100 CentOS7单线多拨
  • ¥15 debian安装过程中老是出现无法将g21dr复制到g21dr怎么解决呀?
  • ¥15 如何用python实现跨工作簿的指定区域批量复制粘贴
  • ¥15 基于CH573f的雷迪安CR1400m通讯代码
  • ¥15 matlab m文件如何与matla app designer 数据交互?
  • ¥15 根据控制框图,传递函数求解