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

6 PHP Manual

This document discusses arrays in PHP. It begins by defining an array as a way to store multiple similar data elements rather than having many similarly named variables. There are three types of arrays: numeric, associative, and multidimensional. Numeric arrays use numeric indexes to store elements, associative arrays use named indexes, and multidimensional arrays can store arrays within arrays. Examples are given for each array type showing how to declare and access elements. The document concludes with an application example using checkboxes and arrays to store selected options, and exercises for the reader to create an array with personal details and add/check names.

Uploaded by

miraguro gamit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views7 pages

6 PHP Manual

This document discusses arrays in PHP. It begins by defining an array as a way to store multiple similar data elements rather than having many similarly named variables. There are three types of arrays: numeric, associative, and multidimensional. Numeric arrays use numeric indexes to store elements, associative arrays use named indexes, and multidimensional arrays can store arrays within arrays. Examples are given for each array type showing how to declare and access elements. The document concludes with an application example using checkboxes and arrays to store selected options, and exercises for the reader to create an array with personal details and add/check names.

Uploaded by

miraguro gamit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

CHAPTER 5

ARRAY

OBJECTIVES:

At the end of the chapter, the students are expected to:

 Know the different types of arrays in PHP.


 Know on how to use array in PHP.
 Develop applications that use arrays.
What is an array?

When working with PHP, sooner or later, you might want to create many similar variables.

Instead of having many similar variables, you can store the data as elements in an array.

Each element in the array has its own ID so that it can be easily accessed.

There are three different kind of arrays:

 Numeric array - An array with a numeric ID key


 Associative array - An array where each ID key is associated with a value
 Multidimensional array - An array containing one or more arrays

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

There are different ways to create a numeric array.

Example 1

In this example the ID key is automatically assigned:

$names = array("Peter","Quagmire","Joe");

Example 2

In this example we assign the ID key manually:

$names[0] = "Peter";
$names[1] = "Quagmire";
$names[2] = "Joe";

The ID keys can be used in a script:

<?php
$names[0] = "Peter";
$names[1] = "Quagmire";
$names[2] = "Joe";
echo $names[1] . " and " . $names[2] .
" are ". $names[0] . "'s neighbors";
?>
The code above will output:

Quagmire and Joe are Peter's neighbors

Associative Arrays
An associative array, each ID key is associated with a value.

When storing data about specific named values, a numerical array is not always the best way to
do it.

With associative arrays we can use the values as keys and assign values to them.

Example 1

In this example we use an array to assign ages to the different persons:

$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);

Example 2

This example is the same as example 1, but shows a different way of creating the array:

$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";

The ID keys can be used in a script:

<?php
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
echo "Peter is " . $ages['Peter'] . " years old.";
?>

The code above will output:

Peter is 32 years old.


Multidimensional Arrays

In a multidimensional array, each element in the main array can also be an array. And each
element in the sub-array can be an array, and so on.

Example

In this example we create a multidimensional array, with automatically assigned ID keys:

$families = array
(
"Griffin"=>array
(
"Peter",
"Lois",
"Megan"
),
"Quagmire"=>array
(
"Glenn"
),
"Brown"=>array
(
"Cleveland",
"Loretta",
"Junior"
)
);

The array above would look like this if written to the output:

Array
(
[Griffin] => Array
(
[0] => Peter
[1] => Lois
[2] => Megan
)
[Quagmire] => Array
(
[0] => Glenn
)
[Brown] => Array
(
[0] => Cleveland
[1] => Loretta
[2] => Junior
)
)
Example 2

Lets try displaying a single value from the array above:

echo "Is " . $families['Griffin'][2] .


" a part of the Griffin family?";

The code above will output:

Is Megan a part of the Griffin family?


ARRAY APPLICATION EXAMPLE:

<?php

$message = '';

if(isset($_POST['SubmitBtn'])){ // check if Submit Button (SubmitBtn) is clicked.

foreach($_POST['Sport'] as $value){ //loop on each checkbox(Sport) that is checked and declare it as $value

$selectedSport = $selectedSport . $value . ', '; //concatenate the value of $value to $selectedSport

$message .= 'You selected ' . $selectedSport; $message = rtrim($message, ', ');

?>

<html><body>

<form action="" method="post">What is your favorite sports?<br>

<input type="checkbox" name="Sport[]" value="Basketball">Basketball<br>

<input type="checkbox" name="Sport[]" value="Volleyball">Volleyball <br>

<input type="checkbox" name="Sport[]" value="Bowling"> Bowling<br>

<input type="submit" name="SubmitBtn" value="Submit" /><br>

<?php echo $message; ?>

</form>

</body></html>
Exercises:
INSTRUCTION: MAKE A PHP PROGRAM THAT WILL EXECUTE THE FF.
1. WRITE YOUR NAME, YEAR AND COURSE, EMAIL ADD. ON AN
ARRAY.
2. OUTPUT THE CONTENT OF THE ARRAY BY READING IT LINE
BY LINE.
3. ACCEPT NAME INPUT, IF THE INPUTTED NAME EXISTS ON
THE ARRAY DISPLAY “NAME ALREADY EXISTS”. OTHERWISE,
WRITE THE INPUTTED NAME ON THE ARRAY.

You might also like