Well if you only want the last row then the foreach loop isn’t needed.
Your code would just be:
[php]$rows = explode("\n", $_POST[‘var’]);
$lastrow = array_pop($rows);
echo $lastrow;[/php]
Well if you only want the last row then the foreach loop isn’t needed.
Your code would just be:
[php]$rows = explode("\n", $_POST[‘var’]);
$lastrow = array_pop($rows);
echo $lastrow;[/php]
thankyou. it works. Is there any other function that lets me view say the last 4(any number for that matter) lines of the text area.
something like this
[php]$lastrow = array_pop(“4”, $rows);[/php]
Im just guessing the above code, it could be completely wrong ?
Also,can end() be used here instead of array_pop?
TO grab the last X elements in your array, you can use the array_slice() function with a negative offset. So for example, if you wanted the last 4 elements, you can do
[php]
$lastFour = ($rows, -4);//grab last 4
[/php]
As for your second question, yes you could. Both functions return the last element of an array. The difference is that array_pop() removes that element from the array (so the array is 1 element shorter) and end doesn’t.
Sorry I made a type. The first code snippet should say
[php]
$lastFour = array_slice($rows, -4);
[/php]
[php]$lastFour = array_slice($rows, -4);[/php]
It doesn’t seem to work. I get the output as “Array”
I tried even a simple program and it dosen give me the required output.
[php]<?php
include “config.php”;
$name = $_POST[‘name’];
$data = $_POST[‘data’];
$explode = (explode("\n", $data);
$last = array_slice($explode, -2);
echo $last;
?>[/php]
There must be a basic error somewhere.
ignore the extra parenthesis in $explode .
bump, some help please
You can’t echo an array.
[php]$last = array_slice($explode, -2);
//echo $last;
echo ‘
’;
print_r($last);
echo ‘’;[/php]
Thanks, That did the trick