Recursive Array Search

Post Reply
TzzSpaceFighter
Posts: 11
Joined: Wed Jun 03, 2020 7:53 pm

Recursive Array Search

Post by TzzSpaceFighter »

Recursive Array Search

I found this really useful to use in the phpflare_contactus.php. The snip is a copy and paste from the php manual: https://www.php.net/manual/en/function. ... .php#91365

Code: Select all

By buddel
the recursive function by tony have a small bug. it failes when a key is 0

here is the corrected version of this helpful function:

<?php
function recursive_array_search($needle,$haystack) {
  foreach($haystack as $key=>$value) {
    $current_key=$key;
    if($needle===$value OR (is_array($value) && recursive_array_search($needle,$value) !== false)) {
      return $current_key;
    }
  }
  return false;
}
?>
Post Reply