Page 1 of 1

Compare elements in two arrays

Posted: Sun Jun 24, 2012 5:29 pm
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 :)

Re: Compare elements in two arrays

Posted: Mon Jun 25, 2012 12:50 am
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

Re: Compare elements in two arrays

Posted: Mon Jun 25, 2012 6:16 am
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