469,625 Members | 1,109 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,625 developers. It's quick & easy.

How to test that an array is 'indexed' vs 'associative'

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.

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?

Daniel Klein
Cuyahoga Falls, OH
Jun 2 '08 #1
13 2193
my opinion is dont check it. just retreve the values with
"array_values"
Jun 2 '08 #2
On Sat, 31 May 2008 15:57:07 +0200, Daniel Klein
<da*****@featherbrain.netwrote:
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.

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?
Something like this maybe?
array_keys($array) === array_flip(array_keys($array))
--
Rik Wasmus
....spamrun finished
Jun 2 '08 #3
On Sat, 31 May 2008 16:03:12 +0200, "Rik Wasmus"
<lu************@hotmail.comwrote:
>On Sat, 31 May 2008 15:57:07 +0200, Daniel Klein
<da*****@featherbrain.netwrote:
>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.

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?

Something like this maybe?
array_keys($array) === array_flip(array_keys($array))
This is exactly what I need!

Thanks,
Dan
Jun 2 '08 #4
On May 31, 9:57*pm, Daniel Klein <dani...@featherbrain.netwrote:
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.

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?

Daniel Klein
Cuyahoga Falls, OH
My idea is using preg_match like this:

if (preg_match('|[^0-9]|', implode('', array_keys($array)))
{
echo "That must be 'associative' array.\n";
}
Jun 2 '08 #5
Rik Wasmus wrote:
On Sat, 31 May 2008 15:57:07 +0200, Daniel Klein
<da*****@featherbrain.netwrote:
>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.

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?

Something like this maybe?
array_keys($array) === array_flip(array_keys($array))
elegant code.
Would you be so kind as to explain how it works.
bill
Jun 2 '08 #6
On Sun, 01 Jun 2008 12:34:25 +0200, bill <no****@spamcop.netwrote:
Rik Wasmus wrote:
>On Sat, 31 May 2008 15:57:07 +0200, Daniel Klein
<da*****@featherbrain.netwrote:
>>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.

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?
Something like this maybe?
array_keys($array) === array_flip(array_keys($array))

elegant code.
Would you be so kind as to explain how it works.
bill

Euhm, the manual for those functions?

Will fail, because it's not sequential:
array(0 ='a', 1 ='b', 3 ='c');
=>
array(0 =0, 1 =1, 2 =3);
!==
array(0 =0, 1 =1, 3 =2);

Will fail, because it has a string key:
array(0 ='a', 'foo' ='b', 2 ='c');
=>
array(0 =0, 1 ='foo', 2 =2);
!==
array(0 =0, 'foo' =1, 2 =2);

Will succeed:
array(0 ='a', 1 ='b', 2 ='c');
=>
array(0 =0, 1 =1, 2 =2);
===
array(0 =0, 1 =1, 2 =2);
--
Rik Wasmus
....spamrun finished
Jun 2 '08 #7
Rik Wasmus wrote:
On Sat, 31 May 2008 15:57:07 +0200, Daniel Klein
<da*****@featherbrain.netwrote:
>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.

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?

Something like this maybe?
array_keys($array) === array_flip(array_keys($array))
--Rik Wasmus
...spamrun finished
Rik, you are sneaky. Nice! =D

--
Curtis
Jun 2 '08 #8
Greetings, Rik Wasmus.
In reply to Your message dated Sunday, June 1, 2008, 15:39:20,
>>>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.

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?
Something like this maybe?
array_keys($array) === array_flip(array_keys($array))

elegant code.
Would you be so kind as to explain how it works.
bill
Euhm, the manual for those functions?
Will fail, because it's not sequential:
array(0 ='a', 1 ='b', 3 ='c');
=>
array(0 =0, 1 =1, 2 =3);
!==
array(0 =0, 1 =1, 3 =2);
Will fail, because it has a string key:
array(0 ='a', 'foo' ='b', 2 ='c');
=>
array(0 =0, 1 ='foo', 2 =2);
!==
array(0 =0, 'foo' =1, 2 =2);
Will succeed:
array(0 ='a', 1 ='b', 2 ='c');
=>
array(0 =0, 1 =1, 2 =2);
===
array(0 =0, 1 =1, 2 =2);
Will fail on

<?php

$arr = array(1 =0, 2 =1, 3 =2);
$test = array_keys($arr);
if($test === array_flip($test))
{
print('Ok');
}
else
{
print_r($test);
}

?>
--
Sincerely Yours, AnrDaemon <an*******@freemail.ru>

Jun 27 '08 #9
On Wed, 04 Jun 2008 20:38:08 +0200, AnrDaemon <an*******@freemail.ru
wrote:
Greetings, Rik Wasmus.
In reply to Your message dated Sunday, June 1, 2008, 15:39:20,
>>>>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.
>
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?
Something like this maybe?
array_keys($array) === array_flip(array_keys($array))

elegant code.
Would you be so kind as to explain how it works.
bill

>Euhm, the manual for those functions?
>Will fail, because it's not sequential:
array(0 ='a', 1 ='b', 3 ='c');
=>
array(0 =0, 1 =1, 2 =3);
!==
array(0 =0, 1 =1, 3 =2);
>Will fail, because it has a string key:
array(0 ='a', 'foo' ='b', 2 ='c');
=>
array(0 =0, 1 ='foo', 2 =2);
!==
array(0 =0, 'foo' =1, 2 =2);
>Will succeed:
array(0 ='a', 1 ='b', 2 ='c');
=>
array(0 =0, 1 =1, 2 =2);
===
array(0 =0, 1 =1, 2 =2);

Will fail on

<?php

$arr = array(1 =0, 2 =1, 3 =2);
Yes, as it should, do you see a 0 key in there?
You might have argued the point wether this one should be considered valid:

$arr = array(1 =0, 0 =1, 2 =2);

.... however, as they're not sequential, I choose to consider them not
valid. Should one want that, a simple ksort($arr); before the array_keys()
would do the trick.
--
Rik Wasmus
....spamrun finished
Jun 27 '08 #10
On May 31, 4:57*pm, Daniel Klein <dani...@featherbrain.netwrote:
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.

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?

Daniel Klein
Cuyahoga Falls, OH
You can easily check keys with is_integer(array_key());
Jun 27 '08 #11
Greetings, Rik Wasmus.
In reply to Your message dated Wednesday, June 4, 2008, 22:49:26,
On Wed, 04 Jun 2008 20:38:08 +0200, AnrDaemon <an*******@freemail.ru>
wrote:
>Greetings, Rik Wasmus.
In reply to Your message dated Sunday, June 1, 2008, 15:39:20,
>>>>>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.
>>
>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?
Something like this maybe?
array_keys($array) === array_flip(array_keys($array))

elegant code.
Would you be so kind as to explain how it works.
bill

>>Euhm, the manual for those functions?
>>Will fail, because it's not sequential:
array(0 ='a', 1 ='b', 3 ='c');
=>
array(0 =0, 1 =1, 2 =3);
!==
array(0 =0, 1 =1, 3 =2);
>>Will fail, because it has a string key:
array(0 ='a', 'foo' ='b', 2 ='c');
=>
array(0 =0, 1 ='foo', 2 =2);
!==
array(0 =0, 'foo' =1, 2 =2);
>>Will succeed:
array(0 ='a', 1 ='b', 2 ='c');
=>
array(0 =0, 1 =1, 2 =2);
===
array(0 =0, 1 =1, 2 =2);

Will fail on

<?php

$arr = array(1 =0, 2 =1, 3 =2);
Yes, as it should, do you see a 0 key in there?
Nop. But they are "numeric and sequential" as requested :)
You might have argued the point wether this one should be considered valid:
$arr = array(1 =>0, 0 =1, 2 =2);

