Home arrow PHP arrow Page 4 - Arrays and PHP: A Primer
PHP

Arrays and PHP: A Primer


For many years, arrays have been used to logically store and sequence data. They have also been the point of confusion for many developers. In this article, Tuna will talk about the structure of arrays and how you can create and manipulate arrays with the PHP scripting language.

Author Info:
By: Tuna Celik
Rating: 4 stars4 stars4 stars4 stars4 stars / 30
December 20, 2001
TABLE OF CONTENTS:
  1. · Arrays and PHP: A Primer
  2. · The fundamentals of memory allocation
  3. · Arrays and their uses
  4. · Single dimension arrays
  5. · Multi-dimensional arrays
  6. · Conclusion

print this article
SEARCH DEVARTICLES

Arrays and PHP: A Primer - Single dimension arrays
(Page 4 of 6 )

As you should know by now, an array is simply a memory address, which is referenced by a variable name. Each indexes memory address is the same as its previous index, plus the size of the value stored in the current index. In PHP, you can declare an array using the array function, like in the following example:

<?

$Aarthur = array(

0 => 'Get Value 01',

1 => 'Get Value 02',

2 => 'Get Value 03',

3 => 'Get Value 04',

4 => 'Get Value 05'

);

?>


This would create the variable $Arthur as an array containing five indexes from 0 to 4. The values of each index will also be filled in. The code below does exactly the same thing:

<?

$Arthur[0] = "Get Value 01";

$Arthur[1] = "Get Value 02";

$Arthur[2] = "Get Value 03";

$Arthur[3] = "Get Value 04";

$Arthur[4] = "Get Value 05";

?>


The important thing to remember is that when we create our $Arthur variable using the array() function, we have to understand how they are accessed, and make sure we reference each index properly. For instance:

<?

$Arthur = array(

0 => 'Get Value 01'

);

?>


This creates a new array variable named $Arthur. The value of 'Get Value 01' is pointed to by using the index of 0:

print $Arthur[0];

However, if we have the following code:

<?

$Arthur = array(

'0' => 'Get Value 01'

);

?>


This would create the $Arthur array in the same way, however instead of accessing the index numerically, we would now access it using the index '0', which is a string. We would change '0' to any other string we like:

<?

$Arthur = array(

'first_index' => 'Get Value 01'

);

?>


There is one pitfall to avoid, however. When we're creating a new array variable, let's say that we used the following declaration:

<?

$Arthur = array(add => "Get Value Add");

?>


Although there is nothing wrong with this declaration, it is not syntactically correct. We are trying to create a new array with one index named add. Because add is not defined anywhere, the PHP parsing engine may raise a warning error to alert you of this. The proper way to create an array like this would be:

<?

$Arthur = array("add" => "Get Value Add");

?>


Notice now that we have a single-indexed array with one index named "add". We can now refer to our $Arthur array like this:

print $Arthur["add"]; // Would print "Get Value Add"

This allows us to create arrays with indexes that can either be referenced numerically or by strings. We can go crazy and combine both numerical and string indexes, like this:

<?

$Arthur = array("first" => "Get Value Add", 1 => "Value One", "Two" => 2);

Print $Arthur["first"] . "<br>";

Print $Arthur[1] . "<br>";

Print $Arthur["Two"] . "<br>";

?>


In the browser, our output would look like this:

Using both numerical and string indexes in one declaration
blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials