Declaring and using an array in PHP involves understanding the syntax and functionalities provided by this versatile scripting language. Below, I’ll explain the process with examples, and all information will be backed with reliable sources.
In PHP, there are primarily three types of arrays:
1. Indexed arrays: Arrays with a numeric index.
2. Associative arrays: Arrays with named keys.
3. Multidimensional arrays: Arrays that contain other arrays.
An indexed array can be declared using the `array()` function or the shorter square bracket syntax `[]`.
Example:
```
// Using the array() function
$fruits = array(“Apple”, “Banana”, “Cherry”);
// Using the short array syntax
$vegetables = [“Carrot”, “Broccoli”, “Peas”];
```
You can access elements using their index:
```
echo $fruits0; // Outputs: Apple
echo $vegetables1; // Outputs: Broccoli
```
Associative arrays use named keys that you assign to them.
Example:
```
// Using the array() function
$ages = array(“Peter” => 35, “Ben” => 37, “Joe” => 43);
// Using the short array syntax
$salaries = [“John” => 25000, “Jane” => 27000, “Mark” => 30000];
```
You can access elements using their keys:
```
echo $ages[‘Peter’]; // Outputs: 35
echo $salaries[‘Jane’]; // Outputs: 27000
```
Multidimensional arrays can contain arrays within arrays.
Example:
```
$employees = array(
“Management” => array(“Alice”, “Bob”),
“Engineering” => array(
“Software” => [“Charlie”, “Dave”],
“Hardware” => [“Eve”, “Frank”]
)
);
// Accessing elements
echo $employees[“Management”]0; // Outputs: Alice
echo $employees[“Engineering”][“Software”]1; // Outputs: Dave
```
You can add elements to an array using the `[]` operator or functions like `array_push()`.
Example:
```
$colors = array(“Red”, “Green”);
$colors[] = “Blue”; // Adds Blue to the array
array_push($colors, “Yellow”); // Adds Yellow to the array
print_r($colors); // Outputs: Array ( [0] => Red [1] => Green [2] => Blue [3] => Yellow )
```
To remove elements, PHP offers functions like `unset()`, `array_pop()`, and `array_shift()`.
Example:
```
unset($colors1); // Removes the element with index 1 (Green in this case)
array_pop($colors); // Removes the last element (Yellow)
array_shift($colors); // Removes the first element (Red)
print_r($colors); // Outputs: Array ( [2] => Blue )
```
You can iterate through indexed and associative arrays using `foreach`.
Example:
```
// Indexed array
foreach ($colors as $color) {
echo $color . “ “;
}
// Associative array
foreach ($ages as $name => $age) {
echo “$name is $age years old. “;
}
```
This array manipulation is well-documented in the official PHP manual and PHP best practices guides.
1. [PHP Manual – Arrays](https://www.php.net/manual/en/language.types.array.php)
2. [W3Schools PHP Arrays](https://www.w3schools.com/php/php_arrays.asp)
3. [PHP.net Official Documentation](https://www.php.net/manual/en/book.array.php)
These sources are recognized for their comprehensive and reliable information on PHP scripting, ensuring a solid understanding of arrays in PHP.