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

PHP Arrays

PHP arrays can be numerically indexed or associative, and can contain multiple dimensions. They can be constructed and accessed in various ways, and useful functions include print_r, count, sort, and explode. Superglobals provide access to GET, POST, COOKIE, and SESSION variables.

Uploaded by

usama sarfraz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

PHP Arrays

PHP arrays can be numerically indexed or associative, and can contain multiple dimensions. They can be constructed and accessed in various ways, and useful functions include print_r, count, sort, and explode. Superglobals provide access to GET, POST, COOKIE, and SESSION variables.

Uploaded by

usama sarfraz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

PHP Arrays

CSC 242, Web Programming


PHP Arrays

In PHP an array is an ordered map that associates keys with


values
PHP has two types of arrays
Numerically indexed arrays use integers as keys
Associative arrays typically use strings as keys
Constructing Arrays with Integer Keys

push consecutive values to the array


<?php
$a[] = "one";
$a[] = "two";
$a[] = "three";
?>
using explicit indices
<?php
$a[0] = "one";
$a[1] = "two";
$a[2] = "three";
?>
Associative Arrays

Associative arrays map keys other than integers to values


<?php
$b['one'] = 1;
$b['two'] = 2;
$b['three'] = 3;
?>
If multiple elements are declared with the same key, then only
the value of the last element is used
Key Casts

A string containing an integer will be cast to an integer


A float is cast to an integer
A bool is cast to an integer
The null value will be cast to an empty string
Arrays and objects cannot be used as keys
Printing Arrays

The print_r function prints a human readable representation


of an array
An element of an array can be used as a variable for string
interpolation purposes
echo "$a[0]";
If the key is not an integer then the array element must be
surrounded by curly braces for string interpolation
echo "${b['one']}";
The array keyword

Arrays can be assigned with the array keyword


<?php
$a = array("one", "two", "three");

$b = array('one' => 1,
'two' => 2,
'three' => 3);
?>
The foreach loop

The foreach loop can be used to iterate through the values of


an array
Arrays with integer keys:
foreach($a as $element) {
echo "$element";
}
Associative arrays:
foreach($b as $key => $value) {
echo "$key: $value";
}
Multidimensional Arrays

The value of a key can be an array


$tic_tac_toe = array(
array('X', ' ', 'O'),
array('O', 'O', 'X'),
array('X', 'O', ' ')
);
Some Useful Array Functions

is_array checks if a variable is an array type


count returns the number of elements in an array
sort performs an in-place sort of an array
explode converts a string into an array
Superglobals

Superglobals are predefined variables that are provided by the


PHP environment
$_GET: variables passed to the current script via the HTTP
GET method
$_POST: variables passed to the current script via the HTTP
POST method
$_COOKIE: variables passed to the current script via HTTP
cookies
$_SESSION: session variables available to the current script

You might also like