Learn PHP Arrays by Examples

Tags: ,

In this post, I will use examples to show how to use PHP arrays. I will first describe the concept of PhP arrays, how to create an array, how to get access to and update the values in an array, and how to loop a php array.  I also use examples to show how to load a csv file into a php array and describe some of the useful functions to process an array. 

What is PHP Array

An array in PHP is actually an ordered map that associates values to keys. This type is optimized for several different use cases.  It can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and more.

The key of an Array can either be an integer or a string, but the value can be of any type.  For example, the array values can be other arrays, trees and even multidimensional arrays.

How to create an PHP array

An array can be created using the array() constructor. It takes any number of comma-separated key =>value pairs as arguments. For example,

Additionally the following key casts will occur:

  • Strings containing valid integers will be cast to the integer type. E.g. the key “8” will actually be stored under 8. On the other hand “08” will not be cast, as it isn’t a valid decimal integer.
  • Floats are also cast to integers, which means that the fractional part will be truncated. E.g. the key 8.7 will actually be stored under 8.
  • Bools are cast to integers, too, i.e. the key true will actually be stored under 1 and the key false under 0.
  • Null will be cast to the empty string, i.e. the key null will actually be stored under “”.
  • Arrays and objects can not be used as keys. Doing so will result in a warning: Illegal offset type.

See the following example to show the cast of keys in PHP Array

As all the keys in the above example are cast to 1,  it overwrites the previous key, so the last assigned value “dd” is the only one left over.

PhP does not distinguish Indexed and Associative Arrays

In php, there is not distinguish between indexed and associative arrays, so both integer and string keys can occur in PHP arrays at the same time.

See the following example for an php array with bother integer and string keys.

The output is :

The Keys of PHP arrays can be optional

The key of PHP arrays is even optional. If it is not specified, PHP will use the increment of the largest previously used integer key.

PhP Indexed Array

The following code shows an example to build an indexed array.

The following example shows that It is possible to specify the key only for some elements and leave it out for others:

The last value “d” was assigned the key 7, as the largest integer key before that was 6.

PHP Accessing array elements

The following example shows how to access php array elements. 

 

Set values to a PHP array

The following example show another way to build an array and set values to it.

We first create an empty array by using $names = array();  Then we add four values to the array. As the first key is not specified, PHP will use 0 as the default key for the first item. 

The resulted array is like this:

0=> “Lei”,
1=> “Jim”,
2=> “Jack”,
3=> “Tom”

To have different numbers for the keys, we can specify the key using the bracket. 

Now the array will look like this:

1=> “Lei”,
2=> “Jack”,
3=> “Tom”,
4=> “Jim”

PHP change Array or Delete Array

We can change the value of php array for a specific Key. To remove a key/value pair, just call unset() function on it.
 

Php Array for loop

The following examples shows how to for loop a PhP Array. In the loop, we can simply print out the value, as well as update the value. 

We can use for loop to fill up an Array. For example:

 

PHP Converting to array 

For these types: integer, float, string, boolean and resource, converting a value to an array will create an array with a single element with index zero and the value of the scalar which was converted. This means, (array)$scalarValue is exactly the same as array($scalarValue).

PhP create one based array

It is easy to create one based array in PHP. we just put the key of the first pair as 1. 

Some examples to use PHP arrays

Put items from a directory into an PHP Array

The following examples show how to traverse the current directory and put the file names into an array.

Load a CSV file into an Array

Suppose we have a csv file, and we want to load it into an array. The following code shows how to accomplish this task;

Pages: 1 2