Jump to content

Fastest way to loop through two arrays, compare elements, and print results?


Zojak_Quaguz

Recommended Posts

I have two arrays, X and Y, which contain names. The arrays are of the same size. The arrays are related to each other such that for each pair

 

X[n] , Y[n]

 

X is friends with Y.

 

For example:

X[0,1,2] = [Link, Cloud, Cloud, Mario, Mario, Luigi]

Y[0,1,2] = [Zelda, Barrett, Tifa, Luigi, Bowser, Mario]

Link is friends with Zelda

Cloud is friends with Barrett and Tifa

Mario is friends with Luigi and Bowser

Luigi is friends with Mario

 

I want to loop through these arrays and, for each unique name, find all of that person's friends.

 

I then want to print the results to a text file, like so:

 

Link, Zelda

Cloud, Barrett, Tifa

Mario, Luigi

Luigi, Mario

 

I know how to do this theoretically, but I just need help with the PHP syntax. Thanks very much.

 

$x =array('a','b','b','c','d','d');
$y=array('1','2','3','4','5','6');
$z = array();
$i=0;
for($i;$i<count($x);$i++){
    if(!array_key_exists($x[$i], $z)){
        $z[$x[$i]]= array($y[$i]);
    }else{
        $z[$x[$i]][] = $y[$i];
    }
}
var_dump($z);

   

Will give you an associative array of each name in array x as an array key with their friends as the corresponding array  so you can just loop through and implode and echo as you wish.

I have two arrays, X and Y, which contain names. The arrays are of the same size. The arrays are related to each other such that for each pair

 

X[n] , Y[n]

 

X is friends with Y.

 

For example:

X[0,1,2] = [Link, Cloud, Cloud, Mario, Mario, Luigi]

Y[0,1,2] = [Zelda, Barrett, Tifa, Luigi, Bowser, Mario]

Link is friends with Zelda

Cloud is friends with Barrett and Tifa

Mario is friends with Luigi and Bowser

Luigi is friends with Mario

 

I want to loop through these arrays and, for each unique name, find all of that person's friends.

 

I then want to print the results to a text file, like so:

 

Link, Zelda

Cloud, Barrett, Tifa

Mario, Luigi

Luigi, Mario

 

I know how to do this theoretically, but I just need help with the PHP syntax. Thanks very much.

 

Since PHP wasn't made with knowledge of the existance of FF7, Zelda or Mario you're going to need a 3rd array.

 

$x     = array('Link', 'Cloud', 'Cloud', 'Mario', 'Mario', 'Luigi');
$y     = array([b]'Zelda', 'Barrett', 'Tifa', 'Luigi', 'Bowser', 'Mario');

$games = array(
                'FF7'   => array('Cloud', 'Barrett', 'Tifa', 'Cid', 'Red XIII'),
                'Mario' => array('Mario', 'Luigi', 'Bowser', 'Yoshi'),
                'Zelda' => array('Link', 'Zelda')
              );

$x = array_unique($x);
$n = '';
foreach($x as $v)
{
   foreach($games as $k => $val)
   {
      if ( in_array($v, $val) )
      {
         $n = $k;
         break;
      }
   }
   foreach($y as $val)
   {
      if ( in_array($val, $games[$n])
      {
         echo $v . ' is friends with ' . $val . '.<br >' . "\r\n";
      }
   }
}

 

Without any actual code, your gonna get 100 different ways ppl think it should be done

 

X[0,1,2] = [Link, Cloud, Cloud, Mario, Mario, Luigi]
Y[0,1,2] = [Zelda, Barrett, Tifa, Luigi, Bowser, Mario]

 

Topcats code is closer to what I think u want, andy-h's code strips out the 1 - 1 match you have going.

in topcats' code u can replace this block

    if(!array_key_exists($x[$i], $z)){
        $z[$x[$i]]= array($y[$i]);
    }else{
        $z[$x[$i]][] = $y[$i];
    }

with just this

        $z[$x[$i]][] = $y[$i];

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.