Solved

php duplicates in an array

Posted on 2014-04-10
8
260 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
Comment Utility
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
Comment Utility
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
Comment Utility
Hi,

I need the data in a table. Any idea?
0
 
LVL 34

Expert Comment

by:Dan Craciun
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:joyacv2
Comment Utility
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
Comment Utility
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
Comment Utility
exactly what i need, works perfect!!!! Thanks!!!!
0
 
LVL 34

Expert Comment

by:Dan Craciun
Comment Utility
Glad I could help!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now