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

I have an array.Its have positive, negative and zero value.Now i want to count those number of positive, negative and zero value then divided each group by number of array count.I have tried below way:

<?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>";
    }
}
?>

Output is :

0.5
0.66666666666667
0.16666666666667

But i want and should be:

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

Try this code.

<?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;
?>
<?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;

If you want to format output values, use number_format function. number_format($positive/$total, 6, '.', '');

Here is a slightly different way to do this :

$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;
    <?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);

The short solution using array with named keys for positive, negative and zero value:

$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;

The output:

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