PHP arrays are used to store multiple values in a single variable. An array can hold a collection of values that can be accessed using indexes or keys.
Visualizing array_push($fruits, value)
Array Count: 3
Arrays in PHP can be created using the array() function or short array syntax. They allow structured storage and easy manipulation of data.
// Creating an indexed array and accessing the first element
// Associative array using custom string keys
35,"John"=>30,"Doe"=>25);
echo $age["Peter"];
?>
// Adding elements and counting array size
// Matrix-style multidimensional array
// Accessing nested associative array values
1,"name"=>"Laptop","price"=>1000),
array("id"=>2,"name"=>"Phone","price"=>500)
);
echo $products[0]["name"];
?>
// Extracting keys from an associative array
"Apple","b"=>"Banana");
$keys = array_keys($fruits);
print_r($keys);
?>
The output depends on the accessed index or key. PHP retrieves values directly from arrays using their respective indexes or keys.