Calculating Ratios of Positive, Negative, and Zero Elements in an Array Using PHP
Efficiently Calculating Ratios of Positive, Negative, and Zero Elements in a PHP Array
Introduction
When working with arrays of integers, we often need to analyze their contents. One common task is to calculate the proportion of positive, negative, and zero values in a given array. This article explores how to achieve this in PHP and provides a detailed breakdown of the logic and implementation.

Problem Statement
Given an array of integers, we need to compute three ratios:
- The proportion of positive numbers.
- The proportion of negative numbers.
- The proportion of zeros.
Each ratio should be printed as a decimal value rounded to six decimal places.
Approach to Solve the Problem
To solve this problem efficiently, we follow these steps:
- Read the array of numbers — The user provides a series of numbers as input.
- Count the occurrences of positive, negative, and zero elements.
- Compute the ratios by dividing each count by the total number of elements.
- Print the results with six decimal places for accuracy.
PHP Implementation
The following PHP script reads an array of integers from user input, processes the data, and prints the required ratios.
<?php
function plusMinus($arr) {
$n = count($arr);
$pos = 0;
$neg = 0;
$zero = 0;
// Count positive, negative, and zero elements
foreach ($arr as $num) {
if ($num > 0) {
$pos++;
} elseif ($num < 0) {
$neg++;
} else {
$zero++;
}
}
// Calculate and print ratios with six decimal places
printf("%.6f\n", $pos / $n);
printf("%.6f\n", $neg / $n);
printf("%.6f\n", $zero / $n);
}
// Taking input from user
echo "Enter numbers separated by space: ";
$handle = fopen("php://stdin", "r");
$input = trim(fgets($handle));
$arr = explode(" ", $input);
$arr = array_map('intval', $arr); // Convert input strings to integers
plusMinus($arr);
?>
Code Explanation
1. Function plusMinus($arr)
This function takes an array of integers and performs the following operations:
- It initializes three counters:
$pos
for positive numbers,$neg
for negative numbers, and$zero
for zeroes. - It loops through the array and updates the respective counters.
- Finally, it calculates the required ratios and prints them using
printf("%.6f\n", value);
to ensure six decimal places.
2. Taking User Input
fgets($handle)
reads user input as a single string.explode(" ", $input)
splits the string into an array of individual numbers.array_map('intval', $arr)
converts these string values into integers.
Example Run
Input:
1 1 0 -1 -1
Processing:
- Positive numbers:
1, 1
→ 2 elements - Negative numbers:
-1, -1
→ 2 elements - Zeros:
0
→ 1 element - Total elements = 5
Computed Ratios:

Output:
0.400000
0.400000
0.200000
Time Complexity Analysis
- Looping through the array takes O(n) time.
- Counting and calculating ratios take O(1) time.
- Overall complexity: O(n), where
n
is the number of elements in the array.
This makes our approach efficient for large input sizes.
Conclusion
In this article, we explored how to calculate the proportion of positive, negative, and zero values in an array using PHP. We used a simple loop to count occurrences and formatted the output to six decimal places for precision. This approach is efficient and works well for both small and large datasets.