Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type check in all array elements

Tags:

arrays

types

php

Is there any simple way of checking if all elements of an array are instances of a specific type without looping all elements? Or at least an easy way to get all elements of type X from an array.

like image 785
DiogoNeves Avatar asked Feb 24 '10 01:02

DiogoNeves


1 Answers

$s = array("abd","10","10.1");
$s = array_map( gettype , $s);
$t = array_unique($s) ;
if ( count($t) == 1 && $t[0]=="string" ){
    print "ok\n";
}
like image 197
ghostdog74 Avatar answered Oct 20 '22 13:10

ghostdog74