View Full Version : Convert String to array
hemi519
03-06-2013, 12:35 PM
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
jscheuer1
03-06-2013, 12:44 PM
$array = explode( ',', $string );
For more on explode, see:
http://www.php.net/manual/en/function.explode.php
hemi519
03-06-2013, 01:13 PM
My real situation is to find count of matching words in two arrays
$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.
jscheuer1
03-06-2013, 04:47 PM
Your PHP code threw an error.
By way of a demonstration that this can work:
<?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
$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
$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
hemi519
03-06-2013, 04:58 PM
Thanks, Working fine
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.