note 114230 added to function.array-search

From: Date: Mon, 27 Jan 2014 14:42:01 +0000
Subject: note 114230 added to function.array-search
Groups: php.notes 
Request: Send a blank email to php-notes+get-198361@lists.php.net to get a copy of this message
If you need to perform a search inside an array looking for a regex pattern, you can try it <?php function array_preg_search($needle, $haystack) { if (!is_scalar($needle)) { return null; } if (!is_array($haystack)) { return null; } foreach ($haystack as $key => $val) { if ( preg_match($needle, $val) ) { return $key; } } return false; } ?> Use: <?php $array = Array ( 'Connected to database', 'Email sent to &lt;&gt; williamprogphp at yahoo dot com dot br with 2 attachment(s).', 'Disconnected to database', '{"status":"OK","sentId":31077}', ); echo array_preg_search('/OK/', $array); ?> will output the key associated with. 3 Cheers ---- Server IP: 192.41.45.150 Probable Submitter: 177.43.119.65 ---- Manual Page -- http://php.net/manual/en/function.array-search.php Edit -- https://master.php.net/note/edit/114230 Del: integrated -- https://master.php.net/note/delete/114230/integrated Del: useless -- https://master.php.net/note/delete/114230/useless Del: bad code -- https://master.php.net/note/delete/114230/bad+code Del: spam -- https://master.php.net/note/delete/114230/spam Del: non-english -- https://master.php.net/note/delete/114230/non-english Del: in docs -- https://master.php.net/note/delete/114230/in+docs Del: other reasons-- https://master.php.net/note/delete/114230 Reject -- https://master.php.net/note/reject/114230 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#198361) next »