📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials PHP for Beginners Array Functions

Array Functions

5 min read Quiz at the end
Arrays store multiple values in one variable. PHP supports indexed arrays like $colors[0] and associative arrays like $person['name']. Loop through them with foreach and use built-in functions like sort() and array_filter().

Array Functions

$a = [3, 1, 4, 1, 5, 9, 2, 6];

count($a);              // 8
array_sum($a);          // 31
array_unique($a);       // [3,1,4,5,9,2,6]
sort($a);               // sort ascending
rsort($a);              // sort descending
in_array(5, $a);        // true/false
array_search(5, $a);    // returns key/false
array_push($a, 7);      // add to end
array_pop($a);          // remove from end
array_shift($a);        // remove from start
array_unshift($a, 0);   // add to start
array_reverse($a);      // reverse
array_slice($a, 1, 3);  // extract portion
array_splice($a, 1, 2); // remove and return
array_merge($a, [10,11]); // merge arrays
array_keys($a);         // all keys
array_values($a);       // all values
array_flip($a);         // swap keys and values
array_chunk($a, 3);     // split into chunks of 3
implode(", ", $a);      // join as string
explode(",","a,b,c");   // split string to array
Topic Quiz · 5 questions

Test your understanding before moving on

1. Which function merges two arrays?
💡 array_merge() merges one or more arrays together.
2. What does array_unique() do?
💡 array_unique() returns a new array with duplicate values removed.
3. Which function sorts an array preserving keys?
💡 asort() sorts by value while maintaining key associations.
4. What does in_array(5, $arr) do?
💡 in_array() checks if a value exists in an array, returning true or false.
5. Which function splits an array into chunks?
💡 array_chunk() splits an array into chunks of a specified size.