Jump to content

Creating an array


sKunKbad

Recommended Posts

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>";

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.