php count 数组,PHP: count - Manual

这是一个关于在3维数组中查找包含最大数量值的子数组的PHP函数实现。函数通过遍历并计算每个子数组的长度,找出最大长度及其对应的键,最后返回最大子数组的详细信息。示例代码展示了如何应用该函数到一个具体的3维数组上。
摘要由CSDN通过智能技术生成

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 "

".$biggest_sub['key1']." ==> ".$biggest_sub['key2']." ==> ".$biggest_sub['max']; // displays : M ==> Seniors ==> 7
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值