Answers for "how to get last value in for loop in php"

PHP
17

how to get last array element in php

<?php

$source_array = ['key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'];

$result = end($source_array);

echo "Last element: ".$result;

?>
Posted by: Guest on August-05-2020
1

php foreach if last item

$numItems = count($arr);
$i = 0;
foreach($arr as $key=>$value) {
  if(++$i === $numItems) {
    echo "last index!";
  }
}
Posted by: Guest on May-19-2020

Code answers related to "how to get last value in for loop in php"

Browse Popular Code Answers by Language