PHP print array

Home - Tutorials - Basics

This is a small tutorial how to print array in PHP. You will learn all the way you can display, print the content of an array.

Tutorial info:

Name:PHP print array
Total steps:2

Bookmark PHP print array

Step 1 - Introduction

PHP print array

Arrays are very usefull data types in PHP. Besides this they are very flexible as well. In PHP there are more ways how to print array content. Regarding what information you want to display we can make some categories.

  1. You want to print only one array element value
  2. You want to print all of the PHP array values
  3. You want to print the complete array with keys, values maybe data types
Before we print array content let's create a small array to work with.
  1. // Create a new array
  2. $colorList2[] = "red";
  3. $colorList2[] = "green";
  4. $colorList2[] = "blue";
  5. $colorList2[] = "black";
  6. $colorList2[] = "white";
Ok, now as we have the array let's see how to print it's content. First if you want to display only one value from the array you can use it as a normal variable in the following way:
  1. echo $colorList2[0];
Don't forget to use the correct key index as if you use the array name without key then it will print only the text "Array" as the actual variable type is array. However if you define a wrong key you will print nothing.

The following examples demonstartes some wrong usage:
  1. echo $colorList2;
  2. echo $colorList2[220];
  3. echo $colorList2["apple"];
However the code in the 2. and 3. line should be ok, if the array have such elements. So if you extend your array as follows then the last 2 statement will print valid values.
  1. $colorList2[220] = "yellow";
  2. $colorList2["apple"] = "apple-green";

Next Step of PHP print array

Tags: php print array, print array, php array, php, print, array

Follow phpf1 on Twitter

F1 Site Family
Java F1
JavaScript F1

Total time: 0.1436