- PHP Basics
- PHP Home
- PHP Environment Setup
- PHP Getting Started
- PHP Basic Syntax
- PHP echo
- PHP print
- PHP echo Vs print
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Variable Scope
- PHP gettype()
- PHP Constants
- PHP Operators
- PHP Program Control
- PHP Decision Making
- PHP if-elseif-else
- PHP switch
- PHP Loops
- PHP for Loop
- PHP while Loop
- PHP do-while Loop
- PHP foreach Loop
- PHP break & continue
- PHP Popular Topics
- PHP Arrays
- PHP print_r()
- PHP Strings
- PHP Functions
- PHP References
- PHP Object Oriented
- PHP Object Oriented
- PHP Classes & Objects
- PHP Member Variable
- PHP Member Function
- PHP Encapsulation
- PHP Data Abstraction
- PHP Inheritance
- PHP Constructor Destructor
- PHP Polymorphism
- PHP Web Developments
- PHP Web Developments
- PHP GET & POST
- PHP Read Requested Data
- PHP File Handling (I/O)
- PHP File Handling (I/O)
- PHP fopen() | Open File
- PHP Create a File
- PHP fwrite() | Write to File
- PHP fread() | Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP fclose() | Close File
- PHP unlink() | Delete File
- PHP Append to File
- PHP copy() | Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP rename() | Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP mkdir() | Create Directory
- PHP rmdir() | Remove Directory
- PHP glob() | Get Files/Directories
- PHP basename() | Get filename
- PHP dirname() | Get Path
- PHP filemtime()
- PHP file()
- PHP Advanced
- PHP Cookies
- PHP Sessions
- PHP Send Emails
- PHP Serialization
- PHP Namespaces
- PHP File Upload
- PHP Date and Time
- PHP Image Processing
- PHP Regular Expression
- PHP Predefined Variables
- PHP Error Handling
- PHP Debugging
- PHP and MySQLi Tutorial
- PHP and MySQLi Home
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Fetch Record
- PHP MySQLi Update Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP connect_errno
- PHP connect_error
- PHP query()
- PHP fetch_row()
- PHP fetch_assoc()
- PHP fetch_array()
- PHP free_result()
- PHP error
- PHP prepare()
- PHP bind_param()
- PHP execute()
- PHP fetch()
- PHP store_result()
- PHP num_rows
- PHP bind_result()
- PHP get_result()
- PHP mysqli_result Class
- PHP Error Constants
- PHP mysqli_driver()
- PHP Misc
- PHP error_reporting()
- PHP Escape Special Characters
- PHP htmlspecialchars()
- PHP new
- PHP header()
- PHP getallheaders()
- PHP empty()
- PHP isset()
- PHP unset()
- PHP exit()
- PHP exit Vs break
- PHP include()
- PHP require()
- PHP include() Vs require()
- PHP AJAX & XML
- PHP AJAX
- PHP XML
- PHP File Handling Functions
- PHP abs()
- PHP Test
- PHP Online Test
- Give Online Test
- All Test List
PHP Arrays
PHP arrays are used to store multiple values in a single variable.
Create an Array in PHP
You have to use a function named array() to create an array in PHP. Here is the general form to create an array in PHP:
array();
Initialize an Array in PHP
Here are some sample ways to initialize an array in PHP.
// initialize an empty array in PHP $array_var = array(); // shorthand notation of initializing an empty array in PHP $array_var = []; // initialize a simple array with some values in PHP $tutorial_var = array('PHP', 'Array', 'One dimensional Array', 'Multidimensional Array'); // shorthand notation of initializing array with some values in PHP $tutorial_var = ['PHP', 'Array', 'One dimensional Array', 'Multidimensional Array']; // initialize simple associative array in PHP $tutorial_var = array( 'first' => 'PHP', 'second' => 'Array', 'third' => 'One Dimensional Array', 'fourth' => 'Multidimensional Array' ); // initialize and set an array with key and value in PHP $tutorial_var['second'] = 'Array'; // shorthand notation of initializing an array with some values in PHP $tutorial_var = [ 'first' => 'PHP', 'second' => 'Array', 'third' => 'One Dimensional Array', 'fourth' => 'Multidimensional Array' ];
Get Length of an Array in PHP
To get the length of an array in PHP, just use count() function, returns the length (number of element) of the array. Here is an example, uses count() function to find the length of the array in PHP:
<!DOCTYPE html> <html> <head> <title>Getting Array Length Example in PHP</title> </head> <body> <?php $cars = array("Audi", "BMW", "Mercedes"); echo count($cars); ?> </body> </html>
Here is the output of the above PHP script:

PHP Arrays Example
Here is an example of arrays in PHP:
<!DOCTYPE html> <html> <head> <title>PHP Array Example</title> </head> <body> <?php // array example in PHP $cars = array('Audi', 'BMW', 'Mercedes'); echo "I like these cars :<br/>"; echo $cars[0].", ".$cars[1].", and ".$cars[2]; ?> </body> </html>
Here is the output of the above PHP script:

Below is one more example demonstrating array in PHP.
<!DOCTYPE html> <html> <head> <title>PHP Array Example</title> </head> <body> <?php $tot = 5; for($i=0; $i<$tot; $i++) { $array_var[$i] = $i; } for($i=0; $i<$tot; $i++) { echo $array_var[$i]."<br/>"; } ?> </body> </html>
The above PHP array example code will produce the output given below:

