What is the use of Array and Array in PHP

In PHP, arrays are fundamental structures used to store multiple values in a single variable. They can hold data of different types and can be indexed or associative.

Arrays are extremely useful for tasks that require grouping similar data together, such as managing lists, storing user information, and handling data collections. PHP provides a variety of built-in functions to manipulate arrays, such as sorting, merging, filtering, and more.

Here’s a simple example of how to create and manipulate an array in PHP:

<?php // Indexed array $fruits = array("Apple", "Banana", "Cherry"); // Associative array $colors = array("red" => "Apple", "yellow" => "Banana", "pink" => "Cherry"); // Accessing array elements echo $fruits[1]; // Output: Banana echo $colors["red"]; // Output: Apple // Adding a new element to the indexed array $fruits[] = "Mango"; // Outputting the entire array print_r($fruits); ?>

PHP Arrays Associative Arrays Indexed Arrays PHP Array Functions Data Structures in PHP