array

Description

Creates an array. Read the section on the
array type for more information
on what an array is.

Parameters

...

Syntax "index => values", separated by commas, define index
and values. index may be of type string or integer. When index is
omitted, an integer index is automatically generated, starting
at 0. If index is an integer, next generated index will
be the biggest integer index + 1. Note that when two identical
index are defined, the last overwrite the first.

Having a trailing comma after the last defined array entry, while
unusual, is a valid syntax.

Return Values

Returns an array of the parameters. The parameters can be given
an index with the => operator. Read the section
on the array type for more
information on what an array is.

Examples

The following example demonstrates how to create a
two-dimensional array, how to specify keys for associative
arrays, and how to skip-and-continue numeric indices in normal
arrays.

The script:<?php$raw = $xml = new SimpleXMLElement('File.XML');foreach($raw->Setting as $A => $B){ // Set Array From XML$Setting[(string) $B['name']] = (string) $B[0];}?>By telling the key to read the object as a string, it will let you set it.

If you need, for some reason, to create variable Multi-Dimensional Arrays, here's a quick function that will allow you to have any number of sub elements without knowing how many elements there will be ahead of time. Note that this will overwrite an existing array value of the same path.

The following function (similar to one above) will render an array as a series of HTML select options (i.e. "<option>...</option>"). The problem with the one before is that there was no way to handle <optgroup>, so this function solves that issue.

So, for example, I needed to render a list of states/provinces for various countries in a select field, and I wanted to use each country name as an <optgroup> label. So, with this function, if only a single array is passed to the function (i.e. "arrayToSelect($stateList)") then it will simply spit out a bunch of "<option>" elements. On the other hand, if two arrays are passed to it, the second array becomes a "key" for translating the first array.

Chek this out!!!. Suppose that you want to create an array like the following:<?php $arr1 = (0 => array ("customer"=>"Client 1","Item a"),1 => array ("customer"=>"Client 2","Item b") );?>Seems prety easy, but what if you want to generate it dinamically woops!!!. Imagine that you have a file with thousands of lines and each line is a purchase order from diferent clients:<?php/*function to add elements*/function addArray(&$array, $id, $var){$tempArray = array( $var => $id);$array = array_merge ($array, $tempArray);}/*The same as above but the element is an array*/function addArrayArr(&$array, $var, &$array1){$tempArray = array($var => $array1);$array = array_merge ($array, $tempArray);}/*labels of our array or heders of the file*/$keyarr = array("customer","item");/*info that may you read from a file line 1 and 2*/$valarr0 = array("Client 1","Item a");$valarr1 = array("Client 2","Item b");

/*Here we just print the info but you can insert it into a database*/echo "Customer: ".$finalarr[0]["customer"]."<br>";echo "Item: ".$finalarr[0]["item"]."<br>";echo "Customer: ".$finalarr[1]["customer"]."<br>";echo "Item: ".$finalarr[1]["item"]."<br>"; ?>

If you are creating an array with a large number of static items, you will find serious performance differences between using the array() function and the $array[] construct. For example:<?// Slower method$my_array = array(1, 2, 3, … 500);

Now a user has a blank value to select if they later decide to not provide their address in the form. The first two options will return TRUE when checked against the php function - EMPTY() after the form is submitted when processing the form

I encountered the following but didn't see it documented/reported anywhere so I'll just mentioned it here.

As written in the manual:
"When index is omitted, an integer index is automatically generated, starting at 0. If index is an integer, next generated index will be the biggest integer index + 1".

This generated index has always the largest integer used as a key so far. So adding $a[5] = 'foo'; after an $a[10] = 'bar'; will not force the next generated index to be 6 but to be 11 as 10 was the highest index encountered until here.

Anyway, the following can happen:
<?php
$max_int = 2147483647; // Max value for integer on a 32-bit system
$arr = array();

$arr[1] = 'foo'; // New generated index will be 2
$arr[ $max_int ] = 'bar'; // Caution: Next generated index will be -2147483648 due to the integer overflow!
$arr[0] = 'bar'; // The highest value should be 2147483648 but due to the i-overflow it is -2147483648 so current index 0 is larger. The new generated index therefore is 1!
$arr[] = 'failure'; // Warning: Cannot add element to the array as the next element is already occupied.
?>

In PHP 4.2.3 (and maybe earlier versions) arrays with numeric indexes may be initialized to start at a specific index and then automatically increment the index. This will save you having to write the index in front of every element for arrays that are not zero-based.

How to use array() to create an array of references rather than of copies? (Especially needed when dealing with objects.) I played around somewhat and found a solution: place & before the parameters of array() that shall be references. My PHP version is 4.3.10.

It is usually advisable to define your arrays like this:
$array = array(
'foo',
'bar',
);

Note the comma after the last element - this is perfectly legal. Moreover,
it's best to add that last comma so that when you add new elements to the
array, you don't have to worry about adding a comma after what used to be
the last element.

I wanted to be able to control the flow of data in a loop instead of just building tables with it or having to write 500 select statements for single line items. This is what I came up with thanks to the help of my PHP brother in FL. Hope someone else gets some use out it. <?

//set array variable $results = array();

//talk to the db$query = "SELECT * FROM yourtable";$result = mysql_query($query) or die(mysql_error());

Here's a cool tip for working with associative arrays-
Here's what I was trying to accomplish:

I wanted to hit a DB, and load the results into an associative array, since I only had key/value pairs returned. I loaded them into an array, because I wanted to manipulate the data further after the DB select, but I didn't want to hit the DB more than necessary.