note 85109 added to function.array-unique

From: Date: Thu, 14 Aug 2008 21:39:39 +0000
Subject: note 85109 added to function.array-unique
Groups: php.notes 
Request: Send a blank email to php-notes+get-143508@lists.php.net to get a copy of this message
Here's another solution for returning an array that only includes repeated values. There is one given below but it only works on numerically indexed arrays; this one is more comprehensive since I used the foreach iterator. Also, this one preserves keys--in that the returned result contains a distinct list of repeats (storing only the first instance it encounters of each duplicate value). <?php function array_repeated($array) { if ( !is_array($array) ) { return false; } $duplicates = array(); foreach ( $array as $key => $val ) { end($array); $k = key($array); $v = current($array); while ( $k !== $key ) { if ( $v === $val ) { $duplicates[$key] = $v; } $v = prev($array); $k = key($array); } } return $duplicates; } ?> ---- Server IP: 64.71.164.2 Probable Submitter: 66.170.10.66 ---- Manual Page -- http://www.php.net/manual/en/function.array-unique.php Edit -- https://master.php.net/note/edit/85109 Del: integrated -- https://master.php.net/note/delete/85109/integrated Del: useless -- https://master.php.net/note/delete/85109/useless Del: bad code -- https://master.php.net/note/delete/85109/bad+code Del: spam -- https://master.php.net/note/delete/85109/spam Del: non-english -- https://master.php.net/note/delete/85109/non-english Del: in docs -- https://master.php.net/note/delete/85109/in+docs Del: other reasons-- https://master.php.net/note/delete/85109 Reject -- https://master.php.net/note/reject/85109 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#143508) next »