Oct
10
2008

ZCE Arrays: Enumerated Arrays

Their are two types of arrays Enumerated Arrays and Associative Arrays with these complex names is just referring to how its index’ed. Arrays will be covered in the PHP5 Zend Certification and high understanding is required.

Enumerated Arrays

An Enumerated array are similar to a table with a single column.
Enumerated Arrays are made with numerical indexes, the index increases from 0. When using arrays remember that the first piece of data is stored at 0 the second at 1. Enumerated arrays can be assigned numbers otherwise a incremented number will be assigned to each data element.
Create Array Method 1:

$Beatles = array();
$Beatles[0] = 'John';
$Beatles[1] = 'Paul';
$Beatles[2] = 'George';
$Beatles[3] = 'Ringo';

Create Array Method 2:

$Beatles[0] = 'John';
$Beatles[1] = 'Paul';
$Beatles[2] = 'George';
$Beatles[3] = 'Ringo';

As you can see Method 1 and Method 2 are very similar. If you do not initiate an array using $Beatles = array(); then it will be initiated anyway when adding the first peace of data. However, it is a better coding practice to explicitly initialize the array.
Create Array Method 3:

$Beatles = array('John','Paul','George','Ringo');

Create Array Method 4:

$Beatles[] = 'John';
$Beatles[] = 'Paul';
$Beatles[] = 'George';
$Beatles[] = 'Ringo';

What? You can create Enumerated Arrays without assigning numbers!
Great stuff, for lazy people like unless you want to give each peace of data a number it gives it them automatically.

Enumerated Array’s Facts:

- Enumerative indexed only by numbers
- arrays can simulate different structures (includeing queues and stacks)
- php arrays come with great flexibility.
- index can be any number negative or positive
- index can numbers can have large gaps between them
- index in array do not determine the order of elements
- when element is added to array, it automatically allocates a number (highest index +1)

Appending Enumerated Arrays

If you know how many elements are in an array, you can append to the array at any point during the script by specifying the index. For example, you could append to the $Beatles array shown above as follows:

$Beatles[5] = 'Nat';

However if you don’t know how many elements are in an array, we can do the lazy way without using a index such as:

$Beatles[] = 'Nat';

Output Enumerated Arrays

You can use the simple way, if you know what the index of the value you want to output such as echo $Beatles[2]; but their is far more on this covered in Array Iteration.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • LinkedIn
  • Live
  • StumbleUpon
  • Technorati
  • TwitThis
Written by Adam in: 03. Arrays | Tags: , , ,

No Comments »

RSS feed for comments on this post. TrackBack URL

Leave a comment

WordPress Powered, Theme by TheBuckmaker.com | Add to Technorati Favorites. | RSS and Comments RSS