PHP Arrays: Types of arrays (indexed, associative, multidimensional), array functions.
In PHP, arrays are versatile data structures that can store multiple values in a single variable. PHP provides different types of arrays to manage data effectively, each with specific characteristics and use cases.
Types of Arrays
1. Indexed Arrays
Indexed arrays are numeric arrays where each element is assigned an index starting from 0 by default.
These arrays are useful when you need to store a list of items and access them by their position.
$colors = array("Red", "Green", "Blue");// Access elementsecho $colors[0]; // Outputs: Red
2. Associative Arrays
Associative arrays use named keys instead of numeric indices to store values.
They are suitable when you want to associate specific keys with values, like in dictionaries.
$ages = array("John" => 25, "Jane" => 30, "Doe" => 22);// Access elementsecho $ages["Jane"]; // Outputs: 30
3. Multidimensional Arrays
Multidimensional arrays contain one or more arrays as elements.
They are ideal for representing complex data structures, such as a matrix or a list of records.
$students = array("John" => array("age" => 25, "grade" => "A"),"Jane" => array("age" => 30, "grade" => "B"),);// Access elementsecho $students["John"]["grade"]; // Outputs: A
Common Array Functions
Common Array Functions
PHP offers a wide variety of built-in functions to manipulate arrays effectively. Here are some frequently used ones:
Adding/Removing Elements
array_push($array, $value)
: Adds one or more elements to the end of an array.array_pop($array)
: Removes the last element from an array.array_unshift($array, $value)
: Adds one or more elements to the beginning of an array.array_shift($array)
: Removes the first element of an array.
Merging and Combining
array_merge($array1, $array2)
: Merges two or more arrays.array_combine($keys, $values)
: Combines two arrays, using one for keys and the other for values.
Searching
in_array($value, $array)
: Checks if a value exists in an array, returns true if found.array_search($value, $array)
: Searches the array for a value and returns the corresponding key if successful.array_key_exists($key, $array)
: Checks if a specific key exists in an array.
Sorting
sort($array)
: Sorts an indexed array in ascending order.rsort($array)
: Sorts an indexed array in descending order.asort($array)
: Sorts an associative array in ascending order, according to the value.ksort($array)
: Sorts an associative array by key in ascending order.
Array Mapping and Filtering
array_map($callback, $array)
: Applies a callback function to each element of an array.array_filter($array, $callback)
: Filters elements of an array using a callback function.array_reduce($array, $callback, $initial)
: Reduces an array to a single value using a callback function.
Counting and Slicing
count($array)
: Returns the number of elements in an array.array_slice($array, $offset, $length)
: Extracts a portion of an array.array_splice($array, $offset, $length, $replacement)
: Removes a portion of an array and replaces it with another array.
Example Usage
$numbers = array(1, 2, 3, 4, 5);array_push($numbers, 6); // Adds 6 at the end$filtered = array_filter($numbers, function($num) {return $num % 2 == 0;});// $filtered is [2, 4, 6]
Summary
Indexed arrays store elements with numeric indexes.
Associative arrays store elements with custom keys.
Multidimensional arrays store complex structures.
PHP provides various array functions for manipulating arrays, making data management simpler and more efficient.