SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    convert string to array

    Hi,

    I have a string like this

    $string = "computer mousecomputer keyboardgame padmonitorphonecardogcat"

    when read correctly they are:

    computer mouse
    computer keyboard
    game pad
    monitor
    phone
    car
    dog
    cat

    The problem is I don't know what to put in explode() first parameter to get the array with those values.

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    you need something to separate the items.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You cannot do it with that string I'm afraid, there is no programmatic approach given the information supplied.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think i should've started from the beginning

    Code PHP:
    			while ($subcat_rows1 = mysql_fetch_assoc($subcat_result1))
    			{
    				$groups = $subcat_rows1['meaning'];
    			}

    echo $groups

    output will be the string I've given.

    Is there any way we can format the string before converting to array ?

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    If 'meaning' contains each individual entry ('game pad' for example) you could just alter your loop slightly to produce an array.

    PHP Code:
    $groups $subcat_rows1['meaning']; 
    PHP Code:
    $groups[] = $subcat_rows1['meaning']; 
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #6
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks.


Bookmarks

Posting Permissions

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