asmith Posted December 23, 2007 Share Posted December 23, 2007 can i find a max in array , and find what key it is ? and find duplicated values ? ? like : $a = array ( 1 ,4,6,3,6,1,6); can find that the max of the array is 6 , and it is located in a[2] , a[4], a[6] ?? thanks Link to comment https://forums.phpfreaks.com/topic/82928-solved-array-max-location/ Share on other sites More sharing options...
Aureole Posted December 23, 2007 Share Posted December 23, 2007 max() Link to comment https://forums.phpfreaks.com/topic/82928-solved-array-max-location/#findComment-421748 Share on other sites More sharing options...
asmith Posted December 23, 2007 Author Share Posted December 23, 2007 come again ? the output of max for that code is max($a) = 6 ; how to locate the duplicate and where is it ? Link to comment https://forums.phpfreaks.com/topic/82928-solved-array-max-location/#findComment-421755 Share on other sites More sharing options...
PHP_PhREEEk Posted December 23, 2007 Share Posted December 23, 2007 Use the result of max() as the argument for in_array to find them all... PhREEEk Link to comment https://forums.phpfreaks.com/topic/82928-solved-array-max-location/#findComment-421757 Share on other sites More sharing options...
asmith Posted December 23, 2007 Author Share Posted December 23, 2007 mm i search the php.net for in_array , found nothing special there , but i found array_search function . with array search i can find its key , but it shows the first one for duplicate values ! any idea ? <?php $a= array(1,5,7,8,8,; $b = array_search(8,$a); echo $b; ?> the code echos 3 . Link to comment https://forums.phpfreaks.com/topic/82928-solved-array-max-location/#findComment-421762 Share on other sites More sharing options...
kenrbnsn Posted December 23, 2007 Share Posted December 23, 2007 I don't believe there is a built-in function to do what you want, you can write one: <?php function get_maxs($ary) { $max = max($ary); $tmp = array(); for ($i=0;$i<count($ary);$i++) if ($ary[$i] == $max) $tmp[] = $i; return ($tmp); } $test = array(1,5,3,6,3,6,8,6,6,6); echo '<pre>' . print_r(get_maxs($test,true)) . '</pre>'; ?> Ken Link to comment https://forums.phpfreaks.com/topic/82928-solved-array-max-location/#findComment-421765 Share on other sites More sharing options...
PHP_PhREEEk Posted December 23, 2007 Share Posted December 23, 2007 Let's try a loop... <?php $a = array ( 1, 4, 6, 3, 6, 1, 6 ); echo "<pre>"; print_r($a); echo "</pre><br>"; $arrayMax = max($a); $found = array(); foreach ( $a as $key => $value ) { if ( $value == $arrayMax ) { $found[$key] = $value; } } echo 'I found the largest value was ' . $arrayMax . ', and it appeared in the following keys of $a[]:<br>'; foreach ( $found as $key => $value ) { echo $key . ' => ' . $value . '<br>'; } Output: Array ( [0] => 1 [1] => 4 [2] => 6 [3] => 3 [4] => 6 [5] => 1 [6] => 6 ) I found the largest value was 6, and it appeared in the following keys of $a[]: 2 => 6 4 => 6 6 => 6 PhREEEk Link to comment https://forums.phpfreaks.com/topic/82928-solved-array-max-location/#findComment-421772 Share on other sites More sharing options...
kenrbnsn Posted December 23, 2007 Share Posted December 23, 2007 Here's my fixed function: <?php function get_maxs($ary) { print_r($ary); $max = max($ary); $tmp = array(); for ($i=0;$i<count($ary);$i++) { if ($ary[$i] == $max) { $tmp[] = $i; echo $i . ' ... ' . $ary[$i] . ' ... ' . $max . "\n"; } } return ($tmp); } $test = array(1,5,3,6,3,6,5,6,6,6); $x = get_maxs($test); echo '<pre>' . print_r($x,true) . '</pre>'; ?> Ken Link to comment https://forums.phpfreaks.com/topic/82928-solved-array-max-location/#findComment-421773 Share on other sites More sharing options...
asmith Posted December 23, 2007 Author Share Posted December 23, 2007 thanks , it works, yo know my main problem cause from this topic : http://www.phpfreaks.com/forums/index.php/topic,173620.0.html as i couldn't solve it , i'm trying to get around it . thanks for taking time guys Link to comment https://forums.phpfreaks.com/topic/82928-solved-array-max-location/#findComment-421774 Share on other sites More sharing options...
wildteen88 Posted December 23, 2007 Share Posted December 23, 2007 PHP has a built in function for returning the keys a value is stored within the array. So need for any loops. Just two function calls max and array_keys: <?php $a = array ( 1, 4, 6, 3, 6, 1, 6 ); echo '<pre>' . print_r($a, true) . '</pre>'; $max = max($a); // get the max value in an array $keys = array_keys($a, $max); // return the keys for the max value echo 'Max value: ' . $max . "<br />\nFound in Keys:<ul>\n<li>" . implode("</li>\n<li>", $keys) . '</li></ul>'; ?> Link to comment https://forums.phpfreaks.com/topic/82928-solved-array-max-location/#findComment-421779 Share on other sites More sharing options...
asmith Posted December 23, 2007 Author Share Posted December 23, 2007 gotcha ! easiest thanks everyone , solved ! Link to comment https://forums.phpfreaks.com/topic/82928-solved-array-max-location/#findComment-421780 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.