查找数组-PHP的分数的正数和负数

时间:2016-05-02 08:35:34

标签: php arrays loops

我有一个array。它有正值,负值和零值。现在我想要计算正数,负数和零值,然后将每个数组除以数组计数。我试过以下方式:

<?php

$arr = ['-4','3','-9','0','4','1'];
$countNum = count($arr);
//print_r($countNum);
foreach ($arr as $key => $value) {
    if ($value<0) {
        continue;
    }elseif($value==0){
        continue;
    }else{
        $result = $value/$countNum;
        echo $result."</br>";
    }
}
?>

输出为:

0.5
0.66666666666667
0.16666666666667

但我想要而且应该是:

for positive, 3/6=0.500000
for negative, 2/6=0.333333
for zero, 1/6=0.166667

6 个答案:

答案 0 :(得分:2)

<?php

$arr = ['-4','3','-9','0','4','1'];
$countNum = count($arr);
$neg_count = $pos_count = $zero_count = 0;
//print_r($countNum);
foreach ($arr as $key => $value) {
    if ($value<0) {
        $neg_count++;
    }elseif($value==0){
        $zero_count++;
    }else{
       $pos_count++;
    }
}
echo 'for Negative : '.$neg_count/$countNum;
echo '<br>for Positive : '.$pos_count/$countNum;
echo '<br>for Zero : '.$zero_count/$countNum;

答案 1 :(得分:1)

试试这段代码。

<?php
$zero = '0';
$positive = '0';
$negative = '0';
$arr = array('-4','3','-9','0','4','1');
$total = count($arr);
foreach ($arr as $num){
    if ($num > '0'){
    $positive++;
    } else if ($num < '0'){
    $negative++;
    } else {
        $zero++;
    }
}
echo "Positive: ".$positive/$total;
echo '<br />';
echo "Negative: ".$negative/$total;
echo '<br />';
echo "Zero: ".$zero/$total;
?>

答案 2 :(得分:0)

如果要格式化输出值,请使用number_format功能。  number_format($positive/$total, 6, '.', '');

答案 3 :(得分:0)

这是一种略有不同的方法:

$arr = ['-4','3','-9','0','4','1'];
$countNum = count($arr);

// Positives = ['3', '4', '1']
$positives = array_filter($arr, function ($v) {
  return $v > 0;
});
// 3/6 = 0.5
echo count($positives) / $countNum;

// Zeros = ['0']
$zeros = array_filter($arr, function ($v) {
  return $v == 0;
});
// 1/6 = 0.1666666667
echo count($zeros) / $countNum;

// Negatives = ['-4', '-9']
$negatives = array_filter($arr, function ($v) {
  return $v < 0;
});
// 2/6 = 0.333333333
echo count($negatives) / $countNum;

答案 4 :(得分:0)

    <?php

        $arr        = ['-4','3','-9','0','4','1'];
        $countNum   = count($arr);
        $positives  = $negatives  = $zeros = 0;
        $arrValues  = array(
          "zeros"       => 0,
          "positives"   => 0,
          "negatives"   => 0,
        );


        foreach ($arr as $key => $value) {
            if ($value == 0) {          //ZEROS
                $zeros++;
                $arrValues['zeros']     = $zeros;
            }else if ($value < 0) {     //NEGATIVES
                $negatives++;
                $arrValues['negatives'] = $negatives;
            }else{                      //POSITIVES
                $positives++;
                $arrValues['positives'] = $positives;
            }
        }

        $arrValues["positives"] = doubleval($arrValues["positives"])/$countNum;
        $arrValues["negatives"] = doubleval($arrValues["negatives"])/$countNum;
        $arrValues["zeros"]     = doubleval($arrValues["zeros"])/$countNum;

        var_dump($arrValues);

答案 5 :(得分:0)

使用具有正值,负值和零值的命名键的数组的简短解决方案

$arr = ['-4','3','-9','0','4','1'];
$total = count($arr);
$numbers = ["negatives" => 0, "positives" => 0, "zeros" => 0];

foreach ($arr as $v) {
    $v = (int) $v;
    $numbers[($v == 0)? 'zeros' : (($v < 0)? 'negatives': 'positives')]++;
}

echo "for positive: ". "{$numbers["positives"]}/$total=" .$numbers["positives"]/$total .PHP_EOL;
echo "for negative: ". "{$numbers["negatives"]}/$total=" .$numbers["negatives"]/$total .PHP_EOL;
echo "for zero: ". "{$numbers["zeros"]}/$total=" .$numbers["zeros"]/$total .PHP_EOL;

输出:

for positive: 3/6=0.5
for negative: 2/6=0.33333333333333
for zero: 1/6=0.16666666666667