This one isn't, according to the statement of (sorry for spelling) "sequentity".
... however, as they're not sequential, I choose to consider them not
valid. Should one want that, a simple ksort($arr); before the array_keys()
would do the trick.
Right. :)
--
Sincerely Yours, AnrDaemon <an*******@freemail.ru>

Jun 27 '08 #12
On Fri, 06 Jun 2008 12:42:55 +0200, AnrDaemon <an*******@freemail.ru
wrote:
Greetings, Rik Wasmus.
In reply to Your message dated Wednesday, June 4, 2008, 22:49:26,
>On Wed, 04 Jun 2008 20:38:08 +0200, AnrDaemon <an*******@freemail.ru>
wrote:
>>Greetings, Rik Wasmus.
In reply to Your message dated Sunday, June 1, 2008, 15:39:20,

>>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.
>>>
>>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?
> Something like this maybe?
>array_keys($array) === array_flip(array_keys($array))
>
elegant code.
Would you be so kind as to explain how it works.
bill
Euhm, the manual for those functions?

Will fail, because it's not sequential:
array(0 ='a', 1 ='b', 3 ='c');
=>
array(0 =0, 1 =1, 2 =3);
!==
array(0 =0, 1 =1, 3 =2);

Will fail, because it has a string key:
array(0 ='a', 'foo' ='b', 2 ='c');
=>
array(0 =0, 1 ='foo', 2 =2);
!==
array(0 =0, 'foo' =1, 2 =2);

