PHP Arrays

PHP Arrays
5 (100%) 1 vote

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

Creating 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.

// 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'];

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.

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

Leave a Reply

Your email address will not be published. Required fields are marked *