note 89519 added to function.array-unique

From: Date: Thu, 12 Mar 2009 07:47:55 +0000
Subject: note 89519 added to function.array-unique
Groups: php.notes 
Request: Send a blank email to php-notes+get-151758@lists.php.net to get a copy of this message
I noticed there was no way to tell array_unique() to ignore certain duplicated keys, so I wrote the following. I imagine there's half a dozen more efficient ways to do this, but here goes: <?php $array = array('foo', 'bar', 'xyzzy', '&', 'xyzzy', 'baz', 'bat', '|', 'xyzzy', 'plugh', 'xyzzy', 'foobar', '|', 'plonk', 'xyzzy', 'apples', '&', 'xyzzy', 'oranges', 'xyzzy', 'pears'); $ignore_values = array('|', '&'); print_r(make_unique($array, $ignore_values)); function make_unique($array, $ignore) { while($values = each($array)) { if(!in_array($values[1], $ignore)) { $dupes = array_keys($array, $values[1]); unset($dupes[0]); foreach($dupes as $rmv) { unset($array[$rmv]); } } } return $array; } ?> OUTPUT: Array ( [0] => foo [1] => bar [2] => xyzzy [3] => & [5] => baz [6] => bat [7] => | [9] => plugh [11] => foobar [12] => | [13] => plonk [15] => apples [16] => & [18] => oranges [20] => pears ) ---- Server IP: 208.69.120.35 Probable Submitter: 207.255.228.38 ---- Manual Page -- http://www.php.net/manual/en/function.array-unique.php Edit -- https://master.php.net/note/edit/89519 Del: integrated -- https://master.php.net/note/delete/89519/integrated Del: useless -- https://master.php.net/note/delete/89519/useless Del: bad code -- https://master.php.net/note/delete/89519/bad+code Del: spam -- https://master.php.net/note/delete/89519/spam Del: non-english -- https://master.php.net/note/delete/89519/non-english Del: in docs -- https://master.php.net/note/delete/89519/in+docs Del: other reasons-- https://master.php.net/note/delete/89519 Reject -- https://master.php.net/note/reject/89519 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#151758) next »