Results 1 to 5 of 5

Thread: Convert String to array

  1. #1
    Join Date
    Nov 2010
    Posts
    114
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Convert String to array

    Hi All,


    Can anyone tell me how to convert string into array. Following is my issue

    i want to convert

    $string = "foo,bar,hallo,world"; // in one string

    to


    $array = array("foo", "bar", "hallo", "world"); // need to be converted into single array like this


    Any help? Thanks in advance

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,952
    Thanks
    43
    Thanked 3,195 Times in 3,157 Posts
    Blog Entries
    12

    Default

    PHP Code:
    $array explode','$string ); 
    For more on explode, see:

    http://www.php.net/manual/en/function.explode.php
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Nov 2010
    Posts
    114
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default

    My real situation is to find count of matching words in two arrays

    Code:
    $array1 = array("foo", "baar", "checkers","cricket","games"); // static
    		$array2 = array("foo","running","tennis","games"); // static
    
    
    		$commonSkills = array_intersect($array1,$array2); // checks for intersection
    		$count = array_count_values($commonSkills); // count for arrays
    		foreach($count as $values)
    		{
    			$count = $count + $values; 
    		}
    echo "$count"; // this will give me number of matching words in $array1 and $array2.

    Now my issue is, From database i am getting values

    $array1 = "foo,bar,checkers,cricket,games"; // from db
    $array2 = "foo,running,tennis,games";

    When i am using explode functionality, it is not giving me exacts matching words. Is there any way to convert

    $string = "foo,bar,checkers,cricket,games";

    to


    $array = array("foo", "baar", "checkers","cricket","games");

    Or, is there anyway to get the count of matching words in two strings.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,952
    Thanks
    43
    Thanked 3,195 Times in 3,157 Posts
    Blog Entries
    12

    Default

    Your PHP code threw an error.

    By way of a demonstration that this can work:

    PHP Code:
    <?php
        $string1 
    "foo,baar,checkers,cricket,games,running";
        
    $string2 "foo,running,tennis,games";
        
    $array1 explode','$string1 );
        
    $array2 explode','$string2 );


        
    $commonSkills array_intersect($array1,$array2); // checks for intersection
        
    $countar array_count_values($commonSkills); // count for arrays
        
    $count 0;
        foreach(
    $countar as $values)
        {
            
    $count += $values
        }
        echo 
    $count;
    ?>
    But to get the count, you don't have to iterate over the array_count_values array, unless you want to count multiple matches of the same item (an item that repeats in the first array and matches at least one item in the other).

    You can just do:

    PHP Code:
    <?php
        $string1 
    "foo,baar,checkers,cricket,games,running";
        
    $string2 "foo,running,tennis,games";
        
    $array1 explode','$string1 );
        
    $array2 explode','$string2 );


        
    $commonSkills array_intersect($array1,$array2); // checks for intersection
        
    $countar array_count_values($commonSkills); // count for arrays
        
    echo count($countar);
    ?>
    Which would perhaps be more accurate. Depends what you're after.

    If you do want duplicates counted, even simpler would be:

    PHP Code:
    <?php
        $string1 
    "foo,baar,checkers,foo,cricket,games,running";
        
    $string2 "foo,running,tennis,running,games";
        
    $array1 explode','$string1 );
        
    $array2 explode','$string2 );


        
    $commonSkills array_intersect($array1,$array2); // checks for intersection
        
    echo count($commonSkills);
    ?>
    If there's white space in your strings after or brfore the commas and it's not uniform, see:

    http://www.php.net/manual/en/function.preg-split.php
    Last edited by jscheuer1; 03-06-2013 at 04:08 PM. Reason: add last code block and bit about white space
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. The Following User Says Thank You to jscheuer1 For This Useful Post:

    hemi519 (03-06-2013)

  6. #5
    Join Date
    Nov 2010
    Posts
    114
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default

    Thanks, Working fine

Similar Threads

  1. convert Results into Associative array
    By hemi519 in forum PHP
    Replies: 4
    Last Post: 02-19-2013, 10:34 PM
  2. convert string to uppercase value
    By boogyman in forum JavaScript
    Replies: 2
    Last Post: 07-04-2008, 01:02 AM
  3. String to Array function
    By hebs in forum PHP
    Replies: 1
    Last Post: 05-27-2008, 11:47 AM
  4. Convert String abbreviation value to numbers
    By bigalo in forum Looking for such a script or service
    Replies: 2
    Last Post: 04-02-2008, 02:10 PM
  5. Convert String abbreviation value to numbers
    By bigalo in forum JavaScript
    Replies: 4
    Last Post: 03-31-2008, 01:56 PM

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
  •