Jump to content

Organise an array by occurence


newbtophp

Recommended Posts

print_r($titles) produces:

Array ( [coding] => 2 [rocks] => 1 [is the best] => 1 [uses] => 1 [has a tld] => 0 ) 

 

Im trying to make it so:

if [$titles] is 2 or above then it becomes a title, otherwise its placed under a global title called "others", then under each title the words from $oldarray are categorized based on if the title is included within the words, if the title doesn't then its placed under "others".

 

Current code:

 

<?php

function count_word_occurrences($str, $case_sensitive = true)
{

$str = $case_sensitive ? $str : strtolower($str);

$occurrences = array();

foreach(str_word_count($str, 1) as $word){

$word = $case_sensitive ? $word : strtolower($word);

if(!in_array($word, array_keys($occurrences))){

$occurrences[$word] = preg_match_all("~\b$word\b~", $str, $matches);

}

}

arsort($occurrences);
return array_slice(array_keys($occurrences), 0, 3);
}



function count_c($arr, $find)
{
$count = 0;
foreach($arr as $words)
{
	$count += preg_match("~\b$find\b~", $words);
}
return $count;
}

$oldarray = array('php coding united kingdom',
'united kingdom rocks',
'php is the best',
'united kingdom uses php',
'united kingdom coding',
'php has a united kingdom tld');

$newarray = array('coding', 'rocks', 'is the best', 'uses', 'coding', 'has a tld');

$titles = array();
foreach($newarray as $title)
{
$titles[$title] = count_c($oldarray, $title);
}

print_r($titles);

?>

 

 

Would Output:

 

Title: coding

 

php coding united kingdom,
united kindom coding

 

Title: others

 

united kingdom rocks,
php is the best,
united kingdom uses php,
php has a united kingdom tld

 

 

:-\

Link to comment
https://forums.phpfreaks.com/topic/190305-organise-an-array-by-occurence/
Share on other sites

OK i've managed to progress:

 

Code:

 

<?php

function count_word_occurrences($str, $case_sensitive = true)
{

$str = $case_sensitive ? $str : strtolower($str);

$occurrences = array();

foreach(str_word_count($str, 1) as $word){

$word = $case_sensitive ? $word : strtolower($word);

if(!in_array($word, array_keys($occurrences))){

$occurrences[$word] = preg_match_all("~\b$word\b~", $str, $matches);

}

}

arsort($occurrences);
return array_slice(array_keys($occurrences), 0, 3);
}



function count_c($arr, $find)
{



$count = 0;



foreach($arr as $words)



{





$count += preg_match("~\b$find\b~", $words);



}



return $count;
}

$oldarray = array('php coding united kingdom',
'united kingdom rocks',
'php is the best',
'united kingdom uses php',
'united kingdom coding',
'php has a united kingdom tld');

$newarray = array('coding', 'rocks', 'is the best', 'uses', 'coding', 'has a tld');

$titles = array();
foreach($newarray as $title)
{



$titles[$title] = count_c($oldarray, $title);
}


foreach($titles as $k=>$v)
{
if($v > 1)
	{
	$realtitles[] = $k;
	}
else
	{
	$others[] = $k;
	}
}


echo "<br><br><b>titles</b>: ";

print_r($realtitles);


echo "<br><br><b>others</b>: ";


print_r($others);

?>

 

Now it gives:

 

Titles: Array ( [0] => coding )

 

Others: Array ( [0] => rocks [1] => is the best [2] => uses [3] => has a tld )

 

 

Just need to figure out how to display it in the following format:

 

Title: coding

 

php coding united kingdom,
united kindom coding

 

Title: others

 

united kingdom rocks,
php is the best,
united kingdom uses php,
php has a united kingdom tld

 

If the $title is in one of the values in $oldarray then echo that value below the title, if the title is not or has occured 1 time then it's echo'd under the title "others".

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.