0

if I have:

$hold = array(1, 1, 4); // this will be true
$allArr = array(1, 1, 3, 4, 5);
$containsHold = count(array_intersect($hold, $allArr)) == count($hold);

$containhold is true and it is ok, but if I have next:

$hold = array(1, 1, 1); // this will be false but it is true
$allArr = array(1, 1, 3, 4, 5);
$containsHold = count(array_intersect($hold, $allArr)) == count($hold);

This return true, but I need to be false because $hold contain three 1

Goran Radovanovic
  • 103
  • 1
  • 1
  • 9
  • First of all read array of php, before asking this question. Still you got your answer in array_intersect (php array function) documentation. If not able to understand try this link :- http://stackoverflow.com/questions/9655687/php-check-if-array-contains-all-array-values-from-another-array – Alive to die - Anant Mar 09 '15 at 13:20
  • It is not answer for my example.Try $hold = array(1, 1, 1); This with array_intersect return true! – Goran Radovanovic Mar 09 '15 at 13:56

1 Answers1

0

if i uderstand correctly you shuld count the values of each array and than compare value and counts....:

function arrayContainArray($array, $array1){
    $c = array_count_values($array);
    $c1 = array_count_values($array1);
    foreach($c as $k=>$n){
        if(!isset($c1[$k]) || $c1[$k]<$n){ return false; }
    }
    return true;
}

$containsHold = arrayContainArray($hold, $allArr);
Federico
  • 1,231
  • 9
  • 13