Courses/PHP/Arrays & Collections

    Lesson 7 โ€ข Intermediate

    Arrays & Collections ๐Ÿ“ฆ

    Master PHP's most powerful data structure โ€” indexed arrays, associative arrays, multidimensional arrays, and 30+ built-in array functions.

    What You'll Learn in This Lesson

    • โ€ข Indexed arrays for ordered lists of values
    • โ€ข Associative arrays with string keys (key => value)
    • โ€ข Multidimensional arrays for complex data structures
    • โ€ข Sorting, searching, filtering, and transforming arrays
    • โ€ข array_map, array_filter, array_reduce for functional programming

    1๏ธโƒฃ Three Types of PHP Arrays

    TypeSyntaxUse Case
    Indexed["a", "b", "c"]Ordered lists
    Associative["name" => "Alice"]Key-value data
    Multidimensional[["a", 1], ["b", 2]]Tables, nested data

    Try It: Array Types

    Create indexed, associative, and multidimensional arrays and access their elements

    Try it Yourself ยป
    JavaScript
    // PHP Arrays & Collections (simulated in JavaScript)
    console.log("=== Indexed Arrays ===");
    console.log();
    
    // PHP: $fruits = ["Apple", "Banana", "Cherry", "Date"];
    let fruits = ["Apple", "Banana", "Cherry", "Date"];
    
    console.log("Fruits array: [" + fruits.join(", ") + "]");
    console.log("First:  $fruits[0] = " + fruits[0]);
    console.log("Last:   $fruits[3] = " + fruits[3]);
    console.log("Count:  count($fruits) = " + fruits.length);
    
    console.log();
    
    // Adding elements
    fruits.push("Elderberry");  /
    ...

    Try It: Array Functions

    Sort, search, filter, map, reduce, merge, and slice arrays

    Try it Yourself ยป
    JavaScript
    // PHP Array Functions (simulated in JavaScript)
    console.log("=== Sorting Arrays ===");
    console.log();
    
    let numbers = [5, 2, 8, 1, 9, 3, 7];
    console.log("Original: [" + numbers.join(", ") + "]");
    
    // sort() โ€” sorts ascending
    let sorted = [...numbers].sort((a, b) => a - b);
    console.log("sort():   [" + sorted.join(", ") + "]");
    
    // rsort() โ€” sorts descending
    let rsorted = [...numbers].sort((a, b) => b - a);
    console.log("rsort():  [" + rsorted.join(", ") + "]");
    
    console.log();
    console.log("=== Sea
    ...

    โš ๏ธ Common Mistakes

    โš ๏ธ
    Confusing indexed and associative โ€” PHP arrays can mix both. [0 => "a", "name" => "b"] is valid but confusing. Pick one style per array.
    โš ๏ธ
    Using sort() on associative arrays โ€” sort() re-indexes keys! Use asort() to preserve key-value associations.
    โš ๏ธ
    Modifying arrays during foreach โ€” can cause unexpected behaviour. Build a new array with array_map() or array_filter() instead.
    ๐Ÿ’ก
    Pro Tip: Use array_map(), array_filter(), and array_reduce() for clean, functional-style code. They're faster and safer than manual loops.

    ๐Ÿ“‹ Quick Reference โ€” PHP Arrays

    FunctionPurposeReturns
    count()Array lengthint
    in_array()Check if value existsbool
    array_map()Transform each elementarray
    array_filter()Keep matching elementsarray
    array_merge()Combine arraysarray
    sort() / asort()Sort valuesvoid (modifies)

    ๐ŸŽ‰ Lesson Complete!

    You've mastered PHP arrays! Next, learn how to read and write files on the server.

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy Policy โ€ข Terms of Service