note 124374 added to function.in-array

From: Date: Wed, 06 Nov 2019 15:09:34 +0000
Subject: note 124374 added to function.in-array
Groups: php.notes 
Request: Send a blank email to php-notes+get-212954@lists.php.net to get a copy of this message
<?php class Method { /** @var int current number of inMultiArray() loop */ private static $currentMultiArrayExec = 0; /** * Checks if an element is found in an array or one of its subarray. * The verification layer of this method is limited to the parameter boundary xdebug.max_nesting_level of your php.ini. * @param mixed $element * @param array $array * @param bool $strict * @return bool */ public static function inMultiArray($element, array $array, bool $strict = true) : bool { self::$currentMultiArrayExec++; if(self::$currentMultiArrayExec >= ini_get("xdebug.max_nesting_level")) return false; foreach($array as $key => $value){ $bool = $strict ? $element === $key : $element == $key; if($bool) return true; if(is_array($value)){ $bool = self::inMultiArray($element, $value, $strict); } else { $bool = $strict ? $element === $value : $element == $value; } if($bool) return true; } self::$currentMultiArrayExec = 0; return isset($bool) ? $bool : false; } } $array = array("foo" => array("foo2", "bar")); $search = "foo"; if(Method::inMultiArray($search, $array, false)){ echo $search . " it is found in the array or one of its sub array."; } else { echo $search . " was not found."; } # foo it is found in the array or one of its sub array. ---- Server IP: 208.43.231.9 Probable Submitter: 185.85.3.195 (proxied: 2a01:cb00:842b:8600:c57a:2fb1:6dab:9d64) ---- Manual Page -- http://php.net/manual/en/function.in-array.php Edit -- https://master.php.net/note/edit/124374 Del: integrated -- https://master.php.net/note/delete/124374/integrated Del: useless -- https://master.php.net/note/delete/124374/useless Del: bad code -- https://master.php.net/note/delete/124374/bad+code Del: spam -- https://master.php.net/note/delete/124374/spam Del: non-english -- https://master.php.net/note/delete/124374/non-english Del: in docs -- https://master.php.net/note/delete/124374/in+docs Del: other reasons-- https://master.php.net/note/delete/124374 Reject -- https://master.php.net/note/reject/124374 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#212954) next »