View Full Version : Looking for values in an array
JasonDFR
02-09-2009, 12:09 PM
Is there a better way to do this:
$notAllowed = array('content-type:', 'mime-version', 'multipart/mixed', 'Content-Transfer-Encoding', 'to:', 'cc:', 'bcc:');
foreach ( $notAllowed as $na) {
foreach ( $_POST as $post ) {
if ( strpos($post, $na) !== false ) {
$invalid = true;
break;
}
}
}
Maybe something like array_search() ?
Thanks in advance!
J
Master_script_maker
02-09-2009, 12:22 PM
try in_array(). info here: http://us.php.net/manual/en/function.in-array.php
JasonDFR
02-09-2009, 12:32 PM
I thought of in_array, but it doesn't work.
$array = array('test', 'php', 'etc');
$string = 'I like php';
if ( in_array($string, $array) )
echo 'In array';
else
echo 'Not in array.';
The above won't find the word 'php' in $string.
Maybe a regex?
I was just trying to avoid nesting the foreach loops.
zeromadpeter
02-09-2009, 04:49 PM
in_array checks for the hole thing (like it should)
$A = "this is what im looking for";
$B = array("this","is","what","im,"looking","for");
in_array($A,$b)
if you want to check for parts you could break the string into parts and then check thous.
somthing like this
function string_in_array($string="",$combobreaker=" ",$arraylist=array())
{
$parts = explode($combobreaker, $string);
$found = false;
foreach($parts as $part)
{
if(in_array($part,$arraylist) != false)
{
$found = true;
break;
}
}
return $found;
}
returns true if found or false if not.
example
<?php
if(string_in_array("I like php"," ",array('test', 'php', 'etc')))
{
echo "found it!";
}
?>
would output "found it!"
you could also do it this way
function array_in_string($stringtest="",$arraylist=array())
{
$found = false;
foreach($arraylist as $item)
{
if(strpos($stringtest,$item) != false)
{
$found = true;
break;
}
}
return $found;
}
I did not check these but both look like they should work
not sure what way is faster thou.
function array_in_string($stringtest="",$arraylist=array())
{
$found = false;
foreach($arraylist as $item)
{
if(strpos($stringtest,$item) != false)
{
$found = true;
break;
}
}
return $found;
}I think this is kind of backwards from what was required... it's also hugely redundant. Don't set defaults for necessary parameters, and since we're returning immediately when we find a value anyway, there's no point in the separate variable to return.
// Doesn't really matter which order these parameters are in.
// The PHP folks don't seem to care much for consistency.
function substr_in_array($needle, $haystack) {
foreach ($haystack as $val)
if (strpos($val, $needle) !== false)
return true;
return false;
}It would be nice to do it Haskell-style, any (needle `isInfixOf`) haystack but I can't seem to find any way to lazily terminate array_filter(). We could do it manually, I guess:
function id($a) {
return $a;
}
function array_any($haystack, $predicate = 'id') {
foreach ($haystack as $val)
if ($predicate($val))
return true;
return false;
}
function substr_in_array($needle, $haystack) {
$pred = create_function('$v',
sprintf('return strpos($v, \'%s\') !== false;',
addslashes($needle)));
return array_any($haystack, $pred);
}It's... rather a bit clumsier, but what can one expect from PHP? :)
JasonDFR
02-10-2009, 07:19 AM
function substr_in_array($needle, $haystack) {
foreach ($haystack as $val)
if (strpos($val, $needle) !== false)
return true;
return false;
}
I like this Twey, but what if you have 10 needles to check for, or better, an array of 10 needles?
In this case would you put the substr_in_array() function inside a foreach loop of the needles array? Check out my first post. Thanks.
djr33
02-10-2009, 08:27 AM
I think one of these functions, or one of their neighbors on php.net should be helpful here, depending on how you apply the logic:
http://www.php.net/manual/en/function.array-map.php
http://www.php.net/manual/en/function.array-walk.php
For example:
$x = 0;
function in_array2($item,$string) {
global $string;
if (strpos($string,$item)!==FALSE) {
global $x;
$x = 1;
}
array_walk('in_array2',$array);
echo $x;
Now that I look at it, there are several odd issues with this, thoughI think that may work.
Anyway, the functions seem useful.
This may already be solved, though.
Well, it should theoretically be easy enough to modify it (the second version) to do that. However, in PHP we end up with something like:
function substr_in_array($needle, $haystack) {
if (!is_array($needle))
$needle = array($needle);
$haystack = addslashes(serialize($haystack));
return array_any($needle, create_function('$a',
"return array_any(unserialize('$haystack'), create_function('\$b',
\"return strpos(\\\$b, \$a) !== false;\"
));"
));
}... so I think we're just going to stick with for loops...
function substr_in_array($needle, $haystack) {
if (!is_array($needle))
$needle = array($needle);
foreach ($needle as $a)
foreach ($haystack as $b)
if (strpos($b, $a) !== false)
return true;
return false;
}The nice folks in ##php assure me that soon we'll be able to do:
function substr_in_array($needle, $haystack) {
if (!is_array($needle))
$needle = array($needle);
return array_any($needle, function($a) using ($haystack) {
return array_any($haystack, function($b) using ($a) {
return strpos($b, $a) !== false;
});
});
}This is still a far cry from the Haskell substr_in_array needle haystack = any (any (`isInfixOf` haystack)) needle but beggars can't be choosers, eh?
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.