Utilizor
Contact Us

PHP Arrays

Detailed look at arrays.

PHP Arrays

An array stores multiple values in one single variable:

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

In PHP, there are three types of arrays:

  • Indexed arrays - Arrays with a numeric index
  • Associative arrays - Arrays with named keys
  • Multidimensional arrays - Arrays containing one or more arrays

Get Array Length

The count() function is used to return the length (the number of elements) of an array.

Sorting Arrays

The elements in an array can be sorted in alphabetical or numerical order, descending or ascending.

  • sort() - sort arrays in ascending order
  • rsort() - sort arrays in descending order
  • asort() - sort associative arrays in ascending order, according to the value
  • ksort() - sort associative arrays in ascending order, according to the key
  • arsort() - sort associative arrays in descending order, according to the value
  • krsort() - sort associative arrays in descending order, according to the key