note 95203 modified in function.array-unique by danbrown
| From: | danbrown@php.net | Date: | Fri, 18 Dec 2009 15:43:07 +0000 |
| Subject: | note 95203 modified in function.array-unique by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-164112@lists.php.net to get a copy of this message | ||
I searched how to show only the duplicate elements from array, but failed.
Here is my solution:
<?php
function arrayDuplicate($array)
{
return array_unique(array_diff_assoc($array1,array_unique($array1)));
};
?>
Example:
<?php
$arr1 = array('foo', 'bar', 'xyzzy', '&',
'xyzzy',
'baz', 'bat', '|', 'xyzzy', 'plugh',
'xyzzy', 'foobar', '|', 'plonk', 'xyzzy',
'apples', '&', 'xyzzy', 'oranges', 'xyzzy',
'pears','foobar');
$result=arrayDuplicate($arr1);
print_r($result);exit;
?>
Output:
Array
(
[4] => xyzzy
[12] => |
[16] => &
[21] => foobar
)
--was--
I searched how to show only the duplicate elements from array, but failed.
Here is my solution:
<?php
function arrayDuplicate($array)
{
return array_unique(array_diff_assoc($array1,array_unique($array1)));
};
?>
Example:
<?php
$arr1 = array('foo', 'bar', 'xyzzy', '&',
'xyzzy',
'baz', 'bat', '|', 'xyzzy', 'plugh',
'xyzzy', 'foobar', '|', 'plonk', 'xyzzy',
'apples', '&', 'xyzzy', 'oranges', 'xyzzy',
'pears','foobar');
$result=arrayDuplicate($arr1);
print_r($result);exit;
?>
Output:
<?php
Array
(
[4] => xyzzy
[12] => |
[16] => &
[21] => foobar
)
See the link for more info (Bulgarian php blog): 'http://www.mpetrov.net'
http://php.net/manual/en/function.array-unique.php