Declaration
explicit location |
<?php $printer[0] = "Laserjet 2001"; $printer[1] = "LMP1724fast";
|
implicit location |
<?php $printer[] = "Laserjet 2001"; $printer[] = "LMP1724fast";
|
Associative Arrays
<?php
$printer['R&B PRINTERS']= "Laserjet 2002";
$printer['Jackson Printers'] = "J2001Laserjet";
print_r($printer);
//result
array (
'R&B PRINTERS' => "Laserjet 2002",
'Jackson Printers' => "J2001Laserjet",
)
?>
|
with associative arrays, you can reference the items in an array by name rather than by number.
Multidimensional Arrays
<?php
$products = array(
'pens' => array(
'ball' => "Ball Point",
'hilite' => "Highlighters",
'marker' => "Markers"),
'misc' => array(
'tape' => "Sticky Tape",
'glue' => "Adhesives",
'clips' => "Paperclips"
)
);
|
|
|
Assignment using the array Keyword
`<?php $p1 = array("China", "Russia", "Austria", "Turkey"); $p2 = array ( 'china' => "Beijing", 'Russia' => "Moscow" , 'Austria' => "Vienna", 'Turkey' => "Ankara" );
|
The for..each as loop
<?php
$paper = array("Copier", "Inkjet", "Laser", "Photo");
$j = 0;
foreach($paper as $item)
{
echo "$j: $item<br>";
++$j;
}
?>
|
|
|
Functions
Functions |
is_array() |
echo (is_array($products)) ? "Is an array" : "Is not an array";
|
count() |
echo count($products);
|
sort() |
sort($products); sort($products, SORT_NUMERIC); sort($products, SORT_STRING);
|
shuffle() |
|
explode() |
<?php $temp = explode(' ', "This is a sentence with seven words"); print_r($temp); ?>
|
extract() |
|
compact()
<?php
$planet = "Gallifrey";
$system = "Gridlock";
$constellation = "Kasterborous";
$contact = compact 'planet', 'system', 'constellation');
|
use compact, the inverse of extract, to create
an array from variables and their values.
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment