PHP Arrays

PHP Arrays Tutorial will explain you what are types of Array and How to print or echo array elements in PHP? With example.

An array is a collection of values in a single variable.

If you want a variable called $state you could only hold one state name. But if you made the same variable an array, you could hold all 50 states.

Example on PHP Arrays:

print_r() is used to show the contents of an array.

The output will be:

An array is made up of a key and a value, where the key points to the value.

Types of PHP Arrays

  • Numeric array in PHP – An array with a numeric index.
  • Associative array in PHP – An array where each ID key is associated with a value.
  • Multidimensional array in PHP – An array containing one or more arrays.

Numeric Array in PHP

In a numeric array we store every component with a numeric ID key.

Example on Numeric Array in php:

The output will be:

In the above output you can see the index numbers for white, black and blue are 0, 1, 2 respectively which are numeric values and hence we call such arrays numeric arrays. Note: If index is not specified then it is numeric by default.

Associative Array in PHP

At the time we need to store components in array with some significant association other than numbers, we utilize associative array in PHP.

 Example on Associative Array in PHP:

Output will be:

Multidimensional Array in PHP

Multidimensional array is an array of arrays.

The output will be:

In the above example the array contains 3 arrays. Look at the echo, we have called two indexes. First one is array index and second one is index of the array element. Suppose you want to call ‘water’ you should mention [2][0].

Previous articlePHP switch case
Next articlePHP While Loop
I am a young Information Systems Security Engineer(CEH). I had started with penetration testing when i am 17 years old. It all started with Kali linux and Metasploit.In free time i will play with Security holes/Bug bounty/Pentesting and Hacking !!!