...

View Full Version : Basic array problem



jeddi
02-24-2012, 10:29 AM
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:


$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:


$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.


.

abduraooft
02-24-2012, 11:01 AM
You could first explode() (http://php.net/explode) it and then array_slice() (http://php.net/array_slice)

jeddi
02-27-2012, 09:25 PM
I think I still need to check that the first FIVE actually have
some keywords in ...

Does isset() work on array elements ?

Like this:



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.


.

abduraooft
02-28-2012, 02:52 PM
Does isset() work on array elements ?
Yes, but if your requirement is to eliminate any empty cells, then you could use array_filter()



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum