Re: Determining array type

From: Date: Sat, 16 Apr 2005 09:07:46 +0000
Subject: Re: Determining array type
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-213184@lists.php.net to get a copy of this message
NSK wrote:
We have two kinds of arrays in PHP: $array["name"] = "George" $array[0] = "George" How can I determine (with a function returning true/false for example) which type of array I am working with?
There aren't two "kinds" of arrays, there is only one array type: "There are no different indexed and associative array types in PHP; there is only one array type, which can both contain integer and string indices." [ http://www.php.net/manual/en/language.types.array.php ] So, you can have an array like this: $array = array("hello", "name" => "Burhan"); If you just want to know if all keys are numeric, then you just do a check on the keys : $keys = array_keys(array("hello","name" => "Burhan", "goodbye")); foreach($keys as $key) { /* put your check in here */ } Make sure you do a type-safe check ( === ) not ( == ) which is not type safe.

« previous php.general (#213184) next »