DONATE Hyvor Developer
PHP
PHP TUTORIALPHP IntroductionPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP CommentsPHP VariablesPHP Variable ScopePHP ConstantsPHP OutputPHP Data TypesPHP Type CastingPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP StringsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP RegexRegex IntroductionRegex PCRE SyntaxPHP PREG FunctionsPHP FormsPHP Forms IntroductionPHP Forms CreatingPHP Forms SecurityPHP Forms ValidationPHP Forms Required InputsPHP Forms StickyPHP Forms Advanced ValidationPHP Forms Finishing

PHP Arrays

Arrays are variables that store multiple values.

PHP Why Arrays?

If you have a set of data (assume names of fruits) to store in variables what will you do? Like this?


<?php

$fruit1 = 'Apple';
$fruit2 = 'Banana';
$fruit3 = 'Orange';
$fruit4 = 'Mango';

(NOT) Nice! Now you need to find the variable which has the value "Orange". What will you do? In this way, it is not that hard. But, if you had thousands of fruits how will you find that? Arrays solve this!

Arrays can have key/value pairs. The key can either be an integer or string. Values can be any data type.

PHP Declaring an Array

In PHP, there are two ways to declare an array. (We have already discussed this in the data types chapter)

  • An array can be declared using the array() function.
  • An array can be declared wrapped with [ and ].

PHP Arrays Example


<?php
$array = array('Apple', 'Banana', 'Orange', 'Mango');
var_dump($array);

$array = ['Apple', 'Banana', 'Orange', 'Mango'];
var_dump($array);

Run Example ››

PHP Types of Arrays

There are three types of arrays in PHP.

PHP Indexed Arrays

Indexed arrays have numeric indexes or keys.

This is not a new thing for you. The array we created in the last step is an indexed array.


<?php
$fruits = ['Apple', 'Banana', 'Orange', 'Mango'];

Also, you can manually add the indexes.


<?php
$fruits[0] = 'Apple';
$fruits[1] = 'Banana';
$fruits[2] = 'Orange';
$fruits[3] = 'Mango';

Tip: Indexes starts from 0, not 1.

You can refer an element of the array as: $arrayName[index].

PHP Indexed Array Example


<?php
$fruits = ['Apple', 'Banana', 'Orange', 'Mango'];

echo "First Fruit is $fruits[0]" . '<br>';  
echo "Second Fruit is $fruits[1]" . '<br>';
echo "Third Fruit is $fruits[2]" . '<br>';
echo "Forth Fruit is $fruits[3]" . '<br>';

Run Example ››

Adding New Element to an Indexed Array

We can add a new element to the end of an array as follows.

PHP Indexed Array - Adding New Element


<?php
$fruits = ['Apple', 'Banana', 'Orange', 'Mango'];

// add new elements to the end
$fruits[] = 'Pears';
$fruits[] = 'Watermelon';

var_dump($fruits);

Run Example ››

PHP Associative Arrays

Indexed arrays have named keys.

There are two ways to declare associative arrays.

  1. An associative array can be declared using the array() function or []
  2. 
    <?php
    $age = array(
    	'Joe' => 22,
    	'Adam' => 25,
    	'David' => 30
    );
    
    // or 
    
    $age = [
    	'Joe' => 22,
    	'Adam' => 25,
    	'David' => 30
    ];
    
    
    
  3. An associative array can be declared manually by adding keys.
  4. 
    <?php
    $age['Joe'] = 22;
    $age['Adam'] = 25;
    $age['David'] = 30;
    
    

In this example, we will save the age of persons. (name as the key and age as the value)

PHP Associative Array Example


<?php
$age = array(
	'Joe' => 22,
	'Adam' => 25,
	'David' => 30
);
$age['Peter'] = 22;
$age['Christina'] = 25;

var_dump($age);

Run Example ››

PHP Multidimensional Arrays

Multidimensional arrays are arrays of arrays.

In PHP, you can store an array as the value of an array element.


<?php
$people = [
	'Joe' => [
		'age' => 22,
		'country' => 'USA'
	],
	'Adam' => [
		'age' => 25,
		'country' => 'United Kingdom'
	], 
	'David' => [
		'age' => 30,
		'country' => 'France'
	]
];

PHP Getting the Length of an Array

The count() function is used to get the length of an Array in PHP.

PHP count() Example


<?php
$fruits = ['Apple', 'Banana', 'Orange', 'Mango'];
echo count($fruits); // will return 4

echo '<br>';

$age = array(
	'Joe' => 22,
	'Adam' => 25,
	'David' => 30
);
echo count($age); // will return 3;

Run Example ››

PHP Foreach Loop

In PHP, the foreach loop is used to loop through arrays.

Foreach Syntax

foreach ($array as $key => $value) { code to execute }

Note: Here, $array is the variable that contains the array. In each loop iteration, the name of the key of the current element will be assigned to $key and value to $value.

foreach with Indexed Arrays

In indexed arrays, => $value part is omitted in the foreach loop.

PHP foreach with Indexed Arrays Example


<?php
$fruits = ['Apple', 'Banana', 'Orange', 'Mango'];

// foreach loop
foreach ($fruits as $fruit) {
	echo $fruit . '<br>';
}

Run Example ››

foreach with Associative Arrays

PHP foreach with Associative Arrays Example


<?php
$people = [
	'Joe' => 22,
	'Adam' => 25,
	'David' => 30
];

foreach ($people as $name => $age) {
	echo "My name is $name, and age is $age" . '<br>';	
}

Run Example ››
1
219