Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    DoA
    DoA is offline
    New Coder
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •