Page 118 - Open Soource Technologies 304.indd
P. 118
Unit 7: Functions
[0] => Cleveland Notes
[1] => Loretta
[2] => Junior
)
)
Example: 2. Lets try displaying a single value from the array above:
echo “Is “ . $families[‘Griffin’][2] .
“ a part of the Griffin family?”;
The code above will output:
Is Megan a part of the Griffin family?
Arrays in PHP are implemented using hash tables, which means that accessing
a value has an average complexity of O(1).
7.8.1 Array ( ) Construct
Array( ) is one of the methods PHP provides to create and populate an array with data. It is
a PHP statement that takes your input and returns an array variable containing that input.
Although the general format never changes.
Syntax
An array can be created by the array( ) language construct. It takes as parameters any number
of comma-separated key => value pairs.
array( key => value
, ...
)
// key may only be an integer or string
// value may be any value of any type
Now that you have an idea of what an array( ) statement looks like, let’s look at how your earlier
syntax of an array can be stored as an array:
<?php
$val1 = “car”;
$mycar = array(0=>”cycle”,
1=>”byke”,
2=>$val1);
?>
LOVELY PROFESSIONAL UNIVERSITY 113