Thursday, 30 August 2018

Check whether all values ​​in an array are int

Could not find a way to do this, but I'm looking for a way to check if all value's in an array are INT, if not it should return false.

Notice I cant sanitize the value's!
// the idea
$good = array(1,2,3,4);

$bad  = array(1,2,'no',4);

if($good){

   echo 'works';

}else{

   echo 'failed';

}


Here is a reusable function you could use which will return false if any element of the array is not an int.
$bad  = array(1,2,'no',4);

CheckArray($bad);

function checkArray($array){

foreach($array as $value){
 if( ! is_int($value)){
    return false;
  }
 }
return true;
}

0 comments:

Post a Comment