...

View Full Version : Resolved Limit Array Size / Values



DoA
03-19-2010, 08:41 PM
Hi guys,

I've got an unlimited text area ... I want to limit the number of elements in my array (say 10) ... the easiest way I think will be to allow an unlimited array & then remove them with PHP (to make it foolproof even for JS disabled browsers) ...

Here's what I'm trying ... am I even close??



// Get Textarea string
$textarea = $_POST['phrase'];

// Explode textarea on newlines to get individual search phrases
$textarea_array = explode("\n", $textarea);
$result = count($textarea_array);

$i = 10;
while ($i<=$result){
unset $textarea_array[$i];
$i++;
}
$result = count($textarea_array);
echo $result; // Just a check to see if there are more than 10 results.


As usual, many thanks for your time & assistance!


-----------------------------------------------------

For those looking for the same thing......



// Get Textarea string
$textarea = $_POST['phrase'];

// Explode textarea on newlines to get individual search phrases
$textarea_array = explode("\n", $textarea);


// Slice off the unwanted items
// First value should be 0 (start of array)
// Second value should be your upper limit (10 in my case)
// Anything about the upper limit will be removed

$ResultArray = array_slice($textarea_array, 0, 10);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum