0

I have 2 php array variables. Variables like below.

$arrIntPropertyIds = [ 1,2,3,4,5,6,7,8,9,10];
$arrIntRequestPropertyIds = [ 3, 5, 7];`

I want to check whether values of $arrIntRequestPropertyIds are in $arrIntPropertyIds without loop.

So I tried like below.

if( count(array_diff($arrIntPropertyIds,$arrIntRequestPropertyIds))==0 ) {
    echo 'invalid property Id'; 
} else { 
    echo 'Valid property Id'; 
}

If all OR any of the value OR atleast 1 value is/are there in first array then it should show message 'invalid'.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Pranav Unde
  • 193
  • 12

3 Answers3

0

All you have to do is change the order in which you present the arrays to the array_diff() function and then change the if accordingly

$arrIntPropertyIds = [ 1,2,3,4,5,6,7,8,9,10];
$arrIntRequestPropertyIds = [ 3, 5, 7];


if( count(array_diff($arrIntRequestPropertyIds, $arrIntPropertyIds))==0 ) {
    echo 'Valid property Id'; 
} else { 
    echo 'invalid property Id'; 
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0

Use array_diff. Simplest way to check this would be the following code

$containsValues = !array_diff($arrIntRequestPropertyIds, $arrIntPropertyIds);

Would return true or false

0
<?php
$arrIntPropertyIds = [ 1,2,3,4,5,6,7,8,9,10];
$arrIntRequestPropertyIds = [ 3, 5, 7];

$intersect = count(array_intersect($arrIntPropertyIds, $arrIntRequestPropertyIds));

if ($intersect == count($arrIntRequestPropertyIds)) {
    echo '$arrIntPropertyIds include all of $arrIntRequestPropertyIds';
} elseif ($intersect > 0) {
    echo '$arrIntPropertyIds include part of $arrIntRequestPropertyIds';
} else {
    echo '$arrIntPropertyIds not include any of $arrIntRequestPropertyIds';
}

run PHP online

Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39