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 4 of 4
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,674
    Thanks
    28
    Thanked 4 Times in 4 Posts

    Basic array problem

    Hi,

    I have a user input where they can input a list of keywords, ( just text input up tp 100 chars ). After santitising the input I get:

    PHP Code:
    $keywords "Simple Page, create website, free web page, simple website, eesy page, make easy page"
    What I want to derive from this is a set of five individual keywords
    like this:

    PHP Code:
    $key_array = array();

    $key1 =     $key_array[0];
    $key2 =     $key_array[1];
    $key3 =     $key_array[2];
    $key4 =     $key_array[3];
    $key5 =     $key_array[4]; 
    My question is: how do I get the data from $keywords
    into my $key_array array AND how do I ONLY take the first 5 ?

    So if there are six comma separated phrases in $keywords, (like above) I only want the first five.

    If there are only three comma separated phrases in $keywords what will happen ? It mustn't "fail" - I need to "pad" the missing keys with some relevant data.

    Thanks for any help.


    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,859
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    You could first explode() it and then array_slice()
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Senior Coder
    Join Date
    May 2006
    Posts
    1,674
    Thanks
    28
    Thanked 4 Times in 4 Posts
    I think I still need to check that the first FIVE actually have
    some keywords in ...

    Does isset() work on array elements ?

    Like this:

    PHP Code:
    if ( isset($key_array[0]) {
       
    $key1 =  $key_array[0]; 
       }
    else {
      
    $key1 =  "dummy keyword 1"
       }

    if ( isset(
    $key_array[1]) {
       
    $key2 =  $key_array[1]; 
       }
    else {
      
    $key2 =  "dummy keyword 2"
       }

    etc
    Thanks.


    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,859
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Does isset() work on array elements ?
    Yes, but if your requirement is to eliminate any empty cells, then you could use array_filter()
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


  •  

    Posting Permissions

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