etrader Posted April 1, 2011 Share Posted April 1, 2011 I want to echo the last two (or at least the last) string of an array. What is the simplest way to do so ? Quote Link to comment https://forums.phpfreaks.com/topic/232427-calling-the-last-string-of-an-array/ Share on other sites More sharing options...
will35010 Posted April 1, 2011 Share Posted April 1, 2011 You could cycle through the array using a loop to increment a counter and then echo the string using the counter. Quote Link to comment https://forums.phpfreaks.com/topic/232427-calling-the-last-string-of-an-array/#findComment-1195580 Share on other sites More sharing options...
will35010 Posted April 1, 2011 Share Posted April 1, 2011 Example: count = "0"; foreach ( $array as $value ) { // Do stuff with $value count++; } $count1 = $count - '1'; //echo next before last array echo $array['$count1;]; //echo last array echo $array['$count;]; I didn't test this, but it should be close. Quote Link to comment https://forums.phpfreaks.com/topic/232427-calling-the-last-string-of-an-array/#findComment-1195595 Share on other sites More sharing options...
DavidAM Posted April 1, 2011 Share Posted April 1, 2011 $last = end($array); $beforeLast = prev($array); See the manual for end() and prev() Quote Link to comment https://forums.phpfreaks.com/topic/232427-calling-the-last-string-of-an-array/#findComment-1195600 Share on other sites More sharing options...
will35010 Posted April 1, 2011 Share Posted April 1, 2011 $last = end($array); $beforeLast = prev($array); See the manual for end() and prev() I knew there would be an easier way! lol Quote Link to comment https://forums.phpfreaks.com/topic/232427-calling-the-last-string-of-an-array/#findComment-1195614 Share on other sites More sharing options...
etrader Posted April 1, 2011 Author Share Posted April 1, 2011 That's exactly that way I hoped to find Thanks so much Quote Link to comment https://forums.phpfreaks.com/topic/232427-calling-the-last-string-of-an-array/#findComment-1195742 Share on other sites More sharing options...
.josh Posted April 1, 2011 Share Posted April 1, 2011 sidenote...if you know that all you are ever going to want is the last one or two, then you can do as previous posters have shown. But if you want to be able to grab a variable amount, you will have to put those in a loop. The better way to handle this is to do something like this: // number of elements from the end you want $n = 2; // if you don't care about preserving array keys use this $outputArray = array_slice($inputArray, -($n)); // if you care about preserving array keys, use this $outputArray = array_slice($inputArray, -($n), count($inputArray), true); Then you will have $outputArray as an array of the last element(s) in your original array. You will be able to specify an arbitrary number with $n and just loop through $outputArray. Quote Link to comment https://forums.phpfreaks.com/topic/232427-calling-the-last-string-of-an-array/#findComment-1195748 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.