Codeiginiter
Codeiginiter

Reputation: 99

get last element of array in php which not blank

I have issue with the PHP Array. I want to get the last element from array which is not null or not blank.

        $string  = '9580 County Road
                    Clarence Center, New York 14032 
                    TEL: 716-863-4133
                    FAX: 716-741-4810 
                    <a href="mailto:[email protected]">[email protected]</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;
            }
        }

when i search on google, I find some below link, but which is not useful for me :-

Upvotes: 1

Views: 175

Answers (1)

Pupil
Pupil

Reputation: 23958

Try this.

Your string contains space characters at the end.

So, you need to trim it.

<?php
$string  = '9580 County Road
                        Clarence Center, New York 14032 
                        TEL: 716-863-4133
                        FAX: 716-741-4810 
                        <a href="mailto:[email protected]">[email protected]</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>';
?>

Working Demo

Upvotes: 2

Related Questions