?
Solved

php duplicates in an array

Posted on 2014-04-10
8
Medium Priority
?
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 35

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 35

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

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 35

Accepted Solution

by:
Dan Craciun earned 2000 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 35

Expert Comment

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

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

762 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