# php - How to get common minimum value in two arrays

420

I have a function where is checks on two arrays and returns the common minimum value in both the arrays. IT returns the correct answer when both the arrays have equal number of elements. But is the other other is bigger it does not return the correct one. How to overcome this?

```
<?php
$A = array(0);
$B= array(1,0);
$n = sizeof($A);
$m = sizeof($B);
sort($A);
sort($B);
$i = 0;
for ($k = 0; $k < $n; $k++) {
if ($i < $m - 1 AND $B[$i] < $A[$k])
$i += 1;
if ($A[$k] == $B[$i])
echo $A[$k];
}
echo "end";
?>
```

Thanks

## Answer

## Solution:

A way simpler way would be to take the minimum value of the intersection of your arrays :

## Answer

## Solution:

Leverage PHP function array_intersect() to extract common values in two arrays, then fetch the minumum value by using min()

Like so:

## Answer

## Solution:

I would use array_intersect. It will give you an array with only values that are present in both arrays you are looking through, then you can find the lowest value in the array