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
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".

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.