note 20594 modified in function.in-array by danbrown

From: Date: Mon, 08 Jun 2009 21:48:09 +0000
Subject: note 20594 modified in function.in-array by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-156533@lists.php.net to get a copy of this message
If you want to search a multiple array for a value - you can use this function - which looks up the value in any of the arrays dimensions (like in_array() does in the first dimension). Note that the speed is growing proportional with the size of the array - why in_array is best if you can determine where to look for the value. Copy & paste this into your code... <?php function in_multi_array($needle, $haystack) { $in_multi_array = false; if(in_array($needle, $haystack)) { $in_multi_array = true; } else { for($i = 0; $i < sizeof($haystack); $i++) { if(is_array($haystack[$i])) { if(in_multi_array($needle, $haystack[$i])) { $in_multi_array = true; break; } } } } return $in_multi_array; } ?> --was-- If you want to search a multiple array for a value - you can use this function - which looks up the value in any of the arrays dimensions (like in_array() does in the first dimension). Note that the speed is growing proportional with the size of the array - why in_array is best if you can determine where to look for the value. Copy & paste this into your code... function in_multi_array($needle, $haystack) { $in_multi_array = false; if(in_array($needle, $haystack)) { $in_multi_array = true; } else { for($i = 0; $i < sizeof($haystack); $i++) { if(is_array($haystack[$i])) { if(in_multi_array($needle, $haystack[$i])) { $in_multi_array = true; break; } } } } return $in_multi_array; } http://php.net/manual/en/function.in-array.php

« previous php.notes (#156533) next »