Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

count and match phrases within an array 1

Status
Not open for further replies.

pushyr

Programmer
Jul 2, 2007
159
GB

i have an array with page titles

Array
(
[0] => Advertising
[1] => Advertising Agency for UK and London
[2] => Advertising Agency London
[3] => Advertising Agency Services
[4] => Advertising Agency | London | UK - Internet Advertising and Traditional Campaigns
[5] => Advertising Agency | London | UK - Internet Advertising and Traditional Campaigns
[6] => 3D Design
[7] => 3D Design
[8] => 3D Design
)


i wondering how i could

1) count the number of page titles within the array that are unique
2) match and output the page titles that are not unique
 
Code:
function countUnique($array){
 return count ( array_unique[$array]);
}

i don't undestand the purpose of the second exercise, so I am not sure that i have properly understood you. can you clarify?
 
hey jpade, for the 2nd exercise i would like to...

1) separate the unique from the non-uniques
2) then match the non-uniques
3) count how many non-uniques match together
4) output a list of the non-uniques
 
and what have you done to fulfil that goal so far?
 
haven't done anything so far... just got up to this part and stalled... with head hanging down in shame
 
something like this perhaps?
Code:
<?php

//get unique values
$unique = array_unique($array); //i.e. this will capture ONE of each duplicate and each non-duplicate

//number of unique
$numUnique = count ($unique);

//get array of duplicates
$duplicates = array_intersect($array, $unique);

//num Duplicates
$numDuplicates = count ($duplicates);

echo "Non Uniques are : <br/><pre>";
print_r($duplicates);

?>
 
i'll give this a try and let you know tomorrow morn!!
 

hey jpadie, how do i get

//get unique values
$unique = array_unique($array); //i.e. this will capture ONE of each duplicate and each non-duplicate

how do i separate this to get to get...
one array with the uniques
a second array with the non-uniques (one of each)
 
tell you what. why don't you post three arrays here. one input array, one unique array and one non-unique array so that we can all see exactly what you want?

personally i thought that the code above gave you what you were after but i must have misunderstood.
 
ok.. here's the definite thing i want to do,

input array....

Array
(
[0] => Advertising
[1] => Advertising Agency for UK and London
[2] => Advertising Agency London
[3] => Advertising Agency Services
[4] => Advertising Agency | London | UK - Internet Advertising and Traditional Campaigns
[5] => Advertising Agency | London | UK - Internet Advertising and Traditional Campaigns
[6] => 3D Design
[7] => 3D Design
[8] => 3D Design
[9] => 3D Design template
[10] => 3D Design template
)


output array uniques.....

Array
(
[0] => Advertising
[1] => Advertising Agency for UK and London
[2] => Advertising Agency London
[3] => Advertising Agency Services
)



output array non uniques....

Array
(
[0] => Advertising Agency | London | UK - Internet Advertising and Traditional Campaigns
[1] => Advertising Agency | London | UK - Internet Advertising and Traditional Campaigns
[2] => 3D Design
[3] => 3D Design
[4] => 3D Design
[5] => 3D Design template
[6] => 3D Design template
)



what i'll do next with the non unique array is...
capture one of each duplicate and count them (x number of page titles had duplicates)
plus it would be nice to say how many duplicates there were (3D Design had 3 duplicates, and so forth)
 
ok. still can't work out what you would want this for and am 100% certain that there are better ways to deal with this (perhaps in the sql code or in the original array generation). but here is code that does what you want.

Code:
<?php
//create the array
$array = Array
(
    0 => 'Advertising',
    1 => 'Advertising Agency for UK and London',
    2 => 'Advertising Agency London',
    3 => 'Advertising Agency Services',
    4 => 'Advertising Agency | London | UK - Internet Advertising and Traditional Campaigns',
   	5 => 'Advertising Agency | London | UK - Internet Advertising and Traditional Campaigns',
    6 => '3D Design',
    7 => '3D Design',
    8 => '3D Design',
    9 => '3D Design template',
    10 => '3D Design template'
);
$unique = array();
$duplicates = array();
$deDuplicates = array();
$count = array_count_values($array);
foreach ($count as $key=>$val){
	if ($val === 1 ){
		$unique [] = $key;
	} else {
		$deDuplicates[] = $key; //fill a depuplicated array
		$duplicates = array_merge($duplicates, array_fill(0, $val, $key));
	}
}
echo "<pre>";
echo "input array\r\n";
print_r ($array);
echo "uniques\r\n";
print_r($unique);
echo "dupicates \r\n";
print_r($duplicates);
echo "de-duplicated duplicates\r\n";
print_r($deDuplicates);
?>

output is:
Code:
input array
Array
(
    [0] => Advertising
    [1] => Advertising Agency for UK and London
    [2] => Advertising Agency London
    [3] => Advertising Agency Services
    [4] => Advertising Agency | London | UK - Internet Advertising and Traditional Campaigns
    [5] => Advertising Agency | London | UK - Internet Advertising and Traditional Campaigns
    [6] => 3D Design
    [7] => 3D Design
    [8] => 3D Design
    [9] => 3D Design template
    [10] => 3D Design template
)
uniques
Array
(
    [0] => Advertising
    [1] => Advertising Agency for UK and London
    [2] => Advertising Agency London
    [3] => Advertising Agency Services
)
dupicates 
Array
(
    [0] => Advertising Agency | London | UK - Internet Advertising and Traditional Campaigns
    [1] => Advertising Agency | London | UK - Internet Advertising and Traditional Campaigns
    [2] => 3D Design
    [3] => 3D Design
    [4] => 3D Design
    [5] => 3D Design template
    [6] => 3D Design template
)
de-duplicated duplicates
Array
(
    [0] => Advertising Agency | London | UK - Internet Advertising and Traditional Campaigns
    [1] => 3D Design
    [2] => 3D Design template
)
 
thanks jpadie... i'm all sorted!!

as you said there are various ways to do this and i'd streamlined it down to a combination of mysql and using bits of your code!!

yesterday i just didnt have the insight to go any further.. but both yesterday and today you've definitely helped me out!
 
no probs. there are loads of array functions and the difficulty is always understanding the needs before you go for the right one. you can always sort these things out with iterative processes, but if you can use a built in function then you'll benefit in terms of speed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top