0% found this document useful (0 votes)
0 views

PHP 06 - Arrays

The document explains PHP arrays, which are variables that can hold multiple values of different types. It categorizes arrays into three types: indexed, associative, and multidimensional, and provides examples of how to create and loop through them. Additionally, it discusses the use of associative arrays in PHP, including how to set and get values, and mentions superglobals like $_GET and $_POST as examples of associative arrays.
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)
0 views

PHP 06 - Arrays

The document explains PHP arrays, which are variables that can hold multiple values of different types. It categorizes arrays into three types: indexed, associative, and multidimensional, and provides examples of how to create and loop through them. Additionally, it discusses the use of associative arrays in PHP, including how to set and get values, and mentions superglobals like $_GET and $_POST as examples of associative arrays.
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/ 11

PHP

06 – Arrays
Arrays

Arrays are special variables that can hold multiple values. Because PHP isn’t
strict with data types, you can store values of different types per array index.
Arrays are created using the array() function.

$pasta = array('carbonara', 'spaghetti', 'pesto’);


echo $pasta[2]; // displays ‘pesto’
3 Kinds of Arrays
INDEXED ASSOCIATIVE MULTIDIMENSIONAL
Arrays with a numeric index. Arrays with named indices (keys) Arrays with arrays inside.

The $pasta example from the There is no specific order with this A two-dimensional array can be
previous slide is a good array. The name of the index must be seen as a table. Two indices will be
example of an indexed array. unique. needed to select inside.

Each element can be visited Each element can only be looped Each element can be traversed
(traversed) using a simple for through using a slightly modified through with a loop, but the kind
loop or foreach loop. foreach loop. of array will need to be considered.

The concept is similar to C++. The concept is similar to JavaScript. The concept is similar to JavaScript.
Looping through indexed
11 <body> Lines 16 and 18
12 <div class="container">
13 <h1>Pasta!</h1> This example shows the
14 <?php $pasta = array('Carbonara','Spaghetti','Pesto');?> alternate version of using a
15 <ul> foreach loop where HTML code
16 <?php foreach($pasta as $p):?> is in between PHP code.
17 <li><?php echo $p;?></li> If the size of an array is known,
18 <?php endforeach;?> a regular for loop will do. If it is
19 </ul> unknown, a foreach loop will
20 </div> work just as fine.
21 </body>
Looping
through
indexed
The screenshot on the
left shows the output
of the code in the
previous slide.
Declaring Associative Arrays
11
12
<body>
<div class="container">
Line 14
13 <h1>Pasta!</h1>
14 <?php $pasta = One way to create an
array('Carbonara'=>'Yellow','Spaghetti'=>'Red','Pesto'=>'Green');?> associative array is by explicitly
15 <ul> creating it with the array
16 <?php foreach($pasta as $p => $c):?> function.
17 <li><?php echo $p;?> has the color <?php echo $c;?></li>
Notice how the ‘=>’ operator
18 <?php endforeach;?>
associates the key to the value.
19 </ul>
20 <?php $pasta['Carbonara'] = 'White';?>
21 <p>My favorite dish has the color <?php echo
$pasta['Carbonara'];?>.</p>
22 </div>
23 </body>
Setting Values in Associative Arrays
11
12
<body>
<div class="container">
Line 20
13 <h1>Pasta!</h1>
14 <?php $pasta = An associative array’s value can
array('Carbonara'=>'Yellow','Spaghetti'=>'Red','Pesto'=>'Green');?> be accessed through its key and
15 <ul> its value changed like an
16 <?php foreach($pasta as $p => $c):?> indexed array.
17 <li><?php echo $p;?> has the color <?php echo $c;?></li>
It is important to remember
18 <?php endforeach;?>
that calling a name that doesn’t
19 </ul>
exist will cause PHP to generate
20 <?php $pasta['Carbonara'] = 'White';?>
an error.
21 <p>My favorite dish has the color <?php echo
$pasta['Carbonara'];?>.</p>
22 </div>
23 </body>
Getting Values from Associative Arrays
11
12
<body>
<div class="container">
Line 21
13 <h1>Pasta!</h1>
14 <?php $pasta = Getting values is as easy as
array('Carbonara'=>'Yellow','Spaghetti'=>'Red','Pesto'=>'Green');?> calling the right key from the
15 <ul> associative array.
16 <?php foreach($pasta as $p => $c):?> Again, it is important to
17 <li><?php echo $p;?> has the color <?php echo $c;?></li>
remember that calling the
18 <?php endforeach;?>
wrong name will cause an error.
19 </ul>
20 <?php $pasta['Carbonara'] = 'White';?>
21 <p>My favorite dish has the color <?php echo
$pasta['Carbonara'];?>.</p>
22 </div>
23 </body>
Looping through Associative Arrays
11
12
<body>
<div class="container">
Lines 16 to 18
13 <h1>Pasta!</h1>
14 <?php $pasta = Notice the modification made
array('Carbonara'=>'Yellow','Spaghetti'=>'Red','Pesto'=>'Green');?> to the foreach loop. The =>
15 <ul> operator was included to add
16 <?php foreach($pasta as $p => $c):?> the value of the array.
17 <li><?php echo $p;?> has the color <?php echo $c;?></li>
$p represents the key name, the
18 <?php endforeach;?>
name of the dish, and $c
19 </ul>
represents the value, the color
20 <?php $pasta['Carbonara'] = 'White';?>
of the dish.
21 <p>My favorite dish has the color <?php echo
$pasta['Carbonara'];?>.</p> These variable names can be
22 </div> anything allowed.
23 </body>
Associative in
Action
The following
illustrates how
Associative Arrays
work.
Notice how the color
of the Carbonara
changed in the middle
of the script’s
execution.
So is $_GET an array?
That’s right and so is $_POST. They’re associative arrays which is why they
showed errors when a key that didn’t exist was called.
They belong in a category known as Superglobals.

You might also like