Visit Our UNIX and Linux User Community

Linux and UNIX Man Pages

Test Your Knowledge in Computers #890
Difficulty: Easy
In sed, the REGEX dot (.) matches exactly one character.
True or False?
Linux & Unix Commands - Search Man Pages

array(3) [php man page]

ARRAY(3)								 1								  ARRAY(3)

array - Create an array

array array ([mixed $...]) DESCRIPTION
Creates an array. Read the section on the array type for more information on what an array is. PARAMETERS
o $... - 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 big- gest 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. Example #1 array(3) example <?php $fruits = array ( "fruits" => array("a" => "orange", "b" => "banana", "c" => "apple"), "numbers" => array(1, 2, 3, 4, 5, 6), "holes" => array("first", 5 => "second", "third") ); ?> Example #2 Automatic index with array(3) <?php $array = array(1, 1, 1, 1, 1, 8 => 1, 4 => 1, 19, 3 => 13); print_r($array); ?> The above example will output: Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 13 [4] => 1 [8] => 1 [9] => 19 ) Note that index '3' is defined twice, and keep its final value of 13. Index 4 is defined after index 8, and next generated index (value 19) is 9, since biggest index was 8. This example creates a 1-based array. Example #3 1-based index with array(3) <?php $firstquarter = array(1 => 'January', 'February', 'March'); print_r($firstquarter); ?> The above example will output: Array ( [1] => January [2] => February [3] => March ) As in Perl, you can access a value from the array inside double quotes. However, with PHP you'll need to enclose your array between curly braces. Example #4 Accessing an array inside double quotes <?php $foo = array('bar' => 'baz'); echo "Hello {$foo['bar']}!"; // Hello baz! ?> NOTES
Note array(3) is a language construct used to represent literal arrays, and not a regular function. SEE ALSO
array_pad(3), list(3), count(3), range(3), foreach, The array type. PHP Documentation Group ARRAY(3)

Check Out this Related Man Page


The SplFixedArray class

The SplFixedArray class provides the main functionalities of array. The main differences between a SplFixedArray and a normal PHP array is that the SplFixedArray is of fixed length and allows only integers within the range as indexes. The advantage is that it allows a faster array implementation. CLASS SYNOPSIS
SplFixedArray SplFixedArrayIteratorArrayAccessCountable Methods o public SplFixedArray::__construct ([int $size]) o public int SplFixedArray::count (void ) o public mixed SplFixedArray::current (void ) o publicstatic SplFixedArray SplFixedArray::fromArray (array $array, [bool $save_indexes = true]) o public int SplFixedArray::getSize (void ) o public int SplFixedArray::key (void ) o public void SplFixedArray::next (void ) o public bool SplFixedArray::offsetExists (int $index) o public mixed SplFixedArray::offsetGet (int $index) o public void SplFixedArray::offsetSet (int $index, mixed $newval) o public void SplFixedArray::offsetUnset (int $index) o public void SplFixedArray::rewind (void ) o public int SplFixedArray::setSize (int $size) o public array SplFixedArray::toArray (void ) o public bool SplFixedArray::valid (void ) o public void SplFixedArray::__wakeup (void ) EXAMPLES
Example #1 SplFixedArray usage example <?php // Initialize the array with a fixed length $array = new SplFixedArray(5); $array[1] = 2; $array[4] = "foo"; var_dump($array[0]); // NULL var_dump($array[1]); // int(2) var_dump($array["4"]); // string(3) "foo" // Increase the size of the array to 10 $array->setSize(10); $array[9] = "asdf"; // Shrink the array to a size of 2 $array->setSize(2); // The following lines throw a RuntimeException: Index invalid or out of range try { var_dump($array["non-numeric"]); } catch(RuntimeException $re) { echo "RuntimeException: ".$re->getMessage()." "; } try { var_dump($array[-1]); } catch(RuntimeException $re) { echo "RuntimeException: ".$re->getMessage()." "; } try { var_dump($array[5]); } catch(RuntimeException $re) { echo "RuntimeException: ".$re->getMessage()." "; } ?> The above example will output: NULL int(2) string(3) "foo" RuntimeException: Index invalid or out of range RuntimeException: Index invalid or out of range RuntimeException: Index invalid or out of range PHP Documentation Group SPLFIXEDARRAY(3)

Featured Tech Videos