PHP Arrays

An array it is a single variables that stores multiple values.

Two dimensional Array

<?php

// ---------------
// Basic Structure
// ---------------
$cars=array("Volvo","BMW","Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
//Output: I like Volvo, BMW and Toyota.

// ---------------
// Associative array
// ---------------

$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
// Output: Peter is 35 years old

// ---------------
// Count - Get the lenght of an array
// ---------------
$cars=array("Volvo","BMW","Toyota");
echo count($cars); // Output: 3

// ---------------
// Loop through and indexed array
// ---------------
$cars=array("Volvo","BMW","Toyota");
$arrlength=count($cars);

for($x=0;$x<$arrlength;$x++)
  {
  echo $cars[$x];
  echo "<br>";
  }
// Output: Volvo BMW Toyota

// ---------------
// Loop through an Associative array
// ---------------
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");

foreach($age as $x=>$x_value)
  {
  echo "Key=" . $x . ", Value=" . $x_value;
  echo "<br>";
  }
// Output: Key=Peter, Value=35 Key=Ben, Value=37 Key=Joe, Value=43

?>

Sorting Arrays (Ordinamento degli Array)

<?php

// ---------------
// sort() - sort arrays in ascending order
// ---------------
$cars=array("Volvo","BMW","Toyota");
sort($cars);

$clength=count($cars);
for($x=0;$x<$clength;$x++)
   {
   echo $cars[$x];
   echo "<br>";
   } 
// Output: BMW Toyota Volvo

// ---------------
// rsort() - sort arrays in descending order
// ---------------
$cars=array("Volvo","BMW","Toyota");
rsort($cars);

$clength=count($cars);
for($x=0;$x<$clength;$x++)
   {
   echo $cars[$x];
   echo "<br>";
   }
// Output: Volvo Totota BMW

// ---------------
// asort() - sort associative arrays in ascending order, according to the value (NOT THE KEY - KEY IS PETER VALUE IN 35)
// ---------------
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
asort($age);

foreach($age as $x=>$x_value)
    {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
    }
// Output: Peter Ben Joe

// ---------------
// arsort() - sort associative arrays in descending order, according to the value (NOT THE KEY - KEY IS PETER VALUE IN 35)
// ---------------
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
arsort($age);

foreach($age as $x=>$x_value)
    {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
    }
Output: Joe Ben Peter

// ---------------
// ksort() - sort associative arrays in ascending order, according to the key (NOT VALUE!!! - KEY IS PETER VALUE IN 35)
// ---------------
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
ksort($age);

foreach($age as $x=>$x_value)
    {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
    }
// Output: Ben Joe Peter

// ---------------
// krsort() - sort associative arrays in descending order, according to the key (NOT VALUE!!! - KEY IS PETER VALUE IN 35)
// ---------------
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
krsort($age);

foreach($age as $x=>$x_value)
    {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
    }
Output: Peter Joe Ben

?>

Multidimensional Array

A multidimensional array is an array containing one or more arrays.

<?php
$families = array   // Multidimensional Array
  (
  "Griffin"=>array  // First Array
  (
  "Peter",
  "Lois",
  "Megan"
  ),
  "Quagmire"=>array // Second Array
  (
  "Glenn"
  ),
  "Brown"=>array    // Third Array
  (
  "Cleveland",
  "Loretta",
  "Junior"
  )
  );
echo "Is " . $families['Griffin'][2] . " a part of the Griffin family?";
// Statement: multidimensional array name['first array name'][first array index]
// Output: Is Megan a part of the Griffin family?
?>