Solarpitch Posted November 23, 2008 Share Posted November 23, 2008 Hey, Is it possible to get the first and last value of an array? If I had for example... <?php $names[] = "Gerard"; $names[] = "John"; $names[] = "Keith"; $names[] = "Aaron"; // How can I tell get that the first value is Gerard and the last is Aaron. I'm thinking I'd have to count the values in the array then use $names[0] = first and $names[4] = last... just not sure how to do it ?> Link to comment https://forums.phpfreaks.com/topic/133897-solved-getting-the-first-and-last-value-of-an-array/ Share on other sites More sharing options...
dezkit Posted November 23, 2008 Share Posted November 23, 2008 I think echo $names[1] . $names[4]; not sure though edit: nevermind i just read your php comment Link to comment https://forums.phpfreaks.com/topic/133897-solved-getting-the-first-and-last-value-of-an-array/#findComment-696998 Share on other sites More sharing options...
PFMaBiSmAd Posted November 23, 2008 Share Posted November 23, 2008 Solving programming problems involves trying what you think works and observing if the results are what you expect. I'm thinking I'd have to count the values in the array then use $names[0] = first and $names[4] = last... You already have a statement of what you think will work, why not just give that a try and see if it works. http://us.php.net/count (hint: numeric array indexes are one less than the count because they start at zero.) Link to comment https://forums.phpfreaks.com/topic/133897-solved-getting-the-first-and-last-value-of-an-array/#findComment-697000 Share on other sites More sharing options...
darkfreaks Posted November 23, 2008 Share Posted November 23, 2008 you could also additionally use substr() to output the variables first and last letter if you want to output the whole variable by number count() is better off Link to comment https://forums.phpfreaks.com/topic/133897-solved-getting-the-first-and-last-value-of-an-array/#findComment-697001 Share on other sites More sharing options...
Solarpitch Posted November 23, 2008 Author Share Posted November 23, 2008 Cheers... got it now. Link to comment https://forums.phpfreaks.com/topic/133897-solved-getting-the-first-and-last-value-of-an-array/#findComment-697014 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.