如何从阵列行的最小值 [英] How to get the minimum value from array row
本文介绍了如何从阵列行的最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图从任何列得到的最小值包含在列名XX。
下面是我的code:
< PHP
$阵列=阵列(
阵列(
'ID'=> 1,
'10XX'=> 14,
'11XX'=> 32,
'12XX'=> 4
) 阵列(
'ID'=> 2,
'10XX'=> 13,
'11XX'=> 36,
'12XX'=> 41
)
);的foreach($数组作为$项)
{
$ lowestKey ='';
的foreach($项目作为重点$ = GT; $值)
{
如果(strpos($键,'XX')=== 0)
{ 如果($ lowestKey =='')
{
$ lowestKey = $关键;
}
其他
{
如果($值< $项目[$ lowestKey])
{
$ lowestKey = $关键;
}
}
}
} 回声最低ID。 $项目['身份证']。 ':'。 $项目[$ lowestKey。 \\ n;
}
?>
解决方案
函数_getNumber($数组){
返回数组$ ['身份证'];
}
$数= array_map('_ getNumber',$阵列);
或
$号= array_map(函数($数组){
返回数组$ ['身份证'];
},$阵列);回声$分钟= MIN($数);
回声$最大值= MAX($数);
I trying to get the minimum values from the any column contains "xx" in the column name.
Below is my code:
<?php
$array = array(
array(
'id' => 1,
'10xx' => 14,
'11xx' => 32,
'12xx' => 4
),
array(
'id' => 2,
'10xx' => 13,
'11xx' => 36,
'12xx' => 41
)
);
foreach($array as $item)
{
$lowestKey = '';
foreach($item as $key => $value)
{
if(strpos($key, 'xx') === 0)
{
if($lowestKey == '')
{
$lowestKey = $key;
}
else
{
if($value < $item[$lowestKey])
{
$lowestKey = $key;
}
}
}
}
echo 'lowest for id ' . $item['id'] . ': ' . $item[$lowestKey] . "\n";
}
?>
解决方案
function _getNumber($array) {
return $array['id'];
}
$numbers = array_map('_getNumber', $array);
OR
$numbers = array_map(function($array) {
return $array['id'];
}, $array);
echo $min = min($numbers);
echo $max = max($numbers);
这篇关于如何从阵列行的最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文