asmith Posted December 23, 2007 Share Posted December 23, 2007 $group = Array ( [1] => Array ( [0] => john [1] => 4 [2] => 5 [3] => 6 ) [2] => Array ( [0] => pat [1] => 5 [2] => 2 [3] => 7 ) [3] => Array ( [0] => mat [1] => 4 [2] => 5 [3] => 4 ) [4] => Array ( [0] => pink [1] => 2 [2] => 2 [3] => 3 ) ) ; as you see in the code there 's a group array. which contain users. each user has 3 values. the first 2 value of a user (e.g $group[1][1] and $group[1][2]) is about his activity rank . 1 more important than 2 . the third value is his warnings for breaking site rules. i want to choose 2 users from the list which has the most activity and less warnings. but the most things matters is the activty , if the value in 1 was equal then i should check the value in 2 . if the value in 2 were equal too , then check for less warnings ( in 3 ) . i'm getting confused at this, any idea ? (the answer for the above array would be 1st pat , and 2nd mat . 1st for 5 in the 1 section, mat and john are equal in 1 and 2 . but mat has less warning in section 3 , so mat will be chosen . thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/82904-array-choose-confussion/ Share on other sites More sharing options...
sKunKbad Posted December 23, 2007 Share Posted December 23, 2007 Give this a try: <?php $group = array( 1 => array ( 0 => 'john' , 1 => 4 , 2 => 5 , 3 => 6 ) , 2 => array ( 0 => 'pat' , 1 => 5 , 2 => 2 , 3 => 7 ) , 3 => array ( 0 => 'mat' , 1 => 4 , 2 => 6 , 3 => 6 ) , 4 => array ( 0 => 'pink' , 1 => 2 , 2 => 2 , 3 => 10 ) ) ; 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/82904-array-choose-confussion/#findComment-421638 Share on other sites More sharing options...
asmith Posted December 23, 2007 Author Share Posted December 23, 2007 thanks a lot . it is working fine although i don't know the meaning of return 1 and -1 . it sorts the array. the key of the sub arrays , hasn't change, how can i choose them ? how can i pull out just the name ? edit : i reviewed all but i'm not understanding this :'( return and x y is killing me! how did x y applied for all 4 sub arrays ? :'( Quote Link to comment https://forums.phpfreaks.com/topic/82904-array-choose-confussion/#findComment-421641 Share on other sites More sharing options...
sKunKbad Posted December 23, 2007 Share Posted December 23, 2007 $winner = array_shift($group); echo "<h3>The winner is {$winner['0']}</h3>"; This uasort function is not easily understood by beginners ( and I still consider myself one!). I learned it from a book called "PHP5 Advanced" by Larry Ulman. The uasort function takes a user defined function, which sorts according to the rules set within. $x and $y are simply variables set to the keys that are being sorted at that particular instance. You won't be disappointed with the book I mentioned. Go ahead and buy it! Quote Link to comment https://forums.phpfreaks.com/topic/82904-array-choose-confussion/#findComment-421646 Share on other sites More sharing options...
asmith Posted December 23, 2007 Author Share Posted December 23, 2007 thanks . the php.net has said about return false or true, i thought return 1 you meant return true , then return -1 should be 0 ! what does this return do ?? (i'm so embaressed i'm didn't understand it 100% , and questions pops up ! :/ can i tell me what to change , i want to make it if there were 3 or more persons who was equal in everything ,then do stuff ? thanks) Quote Link to comment https://forums.phpfreaks.com/topic/82904-array-choose-confussion/#findComment-421650 Share on other sites More sharing options...
asmith Posted December 23, 2007 Author Share Posted December 23, 2007 *bump* Quote Link to comment https://forums.phpfreaks.com/topic/82904-array-choose-confussion/#findComment-421678 Share on other sites More sharing options...
asmith Posted December 23, 2007 Author Share Posted December 23, 2007 anyone out there could help me ? i'm stucked! how can i find if more than 2 persons are equal in everything ? Quote Link to comment https://forums.phpfreaks.com/topic/82904-array-choose-confussion/#findComment-421710 Share on other sites More sharing options...
sKunKbad Posted December 23, 2007 Share Posted December 23, 2007 Maybe somebody else can help with this. This is what I have so far: <?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 = array( 1 => array ( 0 => 'john' , 1 => 4 , 2 => 5 , 3 => 7 ) , 2 => array ( 0 => 'pat' , 1 => 4 , 2 => 5 , 3 => 7 ) , 3 => array ( 0 => 'mat' , 1 => 4 , 2 => 5 , 3 => 7 ) , 4 => array ( 0 => 'pink' , 1 => 4 , 2 => 5 , 3 => 7 ) ) ; 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>'; $group2 = $group; $winner = array_shift($group); echo "<h3>The winner is {$winner['0']}</h3>"; /* *Now check if there are three or more people that have the same values */ $count = count($group2) - 2; for ($i=1; $i <= $count; $i++){ $a = $i; $b = $i + 1; $c = $i + 2; $three = array_intersect_assoc( $group2["$a"] , $group2["$b"] , $group2["$c"] ); if (array_key_exists('1', $three) && array_key_exists('2', $three) && array_key_exists('3', $three)){ $nameOne = $group2["$a"]['0']; $nameTwo = $group2["$b"]['0']; $nameThree = $group2["$c"]['0']; echo "All of the value for $nameOne, $nameTwo, and $nameThree are the same<br>"; echo "<pre>"; print_r($three); echo "</pre>"; } } ?> I just want to have the output fixed so that it says "john, pat, mat, and pink all have the same values" instead of how it outputs now. Quote Link to comment https://forums.phpfreaks.com/topic/82904-array-choose-confussion/#findComment-421850 Share on other sites More sharing options...
sKunKbad Posted December 24, 2007 Share Posted December 24, 2007 OK man, I've got done what you asked for. If you need anything else... buy some books. It's Christmas and I ain't gonna be around for a couple days. <?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 => 9 , 2 => 5 , 3 => 3 ) , 4 => array ( 0 => 'pink' , 1 => 5 , 2 => 5 , 3 => 3 ) , 5 => array ( 0 => 'joey' , 1 => 5 , 2 => 5 , 3 => 3 ) ) ; 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 of single value to array if (count($match["$member[0]"]["$otherMember[0]"])== '3'){ //check that all values are matches, indicating a true match 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 for($z=0; $z<=count($confirmed)-1; $z++){ if(in_array( "$otherMember[0]", $confirmed["$z"] ) || in_array( "$member[0]", $confirmed["$z"] )){ $confirmed["$z"][] = $member[0]; $confirmed["$z"][] = $otherMember[0]; } } $found = 0; } } } } } } } for($g=0; $g<=count($confirmed)-1; $g++){ $confirmed["$g"] = array_unique($confirmed["$g"]); } $new = array(); for($b=0; $b<=count($confirmed)-1; $b++){ foreach ($confirmed["$b"] as $key => $val){ $new["$b"][] = $val; } } for($r=0; $r<=count($new)-1; $r++){ echo "<h3>The following members had an instance of complete duplicate stats:</h3>"; echo "<div style='width:200px; background-color:yellow;'>"; for($t=0; $t<=count($new["$r"])-1; $t++){ echo $new["$r"]["$t"] . "<br>"; } echo "</div>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/82904-array-choose-confussion/#findComment-422265 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.