-1

I want something like array_validate('is_string',$arr);

If the array elements are all strings, then it will return TRUE.
But, if there is/are non-string type in the array, it will return FALSE.

Is there any built-in PHP function that does this?

4
  • use foreach loop and the function is_string() Commented Oct 9, 2015 at 8:18
  • @ArmanMalekzade I know it, but is there a more efficient way? Something like array_map or array_filter, but this one validates all of the array elements... Commented Oct 9, 2015 at 8:20
  • 1
    if you visit the documentation in php.net ... exactly where they describe is_string() function ... in that page there is an example of what you want to do ... so i think there is no built-in function for that ! php.net/manual/en/function.is-string.php Commented Oct 9, 2015 at 8:22
  • you can use : stackoverflow.com/questions/10137674/… Commented Oct 9, 2015 at 8:23

3 Answers 3

1

Another solution using array_reduce:

function array_validate($callable, $arr)
{
  return array_reduce($arr, function($memo, $value) use ($callable){ return $memo === true && call_user_func($callable,$value); }, true);
}

array_validate('is_string', ["John", "doe"]); // True
array_validate('is_string', ["John", "Doe", 94]); // false

Also, you could use other callables:

array_validate([$object, "method"], ["something"]);
array_validate("is_array", [ [], [] ]); // true
Sign up to request clarification or add additional context in comments.

Comments

0

Run through array, if a element is not a string return false. If all elements are string it will reach the end off the function and return true.

function array_validate($array){
    foreach($array as $arr){
        if(!is_string($arr){
            return false;
        }
    }
    return true;
}

2 Comments

So, there is no built-in function that does the thing?
Not that I know off. is_string exists for a string but if you want to check an array you'll have to use a workaround
0

There is no built in function, but this might work for you:

function array_type_of(array $array, $type)
{
    if ('boolean'  === $type ||
        'integer'  === $type ||
        'double'   === $type ||
        'string'   === $type ||
        'array'    === $type ||
        'resource' === $type ||
        'object'   === $type || // for any object
        'NULL'     === $type) {

        foreach ($array as $v) {
            if (gettype($v) !== $type) {
                return false;
            }
        }
    } else {
        foreach ($array as $v) {
            if (!$v instanceof $type) {
                return false;
            }
        }
    }

    return true;
}

Use it as:

$array = array( /* values */ );

$istype = array_type_of($array, 'boolean');
$istype = array_type_of($array, 'integer');
$istype = array_type_of($array, 'double');
$istype = array_type_of($array, 'string');
$istype = array_type_of($array, 'resource');

$istype = array_type_of($array, 'DateTime');
$istype = array_type_of($array, 'IteratorAggregate');

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.