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 9 of 9

Thread: array question

  1. #1
    Regular Coder
    Join Date
    Dec 2006
    Posts
    417
    Thanks
    168
    Thanked 1 Time in 1 Post

    array question

    If I have a string like this:

    $headlineKeywords = "Relatives firefighters blast Giuliani";

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

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

  • #2
    New Coder
    Join Date
    Jul 2007
    Posts
    65
    Thanks
    2
    Thanked 0 Times in 0 Posts

    array stuff

    Quote Originally Posted by Bobafart View Post
    If I have a string like this:

    $headlineKeywords = "Relatives firefighters blast Giuliani";

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

    Code:
    $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...
    PHP Code:
    $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 :-|

  • #3
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    I'm going to guess that there are likely more than one space in between the words. I would use a preg_split()
    PHP Code:
    <?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.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #4
    New Coder
    Join Date
    Jul 2007
    Posts
    65
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by _Aerospace_Eng_ View Post
    I'm going to guess that there are likely more than one space in between the words. I would use a preg_split()
    PHP Code:
    <?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); ?

  • #5
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Quote Originally Posted by clunky View Post
    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.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #6
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    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().

    .
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #7
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    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.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #8
    Regular Coder
    Join Date
    Dec 2006
    Posts
    417
    Thanks
    168
    Thanked 1 Time in 1 Post
    thanks for the help guys

    so I am doing this:

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

    Code:
    Array ( [0] => AMR [1] => plane [2] => diverted [3] => NY [4] => crew [5] => concern [6] => )
    the 6th element in the array is now blank.....

  • #9
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    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.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.


  •  

    Posting Permissions

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