-1

Possible Duplicate:
PHP Arrays: A good way to check if an array is associative or numeric?

My one function's one parameter takes the values of following types:

null,

'*','',

array('domain1','domain2'),

array('domain1'=>'*','domain2'=>'group1','domain3'=>array('group1','group2')),

I have problems while I am trying to determine what kind of type the parameter. Especially, at determining if it is an array and its key parameters.

How can I determine the parameter type basically?

Community
  • 1
  • 1
mrdev
  • 617
  • 7
  • 8

4 Answers4

0

you can use is_array function to find out if the parameter is an array.would you give us your codes to give more details?

hamedkh
  • 909
  • 3
  • 18
  • 35
0

PHP gettype function should do what you want. For checking specific type you can use is_* functions like is_array. You can find info about them here.

Valdars
  • 833
  • 6
  • 9
0

Php has a ton of functions for this.

Here is a search with most (if not all) of these.

You will be using mostly functions that start with is (is_array(), is_null(), isset())

Vlad Preda
  • 9,780
  • 7
  • 36
  • 63
0

If you want an easy one liner for determining type, you probably don't want to be using arrays at all. You might find that populating your data into an object of a specific class would be more useful.

Then you can test the object type with instanceof or is_a().

Using an object would also give you the ability to provide additional functionality to the data structure. And it could still be treated as an array (eg with foreach(), etc) by making it an Iterator.

If you prefer to stick with arrays, and you want to distinguish between a keyed and a numeric array, you could check the answers to this question.

The best answer there was a short function along the following lines:

function array_isassoc(array $array) {
    return array_keys($array) !== range(0,count($array)-1);
}

This will return true for a keyed array or false for a numeric array.

It's probably the most reliable method you could use, although it will stop working if you have a numeric array and remove elements from it without renumbering the keys.

Community
  • 1
  • 1
Spudley
  • 166,037
  • 39
  • 233
  • 307
  • The array_isassoc() function will not work for an enumerated array with non-contiguous keys - if there's any gaps in the sequence, it will fail – Mark Baker Jan 18 '13 at 15:21
  • @MarkBaker - Why are we having this conversation all over again? Go bash [the original answer](http://stackoverflow.com/questions/173400/php-arrays-a-good-way-to-check-if-an-array-is-associative-or-numeric#answer-173479), and mark this question as a duplicate. Oh, and while you're there, you can upvote [the correct answer](http://stackoverflow.com/questions/173400/php-arrays-a-good-way-to-check-if-an-array-is-associative-or-numeric/4254008#4254008). – Joseph Silber Jan 18 '13 at 15:22
  • @JosephSilber - to be fair, I did state that my preferred solution here is not to use an array at all, but a properly considered class structure. That isn't an answer on the other question because the other question is more direct about what it's asking. I only added the isassoc part of the answer for completeness, and I did reference the previous question when I did so. (having said all that, my guess is that the OP will go with a solution of that type rather than redo everything to use objects; it's probably a more pragmatic option if what he's got nearly works anyway) – Spudley Jan 18 '13 at 15:28