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 6 of 6
  1. #1
    Senior Coder
    Join Date
    Jul 2005
    Location
    UK
    Posts
    1,051
    Thanks
    6
    Thanked 13 Times in 13 Posts

    Working With The Results Of explode()

    I've used explode() to seperate a string of words into a whole bunch of substrings, and now need to manipulate them a bit beyond merely echoing them out one by one.

    Firstly, there will be duplicated substrings, how would I go about removing them?

    After removing any duplicates I then need to echo all the remaining substrings as a list on my page, how would I run a while loop on them to do this?

    Thanks in advance.

  • #2
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts
    Take a look a array_unique

    http://us2.php.net/array_unique

    as for a quick simple display
    Code:
    <PRE>
    print_r($array_name);
    </pre>
    Lite...

  • #3
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts

  • #4
    Senior Coder
    Join Date
    Jul 2005
    Location
    UK
    Posts
    1,051
    Thanks
    6
    Thanked 13 Times in 13 Posts
    Thank you.

    Okay, I've taken my best stab at this now - how am I doing? Seriously, I'm a little stuck with the for loop and would appreciate some help.

    Firstly, the data is a string of words seperated by spaces. Processing the array looks like this:

    PHP Code:
    $taglist mysql_query ("SELECT tags FROM bookmarks");
    $tags1 explode (" "$taglist);
    $tags array_unique ($tags1);
    $numtags count ($tags); 
    That seems simple enough so far. Now the for loop:

    PHP Code:
    for ($numtags$numtags 0$numtags--) {
      echo 
    $tags[0];

    Firstly, I'm assuming using $numtags as the initial value is the right thing to do here, given that I aim to echo each substring individually, correct?

    Secondly, is "$numtags--" the right way to decrement?

    Thirdly, how do I increment the array number in each iteration of the loop?

    Thanks.

  • #5
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Try:
    PHP Code:
    for($i 0$i $numtags$i++)
    {
       echo 
    $tags[$i] . "<br>";


  • #6
    Senior Coder
    Join Date
    Jul 2005
    Location
    UK
    Posts
    1,051
    Thanks
    6
    Thanked 13 Times in 13 Posts
    Ah, the joys of lateral thinking. Thanks Brandoe

    EDIT: A further problem has arisen.

    The current code is echoing out the following:

    Resource id #2
    I've read enough to establish that there is a problem in that I'm not converting the data in to a usable form. My gut instinct is that mysql_fetch_array needs to be incorporated somewhere but I'm not sure how or where.

    PHP Code:
    $taglist mysql_query ("SELECT tags FROM bookmarks") or die(mysql_error());
    $tags array_unique (explode (" "$taglist));
    $numtags count ($tags); 
    PHP Code:
    for($i 0$i $numtags$i++) 
    {
       echo 
    $tags[$i] . "<br>";

    I've checked that I'm connected to the database, there is no problem there.
    Last edited by Pennimus; 09-01-2006 at 10:37 PM.


  •  

    Posting Permissions

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