Solved

PHP array index storage (efficiency) question

Posted on 2011-02-26
4
331 Views
Last Modified: 2012-05-11
I have a web form with input fields that have  (POST)  name variables that are an array.
The numeric array indexes are best for me if they are sparse (powers of 2).
In PHP are numeric array indices associative or true contiguous array vectors?
i.e., is it inefficient for either storage or execution time to utilize sparse array indexes?
(if needed i can change to contiguous, but would rather not if all else is the same)

<input ...  name=A[1]... />
<input ...  name=A[2]... />
<input ...  name=A[4]... />
...
<input ...  name=A[1048576]... />

and sample usage once in PHP
$_POST['A'][index]
and
array_sum($_POST['A'])

please advise

thanks
0
Comment
Question by:willsherwood
4 Comments
 
LVL 38

Assisted Solution

by:Aaron Tomosky
Aaron Tomosky earned 166 total points
ID: 34988220
I believe associative. As you can use text and whatnot for keys.
0
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 167 total points
ID: 34988323
From http://us2.php.net/manual/en/language.types.array.php , "An array in PHP is actually an ordered map."  Details on that page.
0
 
LVL 4

Assisted Solution

by:DanDauchy
DanDauchy earned 167 total points
ID: 34988328
I believe it is associative. ie when you create an array at index 1 and then 100, it doesn't fill in the middle w/ padding.  

for storage, it is not inefficient to have an array at 1 and 100 because it only allocates enough memory for those 2 elements.  

for execution time it is not any more or less efficient because you are simply offsetting the start of that array with a number (as you would from index 1 to 2 or from 1 to 100).  Even if it was a contiguous array it would make no difference on execution time.
0
 

Author Closing Comment

by:willsherwood
ID: 34988709
thanks all!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now