Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取php中数组的最后一个非空元素_Php_Arrays - Fatal编程技术网

获取php中数组的最后一个非空元素

获取php中数组的最后一个非空元素,php,arrays,Php,Arrays,我对PHP数组有意见。我想从数组中获取最后一个元素,它不是null或不是空的。 $string = '9580 County Road Clarence Center, New York 14032 TEL: 716-863-4133 FAX: 716-741-4810 <a href="mailto:rsch

我对PHP数组有意见。我想从数组中获取最后一个元素,它不是
null
或不是空的。

        $string  = '9580 County Road
                    Clarence Center, New York 14032 
                    TEL: 716-863-4133
                    FAX: 716-741-4810 
                    <a href="mailto:rschaefer@wmssales.com">rschaefer@wmssales.com</a>
                    <a href="http://www.wmssales.org" target="_blank">wmssales.org</a>
                    Randy Schaefer';
        $array=explode("\n",$string);
        $new_array = array_filter($array);
        $rev_new_array=array_reverse($new_array);
        for( $i = 0; $i < sizeof($rev_new_array); $i++)
        {
            if($rev_new_array[$i]!="")
            {
                echo $writter = $rev_new_array[$i];break;
            }
        }
$string='9580县道
纽约克拉伦斯中心14032
电话:716-863-4133
传真:716-741-4810
兰迪·谢弗;
$array=explode(“\n”,$string);
$new\u array=array\u filter($array);
$rev_new_array=array_reverse($new_array);
对于($i=0;$i
当我在谷歌上搜索时,我在下面找到一些链接,但这些链接对我没有用处:-

    • 试试这个

      字符串末尾包含空格字符

      所以,你需要修剪它

      <?php
      $string  = '9580 County Road
                              Clarence Center, New York 14032 
                              TEL: 716-863-4133
                              FAX: 716-741-4810 
                              <a href="mailto:rschaefer@wmssales.com">rschaefer@wmssales.com</a>
                              <a href="http://www.wmssales.org" target="_blank">wmssales.org</a>
                              Randy Schaefer
                                ';
      $string = trim($string);
      $array=explode("\n",$string);
      echo '<pre>';
      print_r(end($new_array))
      echo '</pre>';
      ?>
      
      
      

      你拥有的东西很不雅观,但应该有用。有什么问题吗?非常感谢你节省我的时间我已经在这个问题上花了1个小时。我也使用
      trim()
      ,但在我的例子中,我使用
      for
      循环接受时间是10分钟,你回答的速度非常快