PHP Array
In PHP, an array is a data structure that can store multiple values in a single variable. Each value in an array is assigned a unique index that can be used to access the value later. Arrays can be used to store a wide variety of data types, including strings, numbers, and objects.
PHP supports three types of arrays:
- Indexed arrays: An indexed array is an array where each element is assigned a numerical index, starting from 0. Indexed arrays can be created using the
array()
function or using square brackets[]
.
// Using array()
$fruits = array("apple", "banana", "orange");
// Using []
$fruits = ["apple", "banana", "orange"];
- Associative arrays: An associative array is an array where each element is assigned a string key that can be used to access the value later. Associative arrays can be created using the
array()
function or using square brackets[]
.
// Using array()
$ages = array("Peter" => 23, "John" => 32, "Mary" => 28);
// Using []
$ages = ["Peter" => 23, "John" => 32, "Mary" => 28];
- Multi-dimensional arrays: A multi-dimensional array is an array that contains one or more arrays as its elements. Multi-dimensional arrays can be created using nested
array()
functions or nested square brackets[]
.
// Using array()
$employees = array(
array("name" => "John", "age" => 32, "department" => "Sales"),
array("name" => "Mary", "age" => 28, "department" => "Marketing"),
array("name" => "Peter", "age" => 23, "department" => "IT")
);
// Using []
$employees = [ ["name" => "John", "age" => 32, "department" => "Sales"],
["name" => "Mary", "age" => 28, "department" => "Marketing"],
["name" => "Peter", "age" => 23, "department" => "IT"]
];
PHP Array functions
In PHP, an array is a data structure that stores multiple values in a single variable. Each value in an array is assigned a unique index that can be used to access the value later. Arrays can be used to store a wide variety of data types, including strings, numbers, and objects.
Here are some of the most commonly used array functions in PHP:
array()
- Creates a new array.count()
- Returns the number of elements in an array.is_array()
- Checks whether a variable is an array.sort()
- Sorts an array in ascending order.rsort()
- Sorts an array in descending order.asort()
- Sorts an array in ascending order, preserving keys.ksort()
- Sorts an array by key in ascending order.usort()
- Sorts an array using a user-defined comparison function.array_push()
- Adds one or more elements to the end of an array.array_pop()
- Removes the last element from an array.array_shift()
- Removes the first element from an array.array_unshift()
- Adds one or more elements to the beginning of an array.array_merge()
- Combines two or more arrays into a single array.array_slice()
- Extracts a portion of an array.array_search()
- Searches an array for a given value and returns the corresponding key if found.
Here are examples of how to use some of these array functions:
// array()
$colors = array("red", "green", "blue");
// count()
$colors = array("red", "green", "blue");
$numberOfColors = count($colors); // $numberOfColors is 3
// is_array()
$colors = array("red", "green", "blue");
$isAnArray = is_array($colors); // $isAnArray is true
// sort()
$numbers = array(3, 1, 4, 2);
sort($numbers); // $numbers is now [1, 2, 3, 4]
// rsort()
$numbers = array(3, 1, 4, 2);
rsort($numbers); // $numbers is now [4, 3, 2, 1]
// asort()
$ages = array("Peter" => 23, "John" => 32, "Mary" => 28);
asort($ages); // $ages is now ["Peter" => 23, "Mary" => 28, "John" => 32]
// ksort()
$ages = array("Peter" => 23, "John" => 32, "Mary" => 28);
ksort($ages); // $ages is now ["John" => 32, "Mary" => 28, "Peter" => 23]
// usort()
$fruits = array("apple", "orange", "banana", "pear");
usort($fruits, function($a, $b) {
return strlen($a) - strlen($b);
});
// $fruits is now ["pear", "apple", "orange", "banana"]
// array_push()
$colors = array("red", "green", "blue");
array_push($colors, "yellow"); // $colors is now ["red", "green", "blue", "yellow"]
// array_pop()
$colors = array("red", "green", "blue");
$lastColor = array_pop($colors); // $lastColor is "blue", $colors is now ["red", "green"]