Arrays in PHP

Contents
Introduction
Create Array
Access Array Element
Go Through All Elements
Get Array Length
Access Array Element by its Index
Dictionary

Introduction

Create Array

Example

<?php $myArray = array(1,2,3,4)

To access array's element run

echo $myArray[0];

1

Go throught all elements

<?php $arr = array(1, 2, 3, 4); foreach ($arr as &$value) { echo $value; echo '<br>'; unset($value); }

1
2
3
4

Get Array's Length

<?php $my_array = array("one", "two", "three"); array_length = count($my_array); echo($array_length);

3

Access Element by its Index

<?php $my_array = array("one", "two", "three"); $indexed_array = array_values($my_array); echo$indexed_array[1];

two

Dictionaries

It is possible to create dictionaries with array()

<?php $myArray = array( 0 => 'Big', 1 => 'Small', 2 => 'Up', 3 => 'Down' );

Access Array's element

echo $myArray[0];

Big

<?php $myArray = array( 'a' => 'Big', 'b' => 'Small', 'c' => 'Up', 'd' => 'Down' );

To Access Array's element

echo $myArray['b'];

Small

Related Articles
Development with PHP
Arrays in PHP
Date and Time in PHP - basics
How to display time of multiple timezones in PHP
How to add variable to url in PHP
json_decode
How to get screen size with PHP
Call function from another file
Premature end of chunk coded message body: closing chunk expected
Generate unique random numbers with PHP
Check your HTTP_USER_AGENT
Compare two dates
PHP 8 Classes
Comments in PHP
Cookies
PHP sessions
Authentication and Authorization in PHP 8
Banner Image

Search on this site

Subscribe to @aofeed channel for updates

Visit Channel

@aofeed

Feedbak and Questions in Telegram

@aofeedchat