0
$array = [0,1,2,3,4,5,6];
$min = min($array);
$max= max($array);  
  • I want min as 1 and max as 6 i don't want to consider 0 as min with php array.
2
  • 2
    You'll have to remove 0 from the array. I don't think there is any other way. Commented Jan 16, 2012 at 11:59
  • 1
    Simplest solution: remove 0 from the array before calling min. Other solutions would involve writing your own custom version of min. Commented Jan 16, 2012 at 11:59

2 Answers 2

8

Use array_filter to filter the zero out of the array

$a = array(0,1,2,3,4,5,6);

$a = array_filter($a);

$min = min($a);
$max = max($a);
Sign up to request clarification or add additional context in comments.

4 Comments

array filter will do it without such a function
That works indeed, but array_filter already filters out 0, as that's converted to the boolean false. So the custom function should not be necessary.
Great answer +1, but I think if in the future he wants to change the limit to get min as 2, or whatever.
@Márcio when that time comes, array_filter is still the correct answer
0

Use array_shift for 0 and array_pop for last

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.