Anda di halaman 1dari 22

PHP Array

Array
An array stores multiple values in one single
variable.
Example:
<?php
$cars = array(Honda Civic", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " .
$cars[2] . ".";
?>
Output:
I like Honda Civic, BMW and Toyota

What is an Array?
An array is a special variable, which can
hold more than one value at a time.
If you have a list of items (a list of car
names, for example), storing the cars in
single variables could look like this:
$cars1 = Honda Civic";
$cars2 = "BMW";
$cars3 = "Toyota";

Continued..
An array can hold many values under a single
name, and you can access the values by
referring to an index number.
Create an Array in PHP
In PHP, the array() function is used to create
an array:
array();

Types of Array
In PHP, there are three types of arrays:
Indexed arrays - Arrays with a numeric
index
Associative arrays - Arrays with named
keys
Multidimensional
arrays
Arrays
containing one or more arrays

PHP Indexed Arrays


There are two ways to create indexed
arrays:
The index can be assigned automatically
(index always starts at 0), like this:
$cars = array("Volvo", "BMW", "Toyota");
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";

Continued..
The following example creates an indexed
array named $cars, assigns three elements to
it, and then prints a text containing the array
values:
<?php
$cars = array(Swift", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and "
. $cars[2] . ".";
?>
I like Swift, BMW and Toyota.

Get The Length of an Array


The count() Function:
The count() function is used to return the
length (the number of elements) of an
array:
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
?>
Output:
3

Loop Through an Indexed Array

To loop through and print all the values of an


indexed array, you could use a for loop, like this:
<?php
$cars = array(Swift", "BMW", "Toyota");
$arrlength = count($cars);
for($x = 0; $x < $arrlength; $x++) {
echo $cars[$x];
echo "<br>";
}
?>

Continued..
Output:
Swift
BMW
Toyota

PHP Associative Arrays


Associative arrays are arrays that use
named keys that you assign to them.
There are two ways to create an associative
array:
$age = array("Peter"=>"35", "Ben"=>"37",
"Joe"=>"43");
OR
$age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";

Continued
The named keys can then be used in a
script:
Example:
<?php
$age = array("Peter"=>"35", "Ben"=>"37",
"Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years
old.";
?>
Output:
Peter is 35 years old.

Loop Through an Associative


Array

To loop through and print all the values of an


associative array, you could use a foreach loop,
like this:
<?php
$age = array("Peter"=>"35", "Ben"=>"37",
"Joe"=>"43");
foreach($age as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>

Continued..
Output:
Key=Peter, Value=35
Key=Ben, Value=37
Key=Joe, Value=43

Multidimensional Arrays

A multidimensional array is an array


containing one or more arrays.
PHP understands multidimensional arrays
that are two, three, four, five, or more levels
deep.

Continued..
The dimension of an array indicates
the number of indices you need to
select an element. For a two dimensional
array you need two indices to select an
element
For a three dimensional array you need
three indices to select an element

Continued..

Name

Stock

Sold

Volvo

22

18

BMW

15

13

Saab

Land Rover

17

15

Continued..

$cars = array
(
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);

Joining Two Arrays with


array_merge()
array_merge() accepts two or more arrays and returns a
merged array combining
all their elements. In the following example, we create two
arrays, joining the
second to the first, and loop through the resultant third array:
$first = array("a", "b", "c");
$second = array(1,2,3);
$third = array_merge( $first, $second );
foreach ( $third as $val )
{
print "$val<BR>";
}

Task 11

Arrays allow you to assign multiple values to one variable. For


this PHP exercise, write an array variable of weather conditions
with the following values: rain, sunshine, clouds, hail, sleet,
snow, wind. Using the array variable for all the weather
conditions, echo the following statement to the browser:

We've seen all kinds of weather this month. At the beginning of


the month, we had snow and wind. Then came sunshine with a
few clouds and some rain. At least we didn't get any hail or sleet.

Don't forget to include a title for your page, both in the header
and on the page itself.

Task12

For this exercise, you will use a list of ten of the largest cities in the
world. (Please note, these are not the ten largest, just a selection of ten
from the largest cities.) Create an array with the following
values: Tokyo, Mexico City, New York City, Mumbai, Seoul,
Shanghai, Lagos, Buenos Aires, Cairo, London.

Print these values to the browser separated by commas, using a loop to


iterate over the array. Sort the array, then print the values to the
browser in an unordered list, again using a loop.

Add the following cities to the array: Los Angeles, Calcutta, Osaka,
Beijing. Sort the array again, and print it once more to the browser in
an unordered list.

Task13
array_push()
array_shift()
array_slice()
sort()
asort()

Anda mungkin juga menyukai