...

View Full Version : array question



Bobafart
07-12-2007, 11:34 AM
If I have a string like this:

$headlineKeywords = "Relatives firefighters blast Giuliani";

and I want to make an array out of it like this:



$array_headlineKeywords = split(" ",$headlineKeywords);


why do I get this output?

print_r($array_headlineKeywords);

Array ( [0] => Relatives [1] => firefighters [2] => blast [3] => Giuliani [4] => [5] => [6] => [7] => [8] => )

what's with 4, 5, 6, 7 and 8 being empty valued? I was hoping for only 0, 1, 2 and 3 not the extra blank elements.

clunky
07-12-2007, 11:45 AM
If I have a string like this:

$headlineKeywords = "Relatives firefighters blast Giuliani";

and I want to make an array out of it like this:



$array_headlineKeywords = split(" ",$headlineKeywords);


why do I get this output?

print_r($array_headlineKeywords);

Array ( [0] => Relatives [1] => firefighters [2] => blast [3] => Giuliani [4] => [5] => [6] => [7] => [8] => )

what's with 4, 5, 6, 7 and 8 being empty valued? I was hoping for only 0, 1, 2 and 3 not the extra blank elements.

Hi
I don't know if it will help but this works fine for me...


$array_headlineKeywords = explode(" ",$headlineKeywords);
for($i = 0; $i < count($array_headlineKeywords );$i++){
echo "$i = $array_headlineKeywords[$i]<br />";
}


Hope it helps

Just wish I could get an answer to my issue :-|

_Aerospace_Eng_
07-12-2007, 11:49 AM
I'm going to guess that there are likely more than one space in between the words. I would use a preg_split()

<?php
$headlineKeywords = "Relatives firefighters blast Giuliani";
$array_headlineKeywords = preg_split("/\s+/",$headlineKeywords);
print_r($array_headlineKeywords);
?>
this way there can any amount of whitespace in between words.

clunky
07-12-2007, 11:54 AM
I'm going to guess that there are likely more than one space in between the words. I would use a preg_split()

<?php
$headlineKeywords = "Relatives firefighters blast Giuliani";
$array_headlineKeywords = preg_split("/\s+/",$headlineKeywords);
print_r($array_headlineKeywords);
?>
this way there can any amount of whitespace in between words.


Does that work better than explode(" ",$whatever); ?

_Aerospace_Eng_
07-12-2007, 11:58 AM
Does that work better than explode(" ",$whatever); ?
Well if there is more than one space in between words which their might be then yeah its better because it uses regex to read the whitespace.

StupidRalph
07-12-2007, 12:01 PM
You shouldn't use split() unless your delimiter is a reg. expression and you want to split into an array. If your delimiter is a string (which it is) then you would use explode() or str_split() which are slightly different but both faster than split().

I haven't tested which is faster between the two explode() - str_split().

.

StupidRalph
07-12-2007, 12:06 PM
You guys are QUICK!!!! lol

Is there ever a time you'd want to use split()? I'd opt to use preg_split() since its faster.

Bobafart
07-12-2007, 12:45 PM
thanks for the help guys

so I am doing this:



$array_headlineKeywords = preg_split("/\s+/",$headlineKeywords);
$num_array_headlineKeywords = count($array_headlineKeywords);
print_r($array_headlineKeywords);


the string is: "AMR plane diverted NY crew concern"

and I am getting this:




Array ( [0] => AMR [1] => plane [2] => diverted [3] => NY [4] => crew [5] => concern [6] => )


the 6th element in the array is now blank.....

StupidRalph
07-12-2007, 01:02 PM
Not sure why you'd even have the 6th element unless you had a trailing space in your string.

You can probably trim() your string before you use your preg_split(). In addition, you can use array_filter() to get rid of any NULL, false, or empty string values.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum