/usr/portage

PHP trick: array or struct? 2

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.

Filed under , & two comments & no trackbacks

Trackbacks

Trackback specific URI for this entry

No Trackbacks

Comments

  1. Marc Jakubowski answers:
    published on October 22nd 2007, 10:14:33 am *

    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.

    Reply

  2. Lars Strojny answers:
    published on October 22nd 2007, 02:22:57 pm *

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

    Reply

Add a Comment & let me know what you think