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

Article author: Andrei Olegovich

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
OOP
Namespaces in PHP
Comments in PHP
Cookies
PHP sessions
Authentication and Authorization in PHP 8
PHP linters we use
PHP errors: case studies
card_from_db(): banner card from a DB row
PHP versions: history in brief
Creating rules in .htaccess
require, require_once, include, include_once
Deduplication in PHP
PHP course: zero to hero in 50 lessons

Search this site

Subscribe to Telegram channel @aofeed to follow new articles and updates to old ones

Go to channel

@aofeed

Ask a question in the Telegram group

@aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: