Beginner Fundamentals
PHP Arrays
An array stores multiple values in a single variable. Indexed arrays use numeric positions starting at zero.
Creating an array
<?php
$fruits = ["apple", "banana", "cherry"];
Accessing items
Use the index in square brackets. Counting starts at 0.
<?php
echo $fruits[0]; // apple
echo $fruits[2]; // cherry
Adding items
Append a value with empty brackets.
<?php
$fruits[] = "orange";
echo $fruits[3]; // orange
Counting items
<?php
echo count($fruits); // 4
Useful array functions
<?php
$numbers = [3, 1, 2];
sort($numbers); // [1, 2, 3]
echo in_array(2, $numbers); // true
echo implode(", ", $numbers); // "1, 2, 3"
array_push($numbers, 4); // adds 4 to the end
Quick reference
count()returns the number of itemssort()orders the valuesin_array()checks if a value existsimplode()joins items into a string