fbpx

Multan Institute of Information Technology

Arrays in PHP

What is an array in PHP?

Array is a box (variable) in which you can keep many things under a single name. Meaning there is no need to create a separate variable for each value, everything is stored in a single array.

Types Of Arrays in PHP

There are 3 types of Arrays in PHP

  • Indexed array
  • Associative array
  • Multidimensional array

Indexed Array:In this every value is associated with a number (index). Counting always starts from 0.

Associative Array:In this names (keys) are given instead of numbers. This makes understanding data easier.

Multidimensional Array:This is a little advanced. In this there are other arrays inside the array. In this way complex data can also be stored easily.

Main work of Arrays

Creating an array: Using [] or array().

Taking a value: Using number in an indexed array, and using key in an associative array.

Inserting a new value: Using [] or array_push().

Deleting a value:unset() → To delete a specific value.

array_pop() → Removes the last value.

array_shift() → Removes the first value and adjusts the rest.

Built-in functions:For sorting → sort(), asort()

For searching → in_array()

For counting → count()

For merging → array_merge()

Mixed values: An array can store numbers, strings, booleans, objects and other arrays together.