我有一个这样的数组:
array (0 =>
array (
'id' => '20110209172713',
'Date' => '2011-02-09',
'Weight' => '200',
),
1 =>
array (
'id' => '20110209172747',
'Date' => '2011-02-09',
'Weight' => '180',
),
2 =>
array (
'id' => '20110209172827',
'Date' => '2011-02-09',
'Weight' => '175',
),
3 =>
array (
'id' => '20110211204433',
'Date' => '2011-02-11',
'Weight' => '195',
),
)
我需要提取最小和最大权重值。 在这个例子中
$ min_value = 175
$ max_value = 200
有关如何执行此操作的任何帮助? 谢谢!
答案 0 :(得分:85)
选项1.首先映射数组以获取这些数字(而不是完整的详细信息):
$numbers = array_column($array, 'weight')
然后你得到最小和最大:
$min = min($numbers);
$max = max($numbers);
选项2.(仅当您没有PHP 5.5时。)与选项1相同,但要采用值,请使用array_map
:
$numbers = array_map(function($details) {
return $details['Weight'];
}, $array);
选项3。
选项4.如果您只需要最小或最大值,array_reduce()
可能会更快:
$min = array_reduce($array, function($min, $details) {
return min($min, $details['weight']);
}, PHP_INT_MAX);
这会有更多min()
s,但它们非常快。 PHP_INT_MAX
从高处开始,越来越低。您可以对$max
执行相同的操作,但是您可以从0
或-PHP_INT_MAX
开始。
答案 1 :(得分:15)
foreach ($array as $k => $v) {
$tArray[$k] = $v['Weight'];
}
$min_value = min($tArray);
$max_value = max($tArray);
答案 2 :(得分:9)
对于使用PHP 5.5+的人来说,使用array_column可以轻松完成。不再需要那些丑陋的array_maps了。
如何获得最大值:
$highest_weight = max(array_column($details, 'Weight'));
如何获得最小值
$lowest_weight = min(array_column($details, 'Weight'));
答案 3 :(得分:3)
值得注意的是,上述两种解决方案都使用数组形式的额外存储(其中前两个,第二个使用一个数组),然后使用“额外存储”数组找到min和max。虽然这在真正的编程世界中是可以接受的(谁给出了两个关于“额外”存储的东西?)但它会让你在编程101中获得“C”。
只需两个额外的内存插槽即可轻松解决查找最小值和最大值的问题
$first = intval($input[0]['Weight']);
$min = $first ;
$max = $first ;
foreach($input as $data) {
$weight = intval($data['Weight']);
if($weight <= $min ) {
$min = $weight ;
}
if($weight > $max ) {
$max = $weight ;
}
}
echo " min = $min and max = $max \n " ;
答案 4 :(得分:0)
$num = array (0 => array ('id' => '20110209172713', 'Date' => '2011-02-09', 'Weight' => '200'),
1 => array ('id' => '20110209172747', 'Date' => '2011-02-09', 'Weight' => '180'),
2 => array ('id' => '20110209172827', 'Date' => '2011-02-09', 'Weight' => '175'),
3 => array ('id' => '20110211204433', 'Date' => '2011-02-11', 'Weight' => '195'));
foreach($num as $key => $val)
{
$weight[] = $val['Weight'];
}
echo max($weight);
echo min($weight);
答案 5 :(得分:0)
<?php
$array = array (0 =>
array (
'id' => '20110209172713',
'Date' => '2011-02-09',
'Weight' => '200',
),
1 =>
array (
'id' => '20110209172747',
'Date' => '2011-02-09',
'Weight' => '180',
),
2 =>
array (
'id' => '20110209172827',
'Date' => '2011-02-09',
'Weight' => '175',
),
3 =>
array (
'id' => '20110211204433',
'Date' => '2011-02-11',
'Weight' => '195',
),
);
foreach ($array as $key => $value) {
$result[$key] = $value['Weight'];
}
$min = min($result);
$max = max($result);
echo " The array in Minnumum number :".$min."<br/>";
echo " The array in Maximum number :".$max."<br/>";
?>
答案 6 :(得分:0)
$Location_Category_array = array(5,50,7,6,1,7,7,30,50,50,50,40,50,9,9,11,2,2,2,2,2,11,21,21,1,12,1,5);
asort($Location_Category_array);
$count=array_count_values($Location_Category_array);//Counts the values in the array, returns associatve array
print_r($count);
$maxsize = 0;
$maxvalue = 0;
foreach($count as $a=>$y){
echo "<br/>".$a."=".$y;
if($y>=$maxvalue){
$maxvalue = $y;
if($a>$maxsize){
$maxsize = $a;
}
}
}
echo "<br/>max = ".$maxsize;
答案 7 :(得分:0)
如果不使用min
或max
等预定义函数,那该怎么办?
$arr = [4,5,6,7,8,2,9,1];
$val = $arr[0];
$n = count($arr);
for($i=1;$i<$n;$i++) {
if($val<$arr[$i]) {
$val = $val;
} else {
$val = $arr[$i];
}
}
print($val);
&GT;
答案 8 :(得分:0)
从数组中快速打印五个最大和最小数字,而不使用php中的排序数组 : -
<?php
$array = explode(',',"78, 60, 62, 68, 71, 68, 73, 85, 66, 64, 76, 63, 81, 76, 73,
68, 72, 73, 75, 65, 74, 63, 67, 65, 64, 68, 73, 75, 79, 73");
$t=0;
$l=count($array);
foreach($array as $v)
{
$t += $v;
}
$avg= $t/$l;
echo "average Temperature is : ".$avg." ";
echo "<br>List of seven highest temperatsures :-";
$m[0]= max($array);
for($i=1; $i <7 ; $i++)
{
$m[$i]=max(array_diff($array,$m));
}
foreach ($m as $key => $value) {
echo " ".$value;
}
echo "<br> List of seven lowest temperatures : ";
$mi[0]= min($array);
for($i=1; $i <7 ; $i++)
{
$mi[$i]=min(array_diff($array,$mi));
}
foreach ($mi as $key => $value) {
echo " ".$value;
}
?>