Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-19-2010, 08:41 PM   PM User | #1
DoA
New Coder

 
Join Date: Mar 2010
Posts: 14
Thanks: 2
Thanked 0 Times in 0 Posts
DoA is an unknown quantity at this point
Limit Array Size / Values

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??

Code:
// 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......

Code:
// 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);

Last edited by DoA; 03-19-2010 at 08:56 PM.. Reason: Solved my own problem
DoA is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:25 PM.


Advertisement
Log in to turn off these ads.