Jump to content

[SOLVED] Getting the first and last value of an array?


Solarpitch

Recommended Posts

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



?>

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.)

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.