Chapter 7 Week 9: Working With Arrays
Chapter 7 Week 9: Working With Arrays
Prepared by:
Ryan B. Azur
PHP
Key Skills and Concepts
Understand the different array types supported by PHP
Process array contents with the foreach loop
Use arrays with Web forms
Sort, merge, add, modify, and split arrays using PHP’s
built-in functions
Work with dates and times
Arrays
Storing Data in Arrays
Array variables are “special,” because they can hold
more than one value at a time.
<?php
// define array
$fruits = array('apple', 'banana', 'pineapple', 'grape');
?>
Arrays
Storing Data in Arrays
PHP also supports a slightly different type of array, in which
index numbers are replaced with user-defined strings, or “keys.”
Here’s a revised version of the preceding example, which uses
keys instead of index numbers:
<?php
Associative Array - The keys of
// define array
the array must be
$fruits = array(
'a' => 'apple', unique; each key references a
'b' => 'banana', single value, and the key-value
'p' => 'pineapple',
relationship is expressed
'g' => 'grape' through the => symbol.
);
?>
Arrays
Assigning Array Values
PHP’s rules for naming array variables are the same as those for
regular variables.
Once you’ve decided a name for your array, there are two ways
of assigning values to it. The first is the method you saw in the
preceding section, where the values are all assigned at once,
separated with commas. This method creates a standard,
numberindexed array.
<?php
// define array
$cars = array(
'Ferrari',
'Porsche',
'Jaguar',
'Lamborghini',
'Mercedes'
);
?>
Arrays
Assigning Array Values
The second way to create such an array is to set values
individually using index notation.
Here’s an example, which is equivalent to the preceding one:
<?php
// define array
$cars[0] = 'Ferrari';
$cars[1] = 'Porsche';
$cars[2] = 'Jaguar';
$cars[3] = 'Lamborghini';
$cars[4] = 'Mercedes';
?>
Question:
In all these examples, you’ve explicitly specified the
array index for each assignment statement. What if I
don’t know what the correct index is? Can I make PHP
automatically assign the next available array index to
my value?
To automatically assign the next available index to an
array value, omit the index number
in your array assignment statement, as you see here:
<?php
// define array
$cars[] = 'Ferrari';
$cars[] = 'Lamborghini';
?>
Question:
You can also use both these techniques with
associative arrays. To assign all the values of such an
array in a single statement, set a key for each value and
link the two using the => connector, remembering to
separate each key-value pair with commas.
<?php
// define array
$data = array(
'username' => 'john',
'password' => 'secret',
'host' => '192.168.0.1'
);
?>
Question:
You can also assign values to keys one by one, as in this
next example:
<?php
// define array
$data['username'] = 'john';
$data['password'] = 'secret';
$data['host'] = '192.168.0.1';
?>
Question:
To access a value from an array in a script, simply use
the array name and index/key in an expression and
PHP will replace it with its value when the script is
executed, as with any normal variable. <?php
// define array
$data = array(
'username' => 'john',
'password' => 'secret',
'host' => '192.168.0.1'
);
// use array value
echo 'The password is: ' . $data['password'];
?>
Arrays
Modifying Array Values
Modifying an array value is as simple as modifying any other
variable value: simply assign a new value to the element using
either its index or its key. Consider the following example, which
modifies the second element of the $meats array to hold the
value 'turkey' instead of 'ham':
<?php
// define array
$meats = array(
'fish',
'chicken',
'ham',
'lamb'
);
// change 'ham' to 'turkey'
$meats[2] = 'turkey';
?>
Arrays
Modifying Array Values
To remove an element from an array, use the unset() function on
the corresponding key or index:
<?php
// define array
$meats = array(
'fish',
'chicken',
'ham',
'lamb'
);
// remove 'fish'
unset($meats[0]);
?>
Question:
If I remove an element from the middle of an array,
what happens to the values on either side?
<?php
// define array
$cities = array('London', 'Paris', 'Madrid', 'Los Angeles',
'Bombay', 'Jakarta');
// iterate over array
// print each value
for ($i=0; $i<count($cities); $i++) {
echo $cities[$i] . "\r\n";
}
?>
Arrays
The foreach loop
With a foreach loop, each time the loop runs, the current array
element is assigned to a temporary variable, which can then be
processed in any way you please—printed, copied to another variable,
used in a calculation, and so on. Unlike a for loop, a foreach loop
doesn’t use a counter; it automatically “knows” where it is in the
array, and it moves forward continuously until it reaches the end of
the array, at which point it automatically halts.
<?php
// define array
$cities = array('London', 'Paris', 'Madrid', 'Los Angeles', 'Bombay', 'Jakarta');
// iterate over array
// print each value
foreach ($cities as $c) {
echo "$c \r\n";
}
?>
Arrays
Array Basic – Source Code
Html file
Arrays
Array Basic – Source Code
PHP file
Arrays
Array Basic – Source Code
Php file
Arrays
Array Sort– Source Code
Html file
Arrays
Array Sort– Source Code
PHP file
Arrays
Array Sort– Source Code
Php file
Arrays
Array Basic – Source Code
Php file
Arrays
Load Array from a file – Source Code
Html file
Arrays
Finding Text – Source Code
Html file
Arrays
Regular Expression – Source Code
Html File
Arrays
Finding Text in a File – Source Code