Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

Arrays

Example

$cars = array("Volvo", "BMW", "Toyota");

What is an Array?

An array is a unique variable that can store multiple values under one name, and you can access these values using an index number or key.

PHP Array Types

In PHP, there are three types of arrays:

  1. Indexed arrays – Arrays that use numeric indices.
  2. Associative arrays – Arrays that use named keys.
  3. Multidimensional arrays – Arrays that contain one or more arrays

Working With Arrays

In this tutorial, you will learn how to work with arrays, including:

  • Creating Arrays
  • Accessing Arrays
  • Updating Arrays
  • Adding Items to Arrays
  • Removing Items from Arrays
  • Sorting Arrays

Array Items

Array items can be of any data type.

The most common types are strings and numbers (int, float), but array items can also include objects, functions, or even other arrays.

You can have various data types within the same array.

Example

Array items with four different data types:

$myArr = array("Volvo", 15, ["apples", "bananas"], myFunction);

Array Functions

The true power of PHP arrays lies in the built-in array functions, such as the count() function, which is used to count array items.

Example

How many items are present in the $cars array

$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);