PHP validate all element of array by type (Integer-String-Boolean-Digit..) By master condition

PHP validate all element of array contains only integer-String-Boolean-Digit-Float or any specific type value by master condition. Single condition for check all element of array contains only integer, check all element of array contains only string, check all element of array contains only Boolean, check all element of array contains only Digit or any specific type.

For PHP programing in many cases requirement comes to validate / sanetise array element. Validation on array looks bit complex, as process required checking all element of array for specific type using for loop or other array function.

If we have some master single code line, that can use for any value type checking for all array element. It can be really helpful and time saving.

PHP validate all element of array by type
PHP validate all element of array by type

For all above validation issues array_filter is master function. You will find use of it by single line to validate array element type.

If you like below master one line validation for PHP validate all element of array, you will also like more useful array processing example in array processing post.

Content

Before check validation process for array element, look at array_filter. You can also find remove null values from array using array_filter example here.

Check all element of array contains only integer

PHP check if array contains only integers or check php validate array of integers or sanetise all element array integer or not.

How given example validate array element for integer value?

First array_filter apply is_integer function on all array element and return only valid integer type element array.

In IF condition we check array_filter output array count with input array count.

If both are equal, it means input array have all valid integer element. If both array have different count, it means input array have some invalid element, i.e., not integer element.

$arr = [1,2,3,'4'];
if (count(array_filter($arr, 'is_integer')) != count($arr)) {
    //Invalid array element
} else {
    //valid array element
}

 

Check all element of array contains only string

PHP check if array contains only string or check php validate array of string or sanetise all element array string or not.

How given example validate array element for string value?

First array_filter apply is_string function on all array element and return only valid string type element array.

In IF condition we check array_filter output array count with input array count.

If both are equal, it means input array have all valid string element. If both array have different count, it means input array have some invalid element, i.e., not string element.

$arr = ['one', 'two', 'three', 4];
if (count(array_filter($arr, 'is_string')) != count($arr)) {
    //Invalid array element
} else {
    //valid array element
}

 

Check all element of array contains only boolean

PHP check if array contains only boolean or check php validate array of boolean or sanetise all element array boolean or not.

How given example validate array element for boolean value?

First array_filter apply is_bool function on all array element and return only valid boolean type element array.

In IF condition we check array_filter output array count with input array count.

If both are equal, it means input array have all valid boolean element. If both array have different count, it means input array have some invalid element, i.e., not boolean element.

$arr = [true, false, 1];
if (count(array_filter($arr, 'is_bool')) != count($arr)) {
    //Invalid array element
} else {
    //valid array element
}

 

Check all element of array contains only digit

PHP check if array contains only digit or check php validate array of digit or sanetise all element array digit or not.

How given example validate array element for digit value?

First array_filter apply ctype_digit function on all array element and return only valid digit type element array.

In IF condition we check array_filter output array count with input array count.

If both are equal, it means input array have all valid digit element. If both array have different count, it means input array have some invalid element, i.e., not digit element.

$arr = [1, '2', '3', 4, 'test'];
if (count(array_filter($arr, 'ctype_digit')) != count($arr)) {
    //Invalid array element
} else {
    //valid array element
}

 

Check all element of array contains only float

PHP check if array contains only float or check php validate array of float or sanetise all element array float or not.

How given example validate array element for float value?

First array_filter apply is_float function on all array element and return only valid float type element array.

In IF condition we check array_filter output array count with input array count.

If both are equal, it means input array have all valid float element. If both array have different count, it means input array have some invalid element, i.e., not float element.

$arr = [5.50, 0.80, 1, 'space'];
if (count(array_filter($arr, 'is_float')) != count($arr)) {
    //Invalid array element
} else {
    //valid array element
}

 

Check all element of array contains only double

PHP check if array contains only double or check php validate array of double or sanetise all element array double or not.

How given example validate array element for double value?

First array_filter apply is_double function on all array element and return only valid double type element array.

In IF condition we check array_filter output array count with input array count.

If both are equal, it means input array have all valid double element. If both array have different count, it means input array have some invalid element, i.e., not double element.

$arr = [1.000005, 2.5555699, 'wrong'];
if (count(array_filter($arr, 'is_double')) != count($arr)) {
    //Invalid array element
} else {
    //valid array element
}

 

Check all element of array contains only array

PHP check if array contains only array or check php validate array of array or sanetise all element array array or not.

How given example validate array element for array value?

First array_filter apply is_array function on all array element and return only valid array type element array.

In IF condition we check array_filter output array count with input array count.

If both are equal, it means input array have all valid array element. If both array have different count, it means input array have some invalid element, i.e., not array element.

$arr = [[1], [2,3], 'wrong'];
if (count(array_filter($arr, 'is_array')) != count($arr)) {
    //Invalid array element
} else {
    //valid array element
}

Hope you like master condition for PHP validate all element of array (integer, string, digit, float, double, array). Validation useful when process accept array values in specific type only, e.g., some integer ID of record.

If you found trick helpful, don’t forget to share with friends.