Arrays in PHP

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

Article author: Andrei Olegovich