Simple Machines Community Forum

General Community => Scripting Help => Topic started by: margarett on October 23, 2007, 12:02:51 PM

Title: Search inside array?
Post by: margarett on October 23, 2007, 12:02:51 PM
Hi all...

Imagine if I have this code:

if (($board = 1) && ($board = 2) && ($board = 3) && ($board = 4) && ($board = 5) && ($board = 6) && ($board = 7))


Can I create an array like:

$myarray = array(1, 2, 3, 4, 5, 6, 7);


And replace all those "board=xx" && "board=yy" for a "search function inside my array?

Thanks!
Title: Re: Search inside array?
Post by: codenaught on October 23, 2007, 07:27:14 PM
Well that code doesn't really make sense because $board is only one value, in SMF at least. But perhaps you meant ||.

You can do something like:

$myarray = array(1, 2, 3, 4, 5, 6, 7);
if (in_array($board, $myarray))
{
   <do something when $board has its value inside of $myarray>
}

If comparing two arrays...

$arr1 = array(1, 2, 3, 4);
$arr2 = array(1, 5, 7, 9);

// Only need one value to be in both?
if (array_intersect($arr1, $arr2))
    <do something>;

// Need $x values to be in both?
if (count(array_intersect($arr1, $arr2)) >= $x)
    <do something>;