Solved

php duplicates in an array

Posted on 2014-04-10
8
273 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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

728 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