2

PHP trick: array or struct?

published on 2007|10|21

PHP does not have different primitive types for arrays and structs. It is all an array. The only difference is, that a list contains just ascending integers as keys, starting from zero, the rest is a struct.

<?php
if (array_keys($array) === range(0, count($array) - 1)) {
    // Is an array
} else {
   // Is a struct
}

In the Zend Framework this differentiation is needed in the XmlRpc-component. I’ve submitted a patch to make it more efficient.

Tags: ,

Comments

Show comments linear or threaded

Marc Jakubowski answers:
published on 2007|10|22, 10:14h
*

Ich weiß nicht wie das Array in der Regel aussieht, aber sobald dann bei nem numerischen Array Lücken auftauchen (array(0=>‘a’, 2=>‘b’) klappts natürlich nicht mehr.

Lars Strojny returns:
published on 2007|10|22, 14:22h
*

Hat man ja normalerweise nicht. Also wenn irgendein Tool ein array(2 => "Foo", 4 => "bla") zurückgibt, ist das Tool futsch, IMHO.

Add comment


Textile-formatting allowed
E-Mail addresses will not be displayed and will only be used for E-Mail notifications
Pavatar/Gravatar/Favatar/MyBlogLog author images supported.