This will remove duplicate items from your array using array_unique():
$new_arr = array_unique($arr, SORT_REGULAR);
Answer from user142162 on Stack OverflowThis will remove duplicate items from your array using array_unique():
$new_arr = array_unique($arr, SORT_REGULAR);
You can simply do it using in_array()
$data = Array(
0 => Array("a", "b", "c"),
1 => Array("x", "y", "z"),
2 => Array("a", "b", "c"),
3 => Array("a", "b", "c"),
4 => Array("a", "x", "z"),
);
$final = array();
foreach ($data as $array) {
if(!in_array($array, $final)){
$final[] = $array;
}
}
which will get you something like
array(3) {
[0] => array(3) {
[0] => string(1) "a"
[1] => string(1) "b"
[2] => string(1) "c"
}
[1] => array(3) {
[0] => string(1) "x"
[1] => string(1) "y"
[2] => string(1) "z"
}
[2] => array(3) {
[0] => string(1) "a"
[1] => string(1) "x"
[2] => string(1) "z"
}
}
<?php
$r = [
['a','b'],
['a','b'],
['c','d'],
['c','d'],
['c','d'],
['e','f'],
];
foreach($r as $arr)
{
$o[implode(',', $arr)][] = 1;
}
$output = [];
array_walk($o, function($v, $k) use(&$output){
$output[] = array_merge(explode(',', $k), [count($v)]);
});
var_dump($output);
and the output:
array(3) {
[0]=>
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
int(2)
}
[1]=>
array(3) {
[0]=>
string(1) "c"
[1]=>
string(1) "d"
[2]=>
int(3)
}
[2]=>
array(3) {
[0]=>
string(1) "e"
[1]=>
string(1) "f"
[2]=>
int(1)
}
}
foreach ( $result1 as $key ):
$o[implode(', ', $key)][] = null;
foreach ($o as $key1) {
$g[implode(', ', $key)] = count($key1);
}
endforeach;
print_r($g);
You can adopt this trick; map each item of the array (which is an array itself) to its respective ['lid'] member and then use array_count_value() to do the counting for you.
array_count_values(array_map(function($item) {
return $item['lid'];
}, $arr);
Plus, it's a one-liner, thus adding to elite hacker status.
Update
Since 5.5 you can shorten it to:
array_count_values(array_column($arr, 'lid'));
foreach ($array as $value)
{
$numbers[$value[lid]]++;
}
foreach ($numbers as $key => $value)
{
echo 'numbers of '.$key.' equal '.$value.'<br/>';
}
If you are using PHP >= 5.5, you can use array_column(), in conjunction with array_count_values():
$colors = array_count_values(array_column(
materials = array_count_values(array_column($log, 1));
See demo
Or, if you're not using PHP >= 5.5, this will work in PHP 4, 5:
$colors = $materials = array();
foreach (
a){
$colors[] =
materials[] = $a[1];
}
$colors = array_count_values($colors);
$materials = array_count_values($materials);
See demo 2
Click here for sample use case that will work with either method.
I make this way:
<?php
$log = array (
array('Red', 'Steel'),
array('Red', 'Wood'),
array('Blue', 'Wood')
);
$materials = array();
$colors = array();
foreach(
line) {
$colors[$line[0]] = (!isset($colors[$line[0]])) ? 1 : $colors[$line[0]] + 1;
$materials[$line[1]] = (!isset($materials[$line[1]])) ? 1 : $materials[$line[1]] + 1;
}
?>
<h2>Colors</h2>\n
<?php
foreach ($colors as $color => $amount)
echo "{$color} => {$amount}<br>\n";
?>
<h2>Materials</h2>\n
<?php
foreach ($materials as $material => $amount)
echo "{$material} => {$amount}<br>\n";
?>
array_count_values, enjoy :-)
$array = array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
$vals = array_count_values($array);
echo 'No. of NON Duplicate Items: '.count($vals).'<br><br>';
print_r($vals);
Result:
No. of NON Duplicate Items: 7
Array
(
[12] => 1
[43] => 6
[66] => 1
[21] => 2
[56] => 1
[78] => 2
[100] => 1
)
if you want to try without 'array_count_values'
you can do with a smart way here
<?php
$input= array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
$count_values = array();
foreach ($input as $a) {
@$count_values[$a]++;
}
echo 'Duplicates count: '.count($count_values);
print_r($count_values);
?>
You can simply use
$result = [];
array_walk($arr, function($v, $k)use(&$result) {
if (isset($result[$v['link']])) {
$result[$v['link']] += $v['total'];
}else{
$result[$v['link']] = $v['total'];
}
});
print_r($result);
Demo
Try below code:
<?php
$array = array(array("link" => "http://myexample.com", "total" => 3), array("link" => "http://myexampledomain.com", "total" => 2), array("link" => "http://myexample.com", "total" => 1));
$res = array();
foreach ($array as $vals) {
if (array_key_exists($vals['link'], $res)) {
$res[$vals['link']]+=$vals['total'];
} else {
$res[$vals['link']]=$vals['total'];
}
}
print_r($res);
?>
// get count of each email
$counters = array_count_values(array_column($data, 'email'));
// collect email with counter > 1
$result = [];
foreach ($data as $item) {
if ($counters[$item['email']] > 1) {
$result[] = $item;
}
}
This one should work for you:
foreach($array as $key => $value) {
foreach ($array as $k => $v) {
if ($key < $k && $value['email'] == $v['email']) {
$result[] = array(
$value,
$v
);
}
}
}
PHPFiddle Link: http://phpfiddle.org/main/code/8trq-k2zc
Please note: this would only find you the conflicting pairs. For example:
$array = array(
array(
'email' => '[email protected]',
'name' => 'abc'
),
array(
'email' => '[email protected]',
'name' => 'def'
),
array(
'email' => '[email protected]',
'name' => 'ghi'
)
);
Would result in:
Array
(
[0] => Array
(
[0] => Array
(
[email] => [email protected]
[name] => abc
)
[1] => Array
(
[email] => [email protected]
[name] => def
)
)
[1] => Array
(
[0] => Array
(
[email] => [email protected]
[name] => abc
)
[1] => Array
(
[email] => [email protected]
[name] => ghi
)
)
[2] => Array
(
[0] => Array
(
[email] => [email protected]
[name] => def
)
[1] => Array
(
[email] => [email protected]
[name] => ghi
)
)
)
So abc conflicts with def, abc conflicts with ghi, and def conflicts with ghi.
The key problem is that an array(that with an id and a title) is not hashable.
So we can try to hash it, just join them all together, and make the result string as a hash key.
So, we maintain a hash table, which key is the object hash key, and the value is the result array index.
And then, For each item we travel, we can find the result array position by the help of the hash table, and then add the count.
Here's my code.
<?php
$array_in = array(
array('id'=>1, 'title'=>'BMW'),
array('id'=>1, 'title'=>'BMW'),
array('id'=>2, 'title'=>'Mercedes'),
);
$hash = array();
$array_out = array();
foreach($array_in as $item) {
$hash_key = $item['id'].'|'.$item['title'];
if(!array_key_exists($hash_key, $hash)) {
$hash[$hash_key] = sizeof($array_out);
array_push($array_out, array(
'id' => $item['id'],
'title' => $item['title'],
'count' => 0,
));
}
$array_out[$hash[$hash_key]]['count'] += 1;
}
var_dump($array_out);
In this sample I've used array_reduce to build a second array out of appropriate elements from your first array. Due to the way that I check for duplicate values, this assumes that your initial array is sorted (all the dupes are next to each other).
function dedupe($carry, $item) {
// check if array is non empty, and the last item matches this item
if (count($carry) > 0
&& $carry[count($carry) - 1][0] == $item[0]
&& $carry[count($carry) - 1][1] == $item[1]) {
// the array contains a match, increment counter
$carry[count($carry) - 1][2]++;
} else {
// there was no match, add element to array
$carry[] = array($item[0], $item[1], 1);
}
return $carry;
}
$cars = array(
array(1, "BMW"),
array(1, "BMW"),
array(2, "Mercedes")
);
$uniques = array_reduce($cars, "dedupe", array());
var_dump($uniques);
Solved it without fancy stuff, but it should work:
/**
* Check if value exists in array
*/
function value_exists($array, $value) {
$count = 0;
foreach($array as $v) {
if($v['Num'] == $value) {
$count++;
}
}
if ($count > 1) {
return $count;
}
}
$duplicated = array();
foreach ($Num as $key => $array ) {
if($count = value_exists($Num, $array['Num'] )) {
$duplicated[] = $array['Num'];
}
}
echo 'Duplicated: ' . count($duplicated);
As of php 5.5 (because of array_column*) you can use
<?php
$num = array (
array ( 'Num' => 2 ), array ( 'Num' => 3 ), array ( 'Num' => 13 ), array ( 'Num' => 2 ),
array ( 'Num' => 3 ), array ( 'Num' => 3 ), array ( 'Num' => 1 ), array ( 'Num' => 44 )
);
$x = array_filter(
array_count_values(
array_column($num, 'Num')
),
function($e) { return $e>1; }
);
var_export($x);
i.e.
- get all the elements 'Num' in $num as a "flat, 1-d" array.
- count the occurence of each value
- only keep count()>1 in the array
--
* in the user contributes notes for array_column there are fill-ins for php <5.5
And since you only have one element per array in $num you could also use array_map/array_shift
edit: a more verbose version without array_column
<?php
$num = array (
array ( 'Num' => 2 ), array ( 'Num' => 3 ), array ( 'Num' => 13 ), array ( 'Num' => 2 ),
array ( 'Num' => 3 ), array ( 'Num' => 3 ), array ( 'Num' => 1 ), array ( 'Num' => 44 )
);
// first remove the superfluous second dimension
$num = array_map(function($e) { return $e['Num']; }, $num); // -> $num = [ 2,3,13,2,3,3,1,44]
$num = array_count_values($num); // -> $num = [2=>2, 3=>3, 13=>1, 1=>1, 44=>1]
$num = array_filter($num, function($e) { return $e>1; }); // -> $num = [2=>2, 3=>3]
printf('There are %d duplicate values. A total of %d elements are doublets',
count($num), array_sum($num)
);
prints There are 2 duplicate values. A total of 5 elements are doublets