View Full Version : How do I get a max number of elements in my array ?

02-09-2010, 07:52 AM

I am creating an array from a table,
(if I can get it to work - see other thread)

Because it is dynamic, I do not know how many elements
the array will contain. But I do want to only have a maximum of 60
and these 60 must be the last 60 elements.

Most of the time the number of elements will be lower than 40, but I must make sure that it is less than 61 elements.

So if my array has (data1, data2, ... data60, data61, data62, data63)

then I want to strip out the first three data elements and leave the last 60 in the array.

Does anyone know how I can achieve this ?



02-09-2010, 09:34 AM

$max = 60;
$count = count( $items );

if ( $max < $count )
$items = array_slice( $items, $count - $max );

Untested, but that should do the trick.

02-09-2010, 11:28 AM
Thanks for that,

I would not have thought about a function called "array_slice"

anyway I checked it out in the manual and it looks just what I want.

BTW - can you see what my problem is in this thread?

Trying to understand variable variables

Thanks again.