note 124666 added to function.count

From: Date: Thu, 30 Jan 2020 22:59:01 +0000
Subject: note 124666 added to function.count
Groups: php.notes 
Request: Send a blank email to php-notes+get-213980@lists.php.net to get a copy of this message
If you want to know the sub-array containing the MAX NUMBER of values in a 3 dimensions array, here is a try (maybe not the nicest way, but it works): function how_big_is_the_biggest_sub ($array) { // we parse the 1st level foreach ($array AS $key => $array_lvl2) { //within level 2, we count the 3d levels max $lvl2_nb = array_map( 'count', $array_lvl2) ; $max_nb = max($lvl2_nb); // we store the matching keys, it might be usefull $max_key = array_search($max_nb, $lvl2_nb); $max_nb_all[$max_key.'|'.$key] = $max_nb; } // now we want the max from all levels 2, so one more time $real_max = max($max_nb_all); $real_max_key = array_search($real_max, $max_nb_all); list($real_max_key2, $real_max_key1) = explode('|', $real_max_key); // preparing result $biggest_sub['max'] = $real_max; $biggest_sub['key1'] = $real_max_key1; $biggest_sub['key2'] = $real_max_key2; return $biggest_sub; } /* $cat_poids_max['M']['Juniors'][] = 55; $cat_poids_max['M']['Juniors'][] = 61; $cat_poids_max['M']['Juniors'][] = 68; $cat_poids_max['M']['Juniors'][] = 76; $cat_poids_max['M']['Juniors'][] = 100; $cat_poids_max['M']['Seniors'][] = 55; $cat_poids_max['M']['Seniors'][] = 60; $cat_poids_max['M']['Seniors'][] = 67; $cat_poids_max['M']['Seniors'][] = 75; $cat_poids_max['M']['Seniors'][] = 84; $cat_poids_max['M']['Seniors'][] = 90; $cat_poids_max['M']['Seniors'][] = 100; //.... $cat_poids_max['F']['Juniors'][] = 52; $cat_poids_max['F']['Juniors'][] = 65; $cat_poids_max['F']['Juniors'][] = 74; $cat_poids_max['F']['Juniors'][] = 100; $cat_poids_max['F']['Seniors'][] = 62; $cat_poids_max['F']['Seniors'][] = 67; $cat_poids_max['F']['Seniors'][] = 78; $cat_poids_max['F']['Seniors'][] = 86; $cat_poids_max['F']['Seniors'][] = 100; */ $biggest_sub = how_big_is_the_biggest_sub($cat_poids_max); echo "<li> ".$biggest_sub['key1']." ==> ".$biggest_sub['key2']." ==> ".$biggest_sub['max']; // displays : M ==> Seniors ==> 7 ---- Server IP: 45.112.84.4 Probable Submitter: 185.85.3.172 (proxied: 77.140.253.10) ---- Manual Page -- http://php.net/manual/en/function.count.php Edit -- https://master.php.net/note/edit/124666 Del: integrated -- https://master.php.net/note/delete/124666/integrated Del: useless -- https://master.php.net/note/delete/124666/useless Del: bad code -- https://master.php.net/note/delete/124666/bad+code Del: spam -- https://master.php.net/note/delete/124666/spam Del: non-english -- https://master.php.net/note/delete/124666/non-english Del: in docs -- https://master.php.net/note/delete/124666/in+docs Del: other reasons-- https://master.php.net/note/delete/124666 Reject -- https://master.php.net/note/reject/124666 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#213980) next »