Solved

php duplicates in an array

Posted on 2014-04-10
8
264 Views
Last Modified: 2014-04-10
I have the following array

array1 = array("test1","test2","test3","test4","test1","test1","test4","test5","test1","test4","test7","test7","test1","test7","test4")

I need a table output that with the duplicates and the quantity, like that

Name         Number of occurrences
test1                   5
test4                   4
test7                   3

Any idea?
0
Comment
Question by:joyacv2
  • 5
  • 3
8 Comments
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39992516
Maybe using array_count_values: http://www.php.net/manual/en/function.array-count-values.php

<?php
$array1 = array("test1","test2","test3","test4","test1","test1","test4","test5","test1","test4","test7","test7","test1","test7","test4");
print_r(array_count_values($array1));
?>

---------------

Array ( [test1] => 5 [test2] => 1 [test3] => 1 [test4] => 4 [test5] => 1 [test7] => 3 )

Open in new window

HTH,
Dan
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39992544
If you only want to use the duplicates, something like this:
<?php
//function to test if a value is 1
function not1($var) { return ($var != 1);}

//test array
$array1 = array("test1","test2","test3","test4","test1","test1","test4","test5","test1","test4","test7","test7","test1","test7","test4");

//get the count for each value of $array1
$array1freq = array_count_values($array1);

//filter the frequency array to only keep the duplicates
$array1dup = array_filter($array1freq, "not1");

print_r($array1dup);
?>
------------------
Array ( [test1] => 5 [test4] => 4 [test7] => 3 )

Open in new window

0
 
LVL 1

Author Comment

by:joyacv2
ID: 39992582
Hi,

I need the data in a table. Any idea?
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39992596
You have an array. That is a table...

You mean a HTML table? You're displaying that where?

Please post the HTML code of the page where you want to display those results.
0
 
LVL 1

Author Comment

by:joyacv2
ID: 39992630
Hi,

I need a table with tr and td, the only thing that i have is the array that i post. How I display the results into these td's?
0
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 39992631
Here's a simple HTML table to output the result:
<?php
//function to test if a value is 1
function not1($var) { return ($var != 1);}

//test array
$array1 = array("test1","test2","test3","test4","test1","test1","test4","test5","test1","test4","test7","test7","test1","test7","test4");

//get the count for each value of $array1
$array1freq = array_count_values($array1);

//filter the frequency array to only keep the duplicates
$array1dup = array_filter($array1freq, "not1");
?>
<table>
	<tr>
		<th>Name</th>
		<th>Number of occurences</th>
	</tr>
	<?php foreach ($array1dup as $key => $value) {
		echo "<tr>";
		echo "<td>".$key."</td>";
		echo "<td>".$value."</td>";
		echo "</tr>";
	}
	?>
</table>

Open in new window

0
 
LVL 1

Author Closing Comment

by:joyacv2
ID: 39992666
exactly what i need, works perfect!!!! Thanks!!!!
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39992668
Glad I could help!
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Wordpress Pagination Function Not working ? 7 39
Wordpress French and English Site 6 77
php help 34 58
Inserting results of While loop to sql server 5 17
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question