Compare elements in two arrays

Ask about a PHP problem here.
Post Reply
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Compare elements in two arrays

Post by Temor »

I have two arrays. One containing each keyword in a search term, and one containing information about the videos matching the search.
Array
(
    [0] => This
    [1] => is
    [2] => a
    [3] => search
    [4] => term
)
Array
(
    [0] => Array
        (
            [title] => This is a video title.
            [description] => This is a video description. It contains words and stuff.
        )

    [1] => Array
        (
            [title] => Words. Wooo!
            [description] => This is also a video description. 
        )

)
I want to count each occurrence of a keyword in every element in the video array and then combine the total matches and put them into one array like this:
Array
(
    [0] => Array
        (
            [title] => This is a video title.
            [description] => This is a video description. It contains words and stuff.
	    [matches]  => 6
        )

    [1] => Array
        (
            [title] => Words. Wooo!
            [description] => This is also a video description. 
	    [matches]  => 3
        )

)
This is where I'm stuck.
I was thinking about using str_replace() to count the occurrences, but every way I turn this it requires me to run a loop inside a loop, which as far as I know is not possible.
So, if anyone has a brilliant solution that I'm too slow to figure out myself, please do share :)
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Compare elements in two arrays

Post by jacek »

Temor wrote:but every way I turn this it requires me to run a loop inside a loop, which as far as I know is not possible.
It's very possible ! :D
for ($x = 1; $x < 10; ++$y){
    echo '<div>';    
    
    for ($y = 0; $y < $x; ++$y){
        echo '* ';
    }
    
    echo '</div>';
}
Try that out ;)

You can use substr_count too, example
$search = array('This', 'is', 'a', 'search', 'term');

$results =	array(
				array(
					'title' => 'This is a video title.',
					'description' => 'This is a video description. It contains words and stuff.',
				),
				
				array(
					'title' => 'Words. Wooo!',
					'description' => 'This is also a video description.',
				),
    		);

foreach ($results as &$result){
	$matches = 0;
	
	foreach ($result as $string){
		foreach ($search as $term){
			$matches += substr_count($string, $term);
		}
	}
	
	$result['matches'] = $matches;
}

print_r($results);
problem is this does not count words, it counts occurrences of a string so not that useful but for the fun of it lets simplify using array_map
$search = array('This', 'is', 'a', 'search', 'term');

$results =	array(
				array(
					'title' => 'This is a video title.',
					'description' => 'This is a video description. It contains words and stuff.',
				),
				
				array(
					'title' => 'Words. Wooo!',
					'description' => 'This is also a video description.',
				),
    		);

foreach ($results as &$result){
	$result['matches']  = array_sum(array_map('substr_count', array_fill(0, count($search), $result['title']), $search));
	$result['matches'] += array_sum(array_map('substr_count', array_fill(0, count($search), $result['description']), $search));
}

print_r($results);
Okay I lied that's not simpler.

The most working way to do it is probably to split each string up into words and compare each one.
$search = array('This', 'is', 'a', 'search', 'term');

$results =	array(
				array(
					'title' => 'This is a video title.',
					'description' => 'This is a video description. It contains words and stuff.',
				),
				
				array(
					'title' => 'Words. Wooo!',
					'description' => 'This is also a video description.',
				),
    		);

foreach ($results as &$result){
	$string = implode(' ', $result);
	$result['matches'] = 0;
	
	preg_match_all('#[a-z]+#i', $string, $matches);
	
	foreach ($matches[0] as $word){
		if (in_array($word, $search)){
			++$result['matches'];
		}
	}
}

print_r($results);
But this is case sensitive so you would need to make sure your search term was strtolower()ed as were all the results when comparing...

Good luck :D
Image
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Compare elements in two arrays

Post by Temor »

It's working like a charm now. Thank you Jacek ! I had no idea you could run a loop inside a loop. So many things just got so much easier :D
Post Reply