1

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?

1
  • 1
    First, please add your code here, not (only) as an external snippet. Second, you might want to use an array containing the relevant values, then compare that to the array you want to test within a loop. Commented Jul 12, 2017 at 11:27

4 Answers 4

5

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;
Sign up to request clarification or add additional context in comments.

2 Comments

Why not make the $flag a boolean? Plus, once you know the input isn't valid, you can break out of the loop immediately.
You are very welcome! One more thing though: if you would turn this into a reusable function, it would be a really great answer.
3
<?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 &&

Comments

2

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";
    }

Comments

1

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";
    }
}

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.