Search a PHP array for a string and sort by the number of matches (relevancy)

I have a PHP array containing a series of text articles and would like to be able to search for a string inside it then sort the results according to the number of occurrences discovered. For example if I looked for the word 'cat' it would return all the records containing the word 'cat' sorted by the number of times it was found in each one of the records within the array. Would anyone be able to help me with the PHP code?
Thanks in advance for your help
fdranAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rok-KraljCommented:
Here you go...

the first result is then $return[0][1], the second is $return[1][1] und so weiter...
<?php
	$texts=array(
		'this is this is this',
		'this is only once',
		'this is this twice'
	);
	$word='this';

	$return=array();
	foreach($texts as $text) {
		$return[]=array(substr_count($text, $word), $text);
	}

	sort($return);
	print_r($return);
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fdranAuthor Commented:
Thank you Rok-Kralj,
What if the array was multidimensional?
0
Rok-KraljCommented:
Miltidimensional - how? Post an example or explain what you mean.

Then you make it one dimensional and apply same code.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.