0% found this document useful (0 votes)
4 views7 pages

Chapter 3. PHP Arrays and Array Structures (1)

Chapter 3 of the document covers PHP arrays, explaining their types, creation, and manipulation. It details indexed, associative, and multidimensional arrays, including examples of how to create and loop through them. Additionally, it introduces sorting functions for arrays, demonstrating how to sort both indexed and associative arrays in various orders.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Chapter 3. PHP Arrays and Array Structures (1)

Chapter 3 of the document covers PHP arrays, explaining their types, creation, and manipulation. It details indexed, associative, and multidimensional arrays, including examples of how to create and loop through them. Additionally, it introduces sorting functions for arrays, demonstrating how to sort both indexed and associative arrays in various orders.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

ISABELA STATE UNIVERSITY Echague Main Campus

IT224 – Integrative Programming and Technologies


Chapter 3 - PHP Arrays and Array Structures
Primitivo S. Gatmen Jr, Subject Teacher

Chapter 3: PHP Arrays and Array


Structures
ARRAYS
An array stores multiple values in one single variable:

<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>

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 = "Volvo";
$cars2 = "BMW";
$cars3 = "Toyota";

However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300?
The solution is to create an array! An array can hold many values under a single name, and you can access the values
by referring to an index number.

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

Create an Array in PHP

<?php
$cars = array(“Volvo”,”BMW”,”Toyota”);

echo $cars[0]; // prints Volvo


echo $cars[1]; // prints BMW
echo $cars[2]; // prints Toyota

Get The Length of an Array

The count() function is used to return the length (the number of elements) of an array:

<?php
$cars = array("Volvo", "BMW", "Toyota");

1|Page
ISABELA STATE UNIVERSITY Echague Main Campus
IT224 – Integrative Programming and Technologies
Chapter 3 - PHP Arrays and Array Structures
Primitivo S. Gatmen Jr, Subject Teacher

echo count($cars); // prints 3


?>

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");

or the index can be assigned manually:

$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";

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("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>

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("Volvo", "BMW", "Toyota");

foreach($cars as $x) {
echo $x."<br />";
}
?>

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:

2|Page
ISABELA STATE UNIVERSITY Echague Main Campus
IT224 – Integrative Programming and Technologies
Chapter 3 - PHP Arrays and Array Structures
Primitivo S. Gatmen Jr, Subject Teacher

$age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";

3|Page
ISABELA STATE UNIVERSITY Echague Main Campus
IT224 – Integrative Programming and Technologies
Chapter 3 - PHP Arrays and Array Structures
Primitivo S. Gatmen Jr, Subject Teacher

The named keys can then be used in a script:

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "Peter is " . $age['Peter'] . " 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 />";
}
?>

or

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

foreach($age as $x => $x_value) {


print "Key=".$x.", Value=".$x_value."<br />";
}
?>

4|Page
ISABELA STATE UNIVERSITY Echague Main Campus
IT224 – Integrative Programming and Technologies
Chapter 3 - PHP Arrays and Array Structures
Primitivo S. Gatmen Jr, Subject Teacher

PHP - Multidimensional Arrays


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

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

PHP - Two-dimensional Arrays

A two-dimensional array is an array of arrays. First, take a look at the following table:

Name Stock Sold

Volvo 22 18

BMW 15 13

Saab 5 2

Land Rover 17 15
We can store the data from the table above in a two-dimensional array, like this:

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

Now the two-dimensional $cars array contains four arrays, and it has two indices: row and column.

To get access to the elements of the $cars array we must point to the two indices (row and column):

<?php
echo $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2].".<br>";
echo $cars[1][0].": In stock: ".$cars[1][1].", sold: ".$cars[1][2].".<br>";
echo $cars[2][0].": In stock: ".$cars[2][1].", sold: ".$cars[2][2].".<br>";
echo $cars[3][0].": In stock: ".$cars[3][1].", sold: ".$cars[3][2].".<br>";
?>

We can also put a for loop inside another for loop to get the elements of the $cars array (we still have to point to the
two indices):

<?php
$cars = array
(
array("Volvo",22,18),

5|Page
ISABELA STATE UNIVERSITY Echague Main Campus
IT224 – Integrative Programming and Technologies
Chapter 3 - PHP Arrays and Array Structures
Primitivo S. Gatmen Jr, Subject Teacher

array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
foreach($cars as $car ) {
foreach($car as $c ) {
echo $c." ";
}
echo $c."<br />";
}
?>

The elements in an array can be sorted in alphabetical or numerical order, descending or ascending.

PHP - Sort Functions For Arrays


In this chapter, we will go through the following PHP array sort functions:

 sort() - sort arrays in ascending order


 rsort() - sort arrays in descending order
 asort() - sort associative arrays in ascending order, according to the value
 ksort() - sort associative arrays in ascending order, according to the key
 arsort() - sort associative arrays in descending order, according to the value
 krsort() - sort associative arrays in descending order, according to the key

Sort Array in Ascending Order - sort()

The following example sorts the elements of the $cars array in ascending alphabetical order:

<?php
$cars = array("Volvo", "BMW", "Toyota");
sort($cars);
?>

The following example sorts the elements of the $numbers array in ascending numerical order:

<?php
$numbers = array(4, 6, 2, 22, 11);
sort($numbers);
?>

Sort Array in Descending Order - rsort()

The following example sorts the elements of the $cars array in descending alphabetical order:

6|Page
ISABELA STATE UNIVERSITY Echague Main Campus
IT224 – Integrative Programming and Technologies
Chapter 3 - PHP Arrays and Array Structures
Primitivo S. Gatmen Jr, Subject Teacher

<?php
$cars = array("Volvo", "BMW", "Toyota");
rsort($cars);
?>

The following example sorts the elements of the $numbers array in descending numerical order:

<?php
$numbers = array(4, 6, 2, 22, 11);
rsort($numbers);
?>

Sort Array (Ascending Order), According to Value - asort()

The following example sorts an associative array in ascending order, according to the value:

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
asort($age);
?>

Sort Array (Ascending Order), According to Key - ksort()

The following example sorts an associative array in ascending order, according to the key:

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
ksort($age);
?>

Sort Array (Descending Order), According to Value - arsort()

The following example sorts an associative array in descending order, according to the value:

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
arsort($age);
?>

Sort Array (Descending Order), According to Key - krsort()

The following example sorts an associative array in descending order, according to the key:

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
krsort($age);
?>

7|Page

You might also like