Zojak_Quaguz Posted December 21, 2010 Share Posted December 21, 2010 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. Link to comment https://forums.phpfreaks.com/topic/222256-fastest-way-to-loop-through-two-arrays-compare-elements-and-print-results/ Share on other sites More sharing options...
topcat Posted December 21, 2010 Share Posted December 21, 2010 $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. Link to comment https://forums.phpfreaks.com/topic/222256-fastest-way-to-loop-through-two-arrays-compare-elements-and-print-results/#findComment-1149715 Share on other sites More sharing options...
Andy-H Posted December 21, 2010 Share Posted December 21, 2010 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"; } } } Link to comment https://forums.phpfreaks.com/topic/222256-fastest-way-to-loop-through-two-arrays-compare-elements-and-print-results/#findComment-1149718 Share on other sites More sharing options...
Zojak_Quaguz Posted December 21, 2010 Author Share Posted December 21, 2010 Thanks! Link to comment https://forums.phpfreaks.com/topic/222256-fastest-way-to-loop-through-two-arrays-compare-elements-and-print-results/#findComment-1149748 Share on other sites More sharing options...
laffin Posted December 21, 2010 Share Posted December 21, 2010 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]; Link to comment https://forums.phpfreaks.com/topic/222256-fastest-way-to-loop-through-two-arrays-compare-elements-and-print-results/#findComment-1149750 Share on other sites More sharing options...
topcat Posted December 21, 2010 Share Posted December 21, 2010 Hey Laffin, good call. Looks like I was 'typing out loud' Link to comment https://forums.phpfreaks.com/topic/222256-fastest-way-to-loop-through-two-arrays-compare-elements-and-print-results/#findComment-1149796 Share on other sites More sharing options...
Zojak_Quaguz Posted December 21, 2010 Author Share Posted December 21, 2010 Yeah, topcat's code is what I used as the basis for mine. And thanks, laffin. Link to comment https://forums.phpfreaks.com/topic/222256-fastest-way-to-loop-through-two-arrays-compare-elements-and-print-results/#findComment-1149890 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.