Rafee
Rafee

Reputation: 4078

Retrieve the last value from array with 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.

Upvotes: 1

Views: 1263

Answers (5)

Alasdair
Alasdair

Reputation: 14103

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).")\n";
$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\n";
echo "array_filter took $time seconds\n";
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\n";
echo "array_flip took $time seconds\n";
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\n";
echo "array_pop took $time seconds\n";

The winner is:

function array_last_noempty($array)
{
$lastValue = array_pop($array);
while($lastValue==='')
 {
 $lastValue = array_pop($array);
 }
return $lastValue;
}

Upvotes: 2

Jamshid Hashimi
Jamshid Hashimi

Reputation: 7825

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;
    }
?>

Upvotes: 0

deceze
deceze

Reputation: 522015

Filter out the empty values, take the last value:

$array = array_filter($arrayname1);
$lastValue = end($array);

Upvotes: 9

paulsm4
paulsm4

Reputation: 121609

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

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

Upvotes: 0

uzair
uzair

Reputation: 766

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);
?>

Upvotes: 1

Related Questions