- PHP Tutorial
- PHP - Home
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Features
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP Operators
- PHP - Operators
- PHP - Arithmatic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - Loop Types
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
- PHP Useful Resources
- PHP - Questions & Answers
- PHP - Quick Guide
- PHP - Useful Resources
- PHP - Discussion
PHP - Indexed Array
In PHP, the array elements may be a collection of key-value pairs or it may contain values only. If the array consists of values only, it is said to be an indexed array, as each element is identified by an incrementing index, starting with "0".
An indexed array in PHP may be created either by using the array() function or with the square bracket syntax.
$arr1 = array("a", 10, 9.99, true); $arr2 = ["a", 10, 9.99, true];
Each element in the array has a positional index, the first element being at index "0". The var_dump() function reveals the structured information of these arrays as −
array(4) { [0]=> string(1) "a" [1]=> int(10) [2]=> float(9.99) [3]=> bool(true) }
We can use the index to traverse the array, fetch the value at a given index or modify the value of an element in place.
Traversing an Indexed Array in PHP
Any type of PHP loop can be employed to traverse an array. If we want to use a for or while loop, we have to find the number of elements in the array with count() function and use its value as the test condition for the counted for or while loop.
Example
The following code uses a for loop to list all the elements in an indexed array.
<?php $numbers = array(10, 20, 30, 40, 50); for ($i=0; $i<count($numbers); $i++){ echo "numbers[$i] = $numbers[$i] \n"; } ?>
It will produce the following output −
numbers[0] = 10 numbers[1] = 20 numbers[2] = 30 numbers[3] = 40 numbers[4] = 50
You can also use a while or do-while loop to traverse an indexed array. Here too, we need to find the array length with count() function.
Example
The following code traverses the given indexed array in reverse order −
<?php $numbers = array(10, 20, 30, 40, 50); $i = count($numbers)-1; while ($i>=0){ echo "numbers[$i] = $numbers[$i] \n"; $i--; } ?>
It will produce the following output −
numbers[4] = 50 numbers[3] = 40 numbers[2] = 30 numbers[1] = 20 numbers[0] = 10
Accessing the Array Elements Using Index
You can access any value from an array using the array[index] syntax. The value at a specific index may be assigned with a new value. The array is thus modified in place.
Example
The following program fetches the values from an array $arr1 and places them in $arr2 in the reverse order. So the value at 0th position in $arr1 becomes the last value in $arr2.
<?php $arr1 = array(10, 20, 30, 40, 50); $size = count($arr1); for ($i=0; $i<$size; $i++){ $arr2[$size-$i-1] = $arr1[$i]; } for ($i=0; $i<$size; $i++){ echo "arr1[$i] = $$arr1[$i] arr2[$i] = $$arr2[$i] \n"; } ?>
It will produce the following output −
arr1[0] = $10 arr2[0] = $50 arr1[1] = $20 arr2[1] = $40 arr1[2] = $30 arr2[2] = $30 arr1[3] = $40 arr2[3] = $20 arr1[4] = $50 arr2[4] = $10
Traversing an Indexed Array Using "foreach" Loop
You can also use the foreach loop to iterate through an indexed array. Take a look at the following example −
<?php $arr1 = [10, 20, 30, 40, 50]; foreach ($arr1 as $val){ echo "$val \n"; } ?>
It will produce the following output −
10 20 30 40 50
Note that PHP internally treats the indexed array as an associative array, with the index being treated as the key. This fact can be verified by the var_dump() output of the array.
Example
We can unpack each element of an indexed array in the key and value variables with foreach syntax −
<?php $arr1 = [10, 20, 30, 40, 50]; foreach ($arr1 as $key => $val){ echo "arr1[$key] = $val \n"; } ?>
It will produce the following output −
arr1[0] = 10 arr1[1] = 20 arr1[2] = 30 arr1[3] = 40 arr1[4] = 50
In PHP, an array may be a mix of only values and key-value pairs. PHP assigns the index only to the values without keys.
Example
In this example, PHP assigns incrementing index to the numbers, skipping the key-value pair appearing in it.
<?php $arr1 = [10, 20, "vals" => ["ten", "twenty"], 30, 40, 50]; var_dump($arr1); ?>
It will produce the following output −
array(6) { [0]=> int(10) [1]=> int(20) ["vals"]=> array(2) { [0]=> string(3) "ten" [1]=> string(6) "twenty" } [2]=> int(30) [3]=> int(40) [4]=> int(50) }