PHP Tutorial – PHP Arrays

PHP Tutorial – PHP Arrays

An array is a special variable that can store multiple values in a single variable.

The elements of the array are accessed via an index number, with the first element starting at zero.

An array can be created by the array() language construct.

Kinds of array in PHP:

  • Numeric array – A numeric array stores each element with a numeric ID key.
  • Associative array – An array where each ID key is associated with a value

Numeric array

A numeric array stores each element with a numeric ID key.

In an indexed array, the keys are numeric and starts with 0.

There are 2 ways to write a numeric array:

Here is an example of writing a numeric array that automatically assigns the index (the index starts at 0):

$names = array("Piolo","Sam","Rolan");

In the above example $name is our array variable that contains the values Piolo, Sam and Rolan.

In this example it automatically assigns the index in the values of our array starting form 0.

The second way of writing a numeric array is that we assign the index manually. Here is an example:

$names[0]="Piolo";
$names[1]="Sam";
$names[2]="Rolan";

The index can be used in a script:

<?php
$names[0] = "Piolo";
$names[1] = "Sam";
$names[2] = "Rolan";
echo $names[1] . " and " . $names[2] ." are ". $names[0] . "'s friends";
?>

Associative array

An associative array, each key is unique, and corresponds to a single value within the array
Keys may be made up of any string of characters, including control characters.

There are also two ways of writing an associative array.

Here is an example of an associative array:

$talent_fee = array(”Piolo"=>5500, ”Sam"=>2500, "Rolan”=19000>);

In this example $ages is an array variable. The keys here are names of Piolo, Sam and Rolan and the value would be their respective talent fee. The => symbol is used to indicate the association between a key and its value.

Here is an example of writing an associative array in a different way:

$talent_fee['Piolo'] = "5500";
$talent_fee['Sam'] = "2500";
$talent_fee['Rolan'] = "19000";

Example script:

<?php
$talent_fee['Piolo'] = "5500";
$talent_fee['Sam'] = "2500";
$talent_fee['Rolan'] = "19000";
echo "Talent fee of Piolo is " . $talent_fee['Piolo'];
?>

PHP-Lesson: PHP Arrays

An array is a special variable that can store multiple values in a single variable.

The elements of the array are accessed via an index number, with the first element starting at zero.

An array can be created by the array() language construct.

Kinds of array in PHP:

  • Numeric array – A numeric array stores each element with a numeric ID key.
  • Associative array – An array where each ID key is associated with a value

Numeric array
A numeric array stores each element with a numeric ID key.

In an indexed array, the keys are numeric and starts with 0.

There are 2 ways to write a numeric array:

Here is an example of writing a numeric array that automatically assigns the index (the index starts at 0):

$names = array("Piolo","Sam","Rolan");

In the above example $name is our array variable that contains the values Piolo, Sam and Rolan.

In this example it automatically assigns the index in the values of our array starting form 0.

The second way of writing a numeric array is that we assign the index manually. Here is an example:

$names[0]="Piolo";
$names[1]="Sam";
$names[2]="Rolan";

The index can be used in a script:

<?php
$names[0] = "Piolo";
$names[1] = "Sam";
$names[2] = "Rolan";
echo $names[1] . " and " . $names[2] ." are ". $names[0] . "'s friends";
?>

Associative array

 

An associative array, each key is unique, and corresponds to a single value within the array
Keys may be made up of any string of characters, including control characters.

There are also two ways of writing an associative array.

Here is an example of an associative array:

$talent_fee = array(”Piolo"=>5500, ”Sam"=>2500, "Rolan”=19000>);

In this example $ages is an array variable. The keys here are names of Piolo, Sam and Rolan and the value would be their respective talent fee. The => symbol is used to indicate the association between a key and its value.

Here is an example of writing an associative array in a different way:

$talent_fee['Piolo'] = "5500";
$talent_fee['Sam'] = "2500";
$talent_fee['Rolan'] = "19000";

Example script:

<?php
$talent_fee['Piolo'] = "5500";
$talent_fee['Sam'] = "2500";
$talent_fee['Rolan'] = "19000";
echo "Talent fee of Piolo is " . $talent_fee['Piolo'];
?>
, , , , , , , ,

Post navigation

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.