How many types of array are there in PHP?

There are three types of array in PHP:

  • Indexed array: an array with a numeric key.
  • Associative array: an array where each key has its specific value.
  • Multidimensional array: an array containing one or more arrays within itself.

In PHP, there are three main types of arrays:

  1. Indexed arrays: These are arrays where each element is assigned a numeric index starting from 0.

Example:

php
$indexedArray = array("apple", "banana", "orange");
  1. Associative arrays: In associative arrays, elements are assigned a specific key rather than a numeric index. The key-value pairs can be of any data type.

Example:

php
$associativeArray = array("fruit1" => "apple", "fruit2" => "banana", "fruit3" => "orange");
  1. Multidimensional arrays: These are arrays within arrays. They can be a combination of indexed and associative arrays.

Example:

php
$multiDimensionalArray = array(
array("name" => "John", "age" => 30),
array("name" => "Jane", "age" => 25),
array("name" => "Doe", "age" => 40)
);

When answering in an interview setting, it’s good to mention these three types and perhaps provide a brief example or explanation of each to demonstrate understanding.