note 95203 modified in function.array-unique by sobak

From: Date: Sun, 11 Dec 2016 06:51:10 +0000
Subject: note 95203 modified in function.array-unique by sobak
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-208477@lists.php.net to get a copy of this message
I searched how to show only the de-duplicate elements from array, but failed. Here is my solution: <?php function arrayUniqueElements($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=arrayUniqueElements($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: Array ( [4] => xyzzy [12] => | [16] => & [21] => foobar ) http://php.net/manual/en/function.array-unique.php

« previous php.notes (#208477) next »