
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP Indexed Array
Definition and Usage
A comma separated sequence of values only instead of key=>value pairs. Each element in such collection has a unique positional index starting from 0. Hence, it is called Indexed array.
Indexed Array object can be initialized by array() function as well as assignment by putting elements inside square brackets [].
Syntax
//Indexed array using array() function $arr=array(val1, val2,val3,..); //Indexed array using assignment method $arr=[val1, val2, val3,..];
An element in the array can be of any PHP type. We can access an element from the array by its index with following syntax −
$arr[index];
PHP Version
Use of square brackets for assignment of array is available since PHP 5.4
Following example uses square brackets to create an indexed array
Example
<?php $arr=[10, "ten",10.0, 1.0E1]; var_dump($arr); ?>
Output
This will produce following result −
array(4) { [0]=> int(10) [1]=> string(3) "ten" [2]=> float(10) [3]=> float(10) }
This Example uses array() function to create indexed array
Example
<?php $arr=array(10, "ten",10.0, 1.0E1); var_dump($arr); ?>
Output
This will produce following result −
array(4) { [0]=> int(10) [1]=> string(3) "ten" [2]=> float(10) [3]=> float(10) }
We can traverse the array elements using foreach loop as well as for loop as follows −
Example
<?php $arr=array(10, "ten",10.0, 1.0E1); //using for loop. Use count() function to determine array size. for ($i=0;$i < count($arr); $i++){ echo $arr[$i] . " "; } echo "
"; //using foreach loop foreach($arr as $i){ echo $i . " "; } ?>
Output
This will produce following result −
10 ten 10 10 10 ten 10 10
This Example shows modify value at certain index using square brackets. To add new element, keep square brackets empty so that next available integer is used as index
Example
<?php $arr=array(10, "ten",10.0, 1.0E1); //modify existing element using index $arr[3]="Hello"; //add new element using next index $arr[]=100; for ($i=0; $i< count($arr); $i++){ echo $arr[$i]; } ?>
Output
This will produce following result −
10 ten 10 Hello 100
- Related Articles
- Absolute Difference of even and odd indexed elements in an Array (C++)?
- Indexed collections in JavaScript
- Absolute Difference of even and odd indexed elements in an Array in C++?
- JavaScript Sum odd indexed and even indexed elements separately and return their absolute difference
- PHP Array Operators
- PHP Associative Array
- PHP Multidimensional Array.
- Are arrays zero indexed in C#?
- array() function in PHP
- Rearrange an array such that every odd indexed element is greater than its previous in C++
- Sort php multidimensional array by sub-value in PHP
- How to convert PHP array to JavaScript array?
- PHP: Remove object from array
- Is the primary key automatically indexed in MySQL?
- Binary Indexed Tree or Fenwick Tree in C++?
