News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

arrays against array

Started by Anguz, August 27, 2003, 10:58:13 PM

Previous topic - Next topic

Anguz

if I want to compare all the values in two arrays, can I do it like this?

$var = ($array1 == $array2 ? $array3 : '');

or do I have to do it like this?

for ($i = '0'; $i < count($array1); $i++)
{
   $var = ($array1[$i] == $array2[$i] ? $array3[$i] : '');
}


note that only one of the comparisons will return true

thanks for your help :)
Cristián Lávaque http://cristianlavaque.com

Anguz

#1
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?

for ($i = '0'; $i < count($array1); $i++)
{
   if ($array1[$i] == $array2[$i])
   {
      $var = $array3[$i];
      break;
   }
   else
   {
      $var = '';
   }
}
Cristián Lávaque http://cristianlavaque.com

[Unknown]

$worked = true;
foreach($array1 as $a)
   if (!in_array($a, $array2))
   {
      $worked = false;
      break;
   }


-[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
Cristián Lávaque http://cristianlavaque.com

Advertisement: