General Community > Scripting Help
arrays against array
(1/1)
Anguz:
if I want to compare all the values in two arrays, can I do it like this?
--- Code: ---$var = ($array1 == $array2 ? $array3 : '');
--- End code ---
or do I have to do it like this?
--- Code: ---for ($i = '0'; $i < count($array1); $i++)
{
$var = ($array1[$i] == $array2[$i] ? $array3[$i] : '');
}
--- End code ---
note that only one of the comparisons will return true
thanks for your help :)
Anguz:
hmm maybe I should escape the loop once there's a match, or in the next loop, $var will be empty again...
then maybe something like this?
--- Code: ---for ($i = '0'; $i < count($array1); $i++)
{
if ($array1[$i] == $array2[$i])
{
$var = $array3[$i];
break;
}
else
{
$var = '';
}
}
--- End code ---
[Unknown]:
--- Code: ---$worked = true;
foreach($array1 as $a)
if (!in_array($a, $array2))
{
$worked = false;
break;
}
--- End code ---
-[Unknown]
Anguz:
hmm... I see... not exactly what I was asking, but you gave me an idea
I just looked up in_array() and also found array_search()
I can do with those something I've been wondering how to
thank you! :D
Navigation
[0] Message Index
Go to full version