Arrays in PHP
In this lesson, we will learn about arrays in PHP with example code. Arrays are data structures that are used to store and manipulate data in a program. We will discuss topics such as array syntax, indexing, and sorting.
Array Syntax
An array is a data structure that stores an ordered list of values. PHP arrays have a specific syntax that must be used when declaring and manipulating them. Here is an example of an array in PHP:
$my_array = array(1, 2, 3, 4, 5);
Indexing
Arrays can be indexed using numerical or associative keys. Numerical keys are integers starting from 0, while associative keys are strings. Here is an example of an associative array in PHP:
$my_assoc_array = array("one" => 1, "two" => 2, "three" => 3);
Sorting
Arrays can be sorted using various sorting algorithms. PHP provides several built-in sorting functions for sorting arrays. Here is an example of the sort() function in PHP:
sort($my_array);