note 114200 added to function.array-search

From: Date: Thu, 23 Jan 2014 19:16:10 +0000
Subject: note 114200 added to function.array-search
Groups: php.notes 
Request: Send a blank email to php-notes+get-198323@lists.php.net to get a copy of this message
This class method can search in multidimensional array by multiple conditions: <?php $data = array( array('foo' => 'test4', 'bar' => 'baz'), array('foo' => 'test', 'bar' => 'baz'), array('foo' => 'test1', 'bar' => 'baz3'), array('foo' => 'test', 'bar' => 'baz'), array('foo' => 'test', 'bar' => 'baz4'), array('foo' => 'test4', 'bar' => 'baz1'), array('foo' => 'test', 'bar' => 'baz1'), array('foo' => 'test3', 'bar' => 'baz2'), array('foo' => 'test', 'bar' => 'baz'), array('foo' => 'test', 'bar' => 'baz'), array('foo' => 'test4', 'bar' => 'baz1') ); class Stdlib_Array { public static function multiSearch(array $array, array $pairs) { $found = array(); foreach ($array as $aKey => $aVal) { $coincidences = 0; foreach ($pairs as $pKey => $pVal) { if (array_key_exists($pKey, $aVal) && $aVal[$pKey] == $pVal) { $coincidences++; } } if ($coincidences == count($pairs)) { $found[$aKey] = $aVal; } } return $found; } } $result = Stdlib_Array::multiSearch($data, array('foo' => 'test4', 'bar' => 'baz1')); var_dump($result); ?> Will produce: array(2) { [5]=> array(2) { ["foo"]=> string(5) "test4" ["bar"]=> string(4) "baz1" } [10]=> array(2) { ["foo"]=> string(5) "test4" ["bar"]=> string(4) "baz1" } } ---- Server IP: 72.52.91.14 Probable Submitter: 72.52.91.14 ---- Manual Page -- http://php.net/manual/en/function.array-search.php Edit -- https://master.php.net/note/edit/114200 Del: integrated -- https://master.php.net/note/delete/114200/integrated Del: useless -- https://master.php.net/note/delete/114200/useless Del: bad code -- https://master.php.net/note/delete/114200/bad+code Del: spam -- https://master.php.net/note/delete/114200/spam Del: non-english -- https://master.php.net/note/delete/114200/non-english Del: in docs -- https://master.php.net/note/delete/114200/in+docs Del: other reasons-- https://master.php.net/note/delete/114200 Reject -- https://master.php.net/note/reject/114200 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#198323) next »