What is Array in php?
In PHP, an array is a data structure that stores a collection of elements, each identified by one or more keys. Arrays can store elements of different types, including numbers, strings, and objects.
There are three types of arrays in PHP:
Numeric arrays: These arrays use integers as keys. The keys are automatically assigned in a sequential order starting from 0.
Associative arrays: These arrays use strings as keys. You can specify the keys yourself when you add elements to the array.
Multidimensional arrays: These arrays contain one or more arrays as elements. They are used to represent data in a more structured way.
Here is an example of how to create and use an array in PHP:
<?php
// Create an array with three elements
$numbers = array(1, 2, 3);
// Access an element of the array
echo $numbers[0]; // Outputs: 1
// Add an element to the array
$numbers[] = 4;
// Loop through the array
foreach ($numbers as $number) {
echo $number;
}
// Outputs: 1234
// Create an associative array
$employee = array(
"name" => "John Smith",
"age" => 30,
"title" => "Manager"
);
// Access an element of the array using a string key
echo $employee["name"]; // Outputs: John Smith
// Create a multidimensional array
$users = array(
array(
"name" => "Alice",
"age" => 25
),
array(
"name" => "Bob",
"age" => 35
)
);
// Access an element of a nested array
echo $users[1]["name"]; // Outputs: Bob
PHP has a number of functions that can be used to manipulate arrays. Here are a few examples:
array_keys()
: This function returns an array of all the keys in an associative array. For example:
$fruits = array('apple' => 'red', 'banana' => 'yellow', 'orange' => 'orange');
$keys = array_keys($fruits);
print_r($keys);
The output of this code will be:
Array ( [0] => apple [1] => banana [2] => orange )
array_values()
: This function returns an array of all the values in an associative array. For example:
$fruits = array('apple' => 'red', 'banana' => 'yellow', 'orange' => 'orange');
$values = array_values($fruits);
print_r($values);
The output of this code will be:
Array ( [0] => red [1] => yellow [2] => orange )
array_merge()
: This function merges two or more arrays into a single array. For example:
$fruits1 = array('apple', 'banana');
$fruits2 = array('orange', 'mango');
$fruits3 = array('pineapple', 'grapes');
$fruits = array_merge($fruits1, $fruits2, $fruits3);
print_r($fruits);
The output of this code will be:
Array ( [0] => apple [1] => banana [2] => orange [3] => mango [4] => pineapple [5] => grapes )
array_unique()
: This function removes duplicate values from an array. For example:
$fruits = array('apple', 'banana', 'orange', 'apple', 'mango');
$unique_fruits = array_unique($fruits);
print_r($unique_fruits);
The output of this code will be:
Array ( [0] => apple [1] => banana [2] => orange [4] => mango )
sort()
: This function sorts an array in ascending order. For example:
$fruits = array('apple', 'banana', 'orange', 'mango');
sort($fruits);
print_r($fruits);
The output of this code will be:
Array ( [0] => apple [1] => banana [2] => mango [3] => orange )
These are just a few examples of the array functions available in PHP. You can find a full list of array functions in the PHP documentation at http://php.net/manual/en/ref.array.php.