PHP Array tutorial - Creating an array

Home - Tutorials - Basics

In this tutorial I will show you how to work with arrays in PHP. You will learn how to create, sort or print an array. Besides this we will touch on multidimensional arrays as well.

Tutorial info:

Name:PHP Array tutorial
Total steps:7

Bookmark PHP Array tutorial

Step 2 - Creating an array

PHP Array tutorial

There are more ways to create an array in PHP. Maybe the most easiest way to create our color list array is the following:

  1. $colorList = array("red","green","blue","black","white");
n other sollution is to initialise array elements one-by-one as follows:
  1. $colorList[0] = "red";
  2. $colorList[1] = "green";
  3. $colorList[2] = "blue";
  4. $colorList[3] = "black";
  5. $colorList[4] = "white";
If you don't want to bother about numbering - I will explain it later - you can create your array as this:
  1. $colorList[] = "red";
  2. $colorList[] = "green";
  3. $colorList[] = "blue";
  4. $colorList[] = "black";
  5. $colorList[] = "white";
As you can see this is almost similar to the other but here we didn't write any number between the square brackets. In this case PHP makes the numbering internally from 0 to 4. This function is the so called auto-incremented keys.

Previous Step of PHP Array tutorialNext Step of PHP Array tutorial

Tags: array, php array, sort array, create array, print array, array length

Follow phpf1 on Twitter

F1 Site Family
Java F1
JavaScript F1

Total time: 0.1392