PHP Arrays

5/5 - (1 vote)

An array is a collection of related values (a list of items) such as names of cities.

PHP Arrays are inspired by Perl. They have all the benefits of Phyton Dictionaries, but they can also maintain the order of the items in the array.

How to create an empty array?

$numbers = [];
$numbers = array();
$numbers = (array) null;

How to create an array?

To create an array use the function array() like:

Numeric Arrays

Numeric Arrays are arrays that use only numeric keys, called as numeric, indexed or ordered arrays.

// An array called $capital with numeric keys 
$capital = array(0 =>'Paris', 1 =>'Berlin', 2 =>'London');

Associative Arrays

Associative arrays are arrays that use string keys, when You assign item to them.
This type of array is also referred to as a hash or map.

An array called $capital with string keys

$capital = array('France'=>'Paris', 'Germany'=>'Berlin', 'UK'=>'London');

An array called $capital with string and numeric keys

$capital = array('France'=>'Paris', 200 =>'Berlin', 'UK'=>'London');

The PHP engine treats arrays with numeric keys and arrays with string keys identically.

Creating an array using short form

The short form to create an array is to use a pair of square brackets for each item (called the short array syntax), like:

Create an array called $capital using the short array syntax

$capital = ['France'=>'Paris', 200 =>'Berlin', 'UK'=>'London'];

Creating a Numeric Array

You can create an array specifying only the list of values instead of key/value pairs.

In this case the PHP engine will assigns automatically a numeric key to each value.

The keys start with 0 and increase one by one for each element.

// An array called $capital with string keys 
$capital = array('Paris','Berlin', 'London');
or
$capital = ['Paris','Berlin', 'London'];

Accesing arrays elements

echo $capital[1]; //Berlin

Add elements to an array

To add an element to the array use empty brackets.

//Add the item 'Amsterdam' to $capital array
$capital = ['Paris','Berlin', 'London'];
$capital[] = 'Amsterdam';

print_r($capital); 
//Result: Array ( [0] => Paris [1] => Berlin [2] => London [3] => Amsterdam ) 

As you can see the numeric key for ‘Amsterdam’ is 3, that’s one more than the biggest numeric key that already exists in the array, in our case 2.

If the array doesn’t exist yet, the empty brackets will add the new item with a key of 0.

Create an array with no elements (an empty array) and add elements later

$capital[] = 'Paris';
$capital[] = 'Berlin';
$capital[] = 'London';

print_r($capital); 
//Result: Array ( [0] => Paris [1] => Berlin [2] => London) 

Get the length of an array

To get the length of an array use the count() function.

$capital = array('France'=>'Paris', 'Germany'=>'Berlin', 'UK'=>'London');
echo count($capital); 

//The result: 3

Looping through arrays

in PHP. the easiest way to iterate through each element of an array is to use foreach() function.

Ussing foreach() with associative arrays

$capital = array('France'=>'Paris', 'Germany'=>'Berlin', 'UK'=>'London');

foreach($capital as $key => $value){
	echo "The capital of $key is $value<br/>";
}

/*Result:
The capital of France is Paris
The capital of Germany is Berlin
The capital of UK is London*/

You can use whatever variable names you want for the $key and $value.

Ussing foreach() with numeric arrays

$capital = ['Paris','Berlin', 'London'];

foreach($capital as $city){
	echo "$city<br/>";
}

/*Result:
Paris
Berlin
London*/

PHP Array functions

Read more here about PHP array functions

HTTP & PHP Arrays

Hello there!

I hope you find this post useful!

I'm Mihai, a programmer and online marketing specialist, very passionate about everything that means online marketing, focused on eCommerce.

If you have a collaboration proposal or need helps with your projects feel free to contact me. I will always be glad to help you!

subscribe youtube

Leave a Comment

WebPedia.net