sKunKbad Posted December 24, 2007 Share Posted December 24, 2007 I like to try to solve people's questions here, and I'm actually working on a solution for another thread. On line 43 starts two foreach loops that should eventually add people with matched values to an array called $confirmed. The people and their values are stored in an array at the top of the script. The guy I'm helping wants his script to show people who have matching values. Why isn't my $confirmed array loading up with arrays of matches? <?php /* Each person should be judged by key 1 first. The person with the highest value wins *If there is a tie, then key 2 is checked, and the person with the highest value wins. *If there is still a tie, key3 is checked, and the person with the LOWEST value wins. */ $group = $savedGroup = array( 1 => array ( 0 => 'john' , 1 => 6 , 2 => 5 , 3 => 3 ) , 2 => array ( 0 => 'pat' , 1 => 6 , 2 => 5 , 3 => 3 ) , 3 => array ( 0 => 'mat' , 1 => 5 , 2 => 5 , 3 => 9 ) , 4 => array ( 0 => 'pink' , 1 => 5 , 2 => 5 , 3 => 9 ) , 5 => array ( 0 => 'joey' , 1 => 5 , 2 => 5 , 3 => 9 ) ) ; function sort_keys ($x, $y) { if ($x['1']>$y['1']) return -1; else if ($x['1']<$y['1']) return 1; if ($x['2']>$y['2']) return -1; else if ($x['2']<$y['2']) return 1; if ($x['3']<$y['3']) return -1; else if ($x['3']>$y['3']) return 1; return 0; } uasort ($group, 'sort_keys'); echo '<h3>Who is best?</h3><pre>' . print_r($group, 1) . '</pre>'; $winner = array_shift($group); echo "<h3>The winner is {$winner['0']}</h3>"; /* *Now check if there are people that have the same values */ $confirmed = array(0=>array()); foreach ($savedGroup as $member){ foreach ( $savedGroup as $otherMember ){ if ( $otherMember != $member ) { for ($k=1; $k<=3; $k++){ if ($member["$k"] == $otherMember["$k"]){ //the initial match of stat values $match["$member[0]"]["$otherMember[0]"]["$k"] = 'yes'; //add match confirmation to array if (count($match["$member[0]"]["$otherMember[0]"])== '3'){ //check that all stats match foreach($confirmed as $possible){ //<<---------- LINE 43 foreach($possible as $matched){ if(!in_array( $otherMember[0], $matched )){ static $matchGroup = 0; $confirmed["$matchGroup"][] = $member[0]; $confirmed["$matchGroup"][] = $otherMember[0]; $matchGroup++; }else{ //add name as key to existing $confirmed array $matched[] = $member[0]; } } } } } } } } } echo "<pre>"; print_r($match); print_r($confirmed); echo "</pre>"; Quote Link to comment https://forums.phpfreaks.com/topic/83006-creating-an-array/ Share on other sites More sharing options...
sKunKbad Posted December 24, 2007 Author Share Posted December 24, 2007 I think I'm getting a lot closer to having this done, but getting burnt out. Any help is appreciated. Here is the modification that I have, and it's almost working right: for ($m=0; $m<=count($confirmed)-1; $m++){ if(in_array( $otherMember[0], $confirmed["$m"] ) || in_array( $member[0], $confirmed["$m"] )){ $found = $found +1; } } if( $found == 0 ){ //if no previous matches, make new $confirmed array static $matchGroup = 0; $confirmed["$matchGroup"][] = $member[0]; $confirmed["$matchGroup"][] = $otherMember[0]; $matchGroup++; }else{ //add name as key to existing $confirmed array foreach ($confirmed as $z){ if(in_array( $otherMember[0], $z )){ $z[] = $member[0]; $z[] = $otherMember[0]; } } $found = 0; } Quote Link to comment https://forums.phpfreaks.com/topic/83006-creating-an-array/#findComment-422227 Share on other sites More sharing options...
sKunKbad Posted December 24, 2007 Author Share Posted December 24, 2007 I posted the finished code at the original thread Quote Link to comment https://forums.phpfreaks.com/topic/83006-creating-an-array/#findComment-422364 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.