Will succeed:
array(0 ='a', 1 ='b', 2 ='c');
=>
array(0 =0, 1 =1, 2 =2);
===
array(0 =0, 1 =1, 2 =2);

Will fail on

<?php

$arr = array(1 =0, 2 =1, 3 =2);
>Yes, as it should, do you see a 0 key in there?

Nop. But they are "numeric and sequential" as requested :)
Hmmm, yes, it's open for debate, I took it to mean 0-indexed, however, the
OP did not mention that specifically, you have a point. Much easier for me
if he did mean it like that :P.
--
Rik Wasmus
....spamrun finished
Jun 27 '08 #13
On Fri, 06 Jun 2008 20:55:22 +0200, "Rik Wasmus"
<lu************@hotmail.comwrote:
>On Fri, 06 Jun 2008 12:42:55 +0200, AnrDaemon <an*******@freemail.ru>
wrote:
>Greetings, Rik Wasmus.
In reply to Your message dated Wednesday, June 4, 2008, 22:49:26,
>>On Wed, 04 Jun 2008 20:38:08 +0200, AnrDaemon <an*******@freemail.ru>
wrote:
>>>Greetings, Rik Wasmus.
In reply to Your message dated Sunday, June 1, 2008, 15:39:20,

>>>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.
>>>>
>>>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?
>> Something like this maybe?
>>array_keys($array) === array_flip(array_keys($array))
>>
>elegant code.
>Would you be so kind as to explain how it works.
>bill
Euhm, the manual for those functions?

Will fail, because it's not sequential:
array(0 ='a', 1 ='b', 3 ='c');
=>
array(0 =0, 1 =1, 2 =3);
!==
array(0 =0, 1 =1, 3 =2);

Will fail, because it has a string key:
array(0 ='a', 'foo' ='b', 2 ='c');
=>
array(0 =0, 1 ='foo', 2 =2);
!==
array(0 =0, 'foo' =1, 2 =2);

Will succeed:
array(0 ='a', 1 ='b', 2 ='c');
=>
array(0 =0, 1 =1, 2 =2);
===
array(0 =0, 1 =1, 2 =2);

Will fail on

<?php

$arr = array(1 =0, 2 =1, 3 =2);
>>Yes, as it should, do you see a 0 key in there?

Nop. But they are "numeric and sequential" as requested :)

Hmmm, yes, it's open for debate, I took it to mean 0-indexed, however, the
OP did not mention that specifically, you have a point. Much easier for me
if he did mean it like that :P.
What the OP (that's me ;-) ) originally meant by 'numeric and sequential' is
a zero-based sequential numbering with no 'holes'. However, since it was
pointed out that PHP does not differentiate 'indexed' and 'associative'
arrays, it became a moot point. I am now not even concerned with the 'keys'
of the array.

That being said, I've been fussing around with subclassing ArrayObject (the
docs on this are very sparse and incomplete on the PHP website, and I had to
buy a book to get the skinny on it) and I have a case where the 'keys' must
be numeric but not necessary to be sequential. It was just a matter of
having a private '_validate' method that gets called before allowing
offsetSet() to do its thing, eg:

public function offsetSet($index, $value) {
self::_validate(array($index =$value));
parent::offsetSet($index, $value);
}

[Side note: I never did find out what ArrayObject::ARRAY_AS_PROPS is for and
what you can do with it :-S ]
Daniel Klein
Jun 27 '08 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by jerrygarciuh | last post: by
4 posts views Thread by Robert | last post: by
16 posts views Thread by frizzle | last post: by
30 posts views Thread by josh | last post: by
7 posts views Thread by harvey | last post: by
14 posts views Thread by julie.siebel | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.