The maximum and minimum integer from the array, excluding NULL or ZERO

I use below php functions to get the highest and lowest integer value from my array.

min($output[$k]) and max($output[$k])

Problem: I want to get maximum and minimum integer values ​​from this array, excluding ZERO and NULL values

0
arrays php
source share
2 answers

Use array_diff() to filter the array:

 <?php header('Content-Type: text/plain'); $buffer = array(null, 0, 1, 2, 3, 0, null, 1); $filtered = array_diff($buffer, array(null, 0)); $max = max($filtered); $min = min($filtered); var_dump($max, $min); ?> 

Shows:

 int(3) int(1) 
+5
source share

Using

 max(array_filter($output[$k)); 
+1
source share

All Articles