Let's take another example on array in PHP
<!DOCTYPE html> <html> <head> <title>PHP Array Example</title> </head> <body> <?php $num = 2; for($i=1; $i<=10; $i++) { $array_var[$i] = $num * $i; } for($i=1; $i<=10; $i++) { echo $array_var[$i]."<br/>"; } ?> </body> </html>
Here is the sample output of the above array example code

Types of Arrays in PHP
There are following three types of arrays available in PHP:
- Associative Arrays
- Indexed Arrays
- Multidimensional Arrays
PHP Indexed Arrays
There are two ways to create an indexed arrays in PHP. The index can be assigned automatically like this:
$cars = array("Audi", "BMW", "Mercedes");
Or, you can assign index manually like this:
$cars[0] = "Audi"; $cars[1] = "BMW"; $cars[2] = "Mercedes";
Note - Index always starts from 0
Here is an example, creates an indexed arrays named $cars, and assigns the three elements to it, and then prints a text containing the array values:
<!DOCTYPE html> <html> <body> <?php $cars = array("Audi", "BMW", "Mercedes"); echo "I like these cars :<br/>"; echo $cars[0].", ".$cars[1]." and ".$cars[2]; ?> </body> </html>
The above PHP script will produce the following output:

Loop to display PHP Indexed Array
To loop through and print all the values of an indexed array in PHP, you could use a for loop as shown in this example:
<!DOCTYPE html> <html> <head> <title>Loop to display indexed array in PHP</title> </head> <body> <?php $cars = array("Audi", "BMW", "Mercedes"); $arrlength = count($cars); echo "I like these cars :<br/>"; for($x = 0; $x < $arrlength; $x++) { echo $cars[$x]; if($x==1) { echo ", and "; continue; } if($x!=2) { echo ", "; } } ?> </body> </html>
Here is the sample output produced by the above indexed array example code in PHP.

PHP Indexed Array Example
Below is an example that demonstrates the indexed array in PHP.
<!DOCTYPE html> <html> <head> <title>PHP Indexed Array Example</title> </head> <body> <?php $array_var = array("Java", "C", "C++", "PHP", "OS", "Networking", "everything"); $array_length = count($array_var); for($i=0; $i<$array_length; $i++) { if($i==6) { echo "I just love to read ".$array_var[$i]." on CodesCracker<br/>"; } else { echo "I just love to read <b>".$array_var[$i]."</b> on CodesCracker<br/>"; } } ?> </body> </html>
The output produced by the above indexed array example code is shown below:

PHP Associative Arrays
Associative arrays in PHP, are arrays that uses named keys that you assign to them. There are two ways to create an associative arrays in PHP. Here is the first way to create an associative arrays in PHP:
$age = array("Anoop"=>"18", "Harsh"=>"19", "Abhay"=>"20");
Here is the second way to create associative arrays in PHP:
$age['Anoop'] = "18"; $age['Harsh'] = "19"; $age['Abhay'] = "20";
The named keys can then be used in a script. Here is an example:
<!DOCTYPE html> <html> <body> <?php $age = array("Anoop"=>"18", "Harsh"=>"19", "Abhay"=>"20"); echo "Anoop " . $age['Anoop'] . " years old."; ?> </body> </html>
It will display the following result:

Loop to display PHP Associative Arrays
Let's look at the following example:
<!DOCTYPE html> <html> <head> <title>Loop to display associative array in PHP</title> </head> <body> <?php $age = array("Anoop"=>"18", "Harsh"=>"19", "Abhay"=>"20"); foreach($age as $x => $x_value) { echo "Name=".$x.", Age=".$x_value; echo "<br/>"; } ?> </body> </html>
It will display the following output:

PHP Associative Array Example
Here is an example illustrating associative array in PHP.
<!DOCTYPE html> <html> <head> <title>PHP Associative Array Example</title> </head> <body> <?php $array_var = array( "1"=>"Java", "2"=>"C", "3"=>"C++", "4"=>"PHP", "5"=>"OS", "6"=>"Networking", "7"=>"everything" ); $array_length = count($array_var); for($i=1; $i<=$array_length; $i++) { if($i==7) { echo "I just love to read ".$array_var[$i]." on CodesCracker<br/>"; } else { echo "I just love to read <b>".$array_var[$i]."</b> on CodesCracker<br/>"; } } ?> </body> </html>
Below is the demo output produced by the above associative array example code in PHP.

PHP Multidimensional Arrays
A multi-dimensional 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. Values in the multi-dimensional array are accessed using multiple index.
PHP Multidimensional Arrays Example
Let's look at the following example where we create a two dimensional array to store marks of three students in three subjects:
<html> <head> <title>PHP Multidimensional Array Example</title> </head> <body> <?php // initializing multidimensional array in PHP $array_var_marks = array( "Anoop" => array( "Physics" => 89, "Maths" => 93, "Chemistry" =>88 ), "Harsh" => array( "Physics" => 91, "Maths" => 94, "Chemistry" => 96 ), "Abhay" => array( "Physics" => 86, "Maths" => 89, "Chemistry" => 90 ) ); // accessing multidimensional array in PHP echo "Marks of Anoop in Physics: "; echo $array_var_marks['Anoop']['Physics']."<br/>"; echo "Marks of Harsh in Maths: "; echo $array_var_marks['Harsh']['Maths']."<br/>"; echo "Marks of Abhay in Chemistry: "; echo $array_var_marks['Abhay']['Chemistry']."<br/>"; ?> </body> </html>
Here is the output of this PHP script:

« Previous Tutorial Next Tutorial »