Developer
PHP
PHP TUTORIALPHP IntroductionPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP VariablesPHP ConstantsPHP OutputPHP Data TypesPHP 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 stores 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 find "Orange". What will you do? In this way it 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 a integer or string. Values can be any data type.

PHP Declaring an Array

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

  • An array can be declared using array() function.
  • An array can be declared wrapping 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.

  • Indexed Arrays: Arrays with numeric indexes (keys).
  • Associative Arrays: Arrays with named keys.
  • Multidimensional arrays: Arrays of arrays

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 ››

PHP Associative Arrays

Indexed arrays have named keys.

There are two ways to declare associative arrays.

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

PHP Multidimensional Arrays

Multidimensional arrays are arrays of arrays.

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

PHP Multidimensional Array Example


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

Run Example ››

Note: You will learn about multidimensional arrays in a later chapter of this tutorial.

PHP Get the Length of an Array

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, 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 value of the current element of array 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 count() Example


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

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

Run Example ››

foreach with associative arrays

PHP count() 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 ››
Advertisement
1
83