Re: How to test that an array is 'indexed' vs 'associative'
- From: NC <nc@xxxxxxxxx>
- Date: Sat, 31 May 2008 11:29:40 -0700 (PDT)
On May 31, 6:57 am, Daniel Klein <dani...@xxxxxxxxxxxxxxxx> wrote:
I have a class constructor that accepts an array as the only
argument. The catch is that the array MUST be an 'integer-indexed'
array, not an 'associative' array, because the index position has
meaning.
It doesn't matter:
The indexed and associative array types are the same type in PHP
http://www.php.net/manual/en/language.types.array.php
Further, the index position has NOTHING do do with keys. Elements
are
stored in the order they were added, regardless of what keys they
were
assigned (unless, of course, keys were not assigned explicitly and
key
assignment was done by PHP). Try this:
$myWeirdArray = array(
3 => 'Element # 3 was added first',
2 => 'Element # 2 was added second',
1 => 'Element # 1 was added last'
);
var_dump($myWeirdArray);
How can I test for this in the constructor without having to
traverse the entire array and checking to make sure the keys
are numeric and sequential?
Don't. If the order is that important, you can just ignore the
keys.
Alternatively, you could always rethink your approach... :)
Cheers,
NC
.
- References:
- How to test that an array is 'indexed' vs 'associative'
- From: Daniel Klein
- How to test that an array is 'indexed' vs 'associative'
- Prev by Date: Re: Creating a simple hitcounter for PHP web pages. (saves to files)
- Next by Date: Re: able to upload 28 meg file, yet php.ini limits posts to 10 megs. What is up with that?
- Previous by thread: Re: How to test that an array is 'indexed' vs 'associative'
- Next by thread: preg_match_all
- Index(es):
Relevant Pages
|