PHP Array Function Cheat Sheet: A Comprehensive Reference Guide

Post Thumbnail of PHP Array Functions Cheat Sheet

Arrays are an integral part of PHP, and mastering array manipulation is essential for effective web development.

PHP provides a rich set of array functions that make working with arrays more efficient and convenient.

In this comprehensive cheat sheet, we'll provide a detailed reference guide to PHP's array functions, categorized by their common use cases.

Array Creation and Modification

Creating Arrays

  1. array(): Creates an array.

    $array = array(1, 2, 3);
    
  2. range(): Creates an array containing a range of elements.

    $numbers = range(1, 5); // $numbers will be [1, 2, 3, 4, 5]
    

Adding and Removing Elements

  1. array_push(): Adds one or more elements to the end of an array.

    $fruits = array("apple", "banana");
    array_push($fruits, "cherry"); // $fruits will be ["apple", "banana", "cherry"]
    
  2. array_pop(): Removes and returns the last element of an array.

    $fruits = array("apple", "banana", "cherry");
    $lastFruit = array_pop($fruits); // $lastFruit will be "cherry"
    
  3. array_shift(): Removes and returns the first element of an array.

    $fruits = array("apple", "banana", "cherry");
    $firstFruit = array_shift($fruits); // $firstFruit will be "apple"
    
  4. array_unshift(): Adds one or more elements to the beginning of an array.

    $fruits = array("banana", "cherry");
    array_unshift($fruits, "apple"); // $fruits will be ["apple", "banana", "cherry"]
    

Sorting and Filtering Arrays

Sorting Arrays

  1. sort(): Sorts an array in ascending order.

    $numbers = array(3, 1, 2);
    sort($numbers); // $numbers will be [1, 2, 3]
    
  2. rsort(): Sorts an array in descending order.

    $numbers = array(3, 1, 2);
    rsort($numbers); // $numbers will be [3, 2, 1]
    
  3. asort(): Sorts an associative array in ascending order, maintaining key-value associations.

    $ages = array("John" => 30, "Alice" => 25, "Bob" => 35);
    asort($ages); // $ages will be ["Alice" => 25, "John" => 30, "Bob" => 35]
    
  4. ksort(): Sorts an associative array by keys.

    $ages = array("John" => 30, "Alice" => 25, "Bob" => 35);
    ksort($ages); // $ages will be ["Alice" => 25, "Bob" => 35, "John" => 30]
    

Filtering Arrays

  1. array_filter(): Filters elements of an array using a callback function.

    $numbers = array(1, 2, 3, 4, 5);
    $filtered = array_filter($numbers, function($element) {
        return $element % 2 == 0;
    }); // $filtered will be [2, 4]
    
  2. array_diff(): Computes the difference between two or more arrays.

    $array1 = array("a", "b", "c");
    $array2 = array("b", "c", "d");
    $difference = array_diff($array1, $array2); // $difference will be ["a"]
    

Array Searching and Retrieval

Searching Arrays

  1. in_array(): Checks if a value exists in an array.

    $fruits = array("apple", "banana", "cherry");
    $exists = in_array("banana", $fruits); // $exists will be true
    
  2. array_search(): Searches for a value in an array and returns the corresponding key.

    $fruits = array("apple", "banana", "cherry");
    $key = array_search("banana", $fruits); // $key will be 1
    
  3. array_keys(): Returns all the keys in an array.

    $person = array("name" => "John", "age" => 30);
    $keys = array_keys($person); // $keys will be ["name", "age"]
    
  4. array_values(): Returns all the values in an array.

    $person = array("name" => "John", "age" => 30);
    $values = array_values($person); // $values will be ["John", 30]
    

Slicing and Splicing Arrays

Slicing Arrays

  1. array_slice(): Returns a portion of an array.

    $fruits = array("apple", "banana", "cherry", "date");
    $slice = array_slice($fruits, 1, 2); // $slice will be ["banana", "cherry"]
    
  2. array_splice(): Removes and returns elements

    from an array.

    $fruits = array("apple", "banana", "cherry", "date");
    $removed = array_splice($fruits, 1, 2); // $removed will be ["banana", "cherry"], $fruits will be ["apple", "date"]
    

Array Merging and Combining

Merging Arrays

  1. array_merge(): Merges one or more arrays.

    $array1 = array("a", "b");
    $array2 = array("c", "d");
    $merged = array_merge($array1, $array2); // $merged will be ["a", "b", "c", "d"]
    
  2. array_combine(): Creates an array by using one array for keys and another for values.

    $keys = array("name", "age");
    $values = array("John", 30);
    $combined = array_combine($keys, $values); // $combined will be ["name" => "John", "age" => 30]
    

Conclusion

This comprehensive PHP array function cheat sheet covers a wide range of array manipulation functions, categorized by their common use cases. Whether you're creating, modifying, sorting, searching, or slicing arrays, these functions will prove invaluable in your PHP development journey. Feel free to refer to this cheat sheet as you work on PHP projects and become more proficient in handling arrays in your applications.