Jhonny Jr. Jhonny Jr. - 5 months ago 9
PHP Question

Check same word on each array

I want to check that a string contains the same word when comparing 2 different array's. If there is the same word on each array it will show how many on each inside multidimensional array

Array one is common array type and array two is multidimensional array

Array 1:

Array
(
[0] => royalty
[1] => free
[2] => picture
)


Array 2:

Array
(
[0] => Array
(
[0] => Affordable and search from millions of royalty free picture
)

[1] => Array
(
[0] => from millions of royalty picture
)

[2] => Array
(
[0] => Provides free picture upload and hosting
)

[3] => Array
(
[0] => Post your picture here Get permanent links
)

[4] => Array
(
[0] => Choose your own unique username to access image
)

)


Result:

Array
(
[0] => Array
(
[0] => 3
)

[1] => Array
(
[0] => 2
)

[2] => Array
(
[0] => 2
)

[3] => Array
(
[0] => 1
)

[4] => Array
(
[0] => 0
)

)


From the above example, array contain each word of royalty free picture will show how many word the same in each inside multidimensional array

I was trying it using strcasecmp() but it just give me 0 results if two string contains same word and I think it can't give right result for string with so many words.

Answer

Split string and find intersection with the array of seached words

foreach($array2 as &$item)
   $item[0] = count(array_intersect($array1, explode(' ', $item[0])));
Comments