473,399 Members | 4,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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 2378
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: jerrygarciuh | last post by:
Could someone please tell me the correct way to decalre this multidimensional array? TIA! jg var n = = = 25, = 0 ], = = 10, = 20 ],
5
by: mark4asp | last post by:
Suppose I wanted to create an array that was associative in 2 dimensions. The rows are associated with numbers. The columns with words. For instance for 3 columns. This array will have 20 rows...
4
by: Robert | last post by:
I am curious why some people feel that Javascript doesn't have associative arrays. I got these definitions of associative arrays via goggle: Arrays in which the indices may be numbers or...
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
35
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
16
by: frizzle | last post by:
Hi there! I'd like to create a function which input is the result of a mySQL query. The output should be exactly the same, only not a mySQL result array, but a 'real' array. So it should also...
30
by: josh | last post by:
Hi all, what does it meaning that strange sintax (look at the object :) ? if I have i.e. array.length I can use array. and is it IE/Firefox compatible??
7
by: harvey | last post by:
What is the easiest way to get both the last item name and last item value from an associative array? I can get the value with array_pop() but can't see anything similar for the item's name. ...
14
by: julie.siebel | last post by:
I've been wrestling with a really complex page. All the data is drawn down via SQL, the page is built via VBScript, and then controlled through javascript. It's a page for a travel company that...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.