FreshPro
FreshPro

Reputation: 873

Going through array elements and compare them with each other

I have an array in php and what I wanna do is to check if certain values are in the array (which can easily be done using in_array no problem) and to check if some values are not in array.

Here's an example of what I'm trying to do. I'm trying to return something if $array has 1,2,11,41 but not 13,21,12,22,14

It should say "not all valid" but instead I get "all valid"

Here's the snippet: snippet

What am I doing wrong?

Upvotes: 1

Views: 195

Answers (4)

RAUSHAN KUMAR
RAUSHAN KUMAR

Reputation: 6006

You can make a reusable function for your requirement which accepts three parametres your main array($mainArray), your required valid elements array($reqArray), and the restricted invalid elements array($restrictArray). And then return the status from this function to your main calling function. like this

public function getStatus($mainArray,$reqArray,$restrictArray){
    $status = true;
    foreach ($array as $num){
        if(in_array($num,$reqArray) && !in_array($num,$restrictArray)){
            $status = false;
            break;
        }
    }
   return $status;
}

and in your main function you can call this reusable function like this

public function mainFunction(){
    $array = array("1","2","11","13","21","12","22","14", "41");
    $required = array("1","2","11","41");
    $disallowed = array("13","21","12","22","14");
    if(getStatus($array,$required,$disallowed){
        echo "All Valid";
    }else{
        echo "Some Inputs Are Invalid";
    }
}

Upvotes: 1

Bibhudatta Sahoo
Bibhudatta Sahoo

Reputation: 4894

You can do like this for dynamic values

$array = array("1","2","11","13","21","12","22","14", "41");
$required = array("1","2","11","41");
$disallowed = array("13","21","12","22","14");
$flag='All valid';
foreach ($array as $num){
    if(in_array($num,$required )&&!in_array($num,$disallowed )){
        $flag='Not all valid';
    }
}
echo  $flag;

Upvotes: 5

Shaunak Shukla
Shaunak Shukla

Reputation: 2347

<?php
        //Enter your code here, enjoy!

$array = array("1","2","11","13","21","12","22","14", "41");


if (
        ((in_array('1', $array))
        || (in_array('2', $array)) 
        || (in_array('11', $array)) 
        || (in_array('41', $array))) 
        && ((!in_array('13', $array))
        && (!in_array('21', $array))
        && (!in_array('12', $array))
        && (!in_array('22', $array))
        && (!in_array('14', $array)))
    ) {
        echo "All valid";
    } else {
        echo "Not all valid";
    }

You need to care about brackets in if condition. You need (1,2,11,41) in array but (13,21,12,22,14) not in array, so your main condition will be in main brackets with && conjunction. Then add brackets for sub-conditions into those brackets.

You used || for the first set - Do you mean any of that value can be valid? If you need all must have in array of first set then you must change || to &&

Upvotes: 3

gavgrif
gavgrif

Reputation: 15509

The echoed result provided is in fact correct - you are checking whether the items exist in the array - since you are using the || ('or' operator) the first positive instance will return the "all valid" outcome and the remaining options will never be checked. In effect - you are saying if "1" is in the array- echo "all valid". You need to refactor your code to ensure that all elements are checked.

$array = array("1","2","11","13","21","12","22","14", "41");


if (
        (in_array('1', $array))
        || (in_array('2', $array)) 
        || (in_array('11', $array)) 
        || (in_array('41', $array)) 
        && (!in_array('13', $array))
        && (!in_array('21', $array))
        && (!in_array('12', $array))
        && (!in_array('22', $array))
        && (!in_array('14', $array))
    ) {
        echo "All valid";
    } else {
        echo "Not all valid";
    }

Upvotes: 2

Related Questions