Solved

php duplicates in an array

Posted on 2014-04-10
8
272 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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 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 35

Expert Comment

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

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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.

740 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