note 88710 deleted from function.array-search by levim

From: Date: Mon, 24 Feb 2014 22:35:27 +0000
Subject: note 88710 deleted from function.array-search by levim
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-198637@lists.php.net to get a copy of this message
Note Submitter: info at [hidden] dot apanel dot nl ---- I made this function after I didnt find a recursive array_search function with a limit and the possibility to inverse the search. So here is it: <?php function search_in_array ($needle, $haystack, $inverse = false, $limit = 1) { # Settings $path = array (); $count = 0; # Check if inverse if ($inverse == true) $haystack = array_reverse ($haystack, true); # Loop foreach ($haystack as $key => $value) { # Check for return if ($count > 0 && $count == $limit) return $path; # Check for val if ($value === $needle) { # Add to path $path[] = $key; # Count $count++; } else if (is_array ($value)) { # Fetch subs $sub = search_in_array ($needle, $value, $inverse, $limit); # Check if there are subs if (count ($sub) > 0) { # Add to path $path[$key] = $sub; # Add to count $count += count ($sub); } } } return $path; } ?>

« previous php.notes (#198637) next »