-
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
-
PHP Code:
$array = explode( ',', $string );
For more on explode, see:
http://www.php.net/manual/en/function.explode.php
-
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.
-
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
-