PHP
  1. PHP intro
  2. PHP basics
  3. PHP variables
  4. PHP functions
  5. PHP conditions
  6. PHP loops
  7. PHP arrays
  8. PHP classes & objects
  9. PHP strings
  10. PHP forms
  11. PHP entities
  12. PHP files
  13. PHP include files
  14. PHP date & time
  15. PHP cookies
  16. PHP databases
  17. PHP sessions
  18. PHP summary

PHP arrays

Arrays are a very important concept in PHP as well as many other programming languages. An array is a special type of variable which can hold a list of information.

This tutorial focuses on:

The necessity of arrays

Imagine you are writing a program that has three variables which contain the names of cars, your code might look like this:

Example:
<?php $car1 = "Toyota Camry"; $car2 = "Honda Accord"; $car3 = "Mitsubishi Galant"; ?>

With Arrays, it is much simpler. An array gives you the ability to group together related variables into one set. Arrays are special variables which hold lists of information. Therefore, instead of the code above you can declare a 'cars' array:

Example:
<?php $cars[0] = "Toyota Camry"; $cars[1] = "Honda Accord"; $cars[2] = "Mitsubishi Galant"; ?>

Numeric arrays

The numeric array is an array which associates an index number to a certain value.

Creating a numeric array

A numeric array is created by initializing a variable which will be the name of the array together with the array function

Syntax:
$arrayName = array();
Example:
<?php $animals = array(); ?>

Creating an array this way, you can add as many values as you want into it. Alternatively, you can specify an array to be of a particular size.

Example:
<?php $animals = array(4); ?>

In the above example, an array named "animals" that can store 4 elements is created.

Adding values to a numeric array

Once you create a numeric array, you can add values to it by specifying the array name, an index in the array and a value to be placed in that index.

Example:
$arrayName[index] = value;
Example:
<?php $animals[0] = "turtle"; $animals[1] = "frog"; $animals[2] = "rabbit"; $animals[3] = "giraffe"; ?>

In the above example, an array named "animals" is declared. Four values are added to it.

NOTE: Array indexes start at 0. So the 1st element of an array would be at index 0, the 5th element of an array would be at index at 4, and so on.

Another way to assign values to an array involves doing so during the array's initialization.

Example:
$animals = array("turtle", " frog", "rabbit", "giraffe");

Accessing a numeric arrays elements

To access a numeric arrays elements, refer to the array with the appropriate index number in brackets.

Example:
<?php $animals[0] = "turtle"; $animals[1] = "frog"; $animals[2] = "rabbit"; $animals[3] = "giraffe"; //print the last element of the animals array print "The last element in the animals array is " . $animals[3]; ?>
Output:
The last element in the animals array is giraffe

Modifying a numeric arrays elements

To modify a numeric arrays elements, refer to the array with the appropriate index number in brackets of the value you want to change and set it to the new value.

Example:
<?php $animals[0] = "turtle"; $animals[1] = "frog"; $animals[2] = "rabbit"; $animals[3] = "giraffe"; print "The old value of animals[1]: " . $animals[1]; print "<br />The old value of animals[3]: " . $animals[3]; //change the value of animals[1] and animals[3] $animals[1] = "porcupine"; $animals[3] = "pony"; print "<br />The new value of animals[1]: " . $animals[1]; print "<br />The new value of animals[3]: " . $animals[3]; ?>
Output:
The old value of animals[1]: frog The old value of animals[3]: giraffe The new value of animals[1]: porcupine The new value of animals[3]: pony

Associative arrays

The associative array is an array which associates an index value (instead of a number) to a certain value.

Creating an associative array

An associative array is created by initializing a variable which will be the name of the array together with the array function.

Syntax:
$arrayName = array();
Example:
<?php $car = array(); ?>

Creating an array this way, you can add as many values as you want into it. Alternatively, you can specify an array to be of a particular size.

Example:
<?php $car = array(4); ?>

In the above example, an array named "cars" that can store 4 elements is created.

Adding values to an associative array

Once you create an associative array, you can add values to it by specifying the array name, an index value in the array and a value to be placed in that index.

Syntax:
$arrayName["index_value"] = value;
Example:
<?php $car["company"] = "Mercub"; $car["model"] = "Grand Vehicle"; $car["year"] = 1989; $car["mileage"] = 150,000; ?>

Another way to assign values to an associative array involves doing so during the array's initialization.

Example:
<?php $car = array("company"=>"Mercub", "model"=>"Grand Vehicle", "year"=>"1989", "mileage"=>"150,000"); ?>

Accessing an associative arrays elements

To access an associative arrays elements, refer to the array with the appropriate index value in brackets.

Example:
<?php $car["company"] = "Mercub"; $car["model"] = "Grand Vehicle"; $car["year"] = 1989; $car["mileage"] = 150000; //print the first element of the cars array print "The first element in the cars array is " . $car["company"]; ?>
Output:
The first element in the cars array is Mercub

Modifying an associative arrays elements

To modify an associative arrays elements, refer to the array with the appropriate index value in brackets of the value you want to change and set it to the new value.

Example:
<?php $car["company"] = "Mercub"; $car["model"] = "Grand Vehicle"; $car["year"] = 1989; $car["mileage"] = 150000; print "Car company: " . $car["company"]; print "<br />Car year: " . $car["year"]; //change the value of $car["company"] $car["company"] = "Bord"; //change the value of $car["year"]; $car["year"] = 1990; print "<br />Car company: " . $car["company"]; print "<br />Car year: " . $car["year"]; ?>
Output:
Car company: Mercub Car year: 1989 Car company: Bord Car year: 1990
© Copyright 2013-2014 Landofcode.com
Terms of use | Privacy policy | Copyright information