← PHP EnglishChapter 06 of 13

Arrays

## Learning Objectives - Understand indexed arrays - Work with associative arrays - Handle multidimensional arrays - Master array functions ## Creating Arrays ### Basic Array ```php ``` ### array() Syntax ```php ``` ## Indexed Arrays ### Creating ```php ``` ### Adding Elements ```php red, [1] => green, [2] => blue, [3] => yellow, [4] => purple ?> ``` ### Removing Elements ```php green unset($colors[0]); // Remove by index $colors = array_values($colors); // Re-index ?> ``` ## Associative Arrays ### Creating Associative Arrays ```php "Alice", "age" => 25, "city" => "Paris" ]; echo $person["name"]; // Alice ?> ``` ### Adding/Updating ```php "123-456", "country" => "France"]); ?> ``` ### Removing ```php "Alice", "age" => 25, "city" => "Paris"]; unset($person["city"]); // Remove city print_r($person); // [name] => Alice, [age] => 25 ?> ``` ## Array Iteration ### for Loop (Indexed) ```php ``` ### foreach Loop ```php $fruit) { echo "$index: $fruit\n"; } // By key-value (associative) foreach ($person as $key => $value) { echo "$key: $value\n"; } ?> ``` ## Multidimensional Arrays ### 2D Array ```php ``` ### Associative with Arrays ```php ["math" => 95, "science" => 88], "Bob" => ["math" => 78, "science" => 92] ]; echo $students["Alice"]["math"]; // 95 ?> ``` ### Iterating 2D Array ```php ``` ## Array Functions ### Count and Size ```php ``` ### Checking Keys/Values ```php "Alice", "age" => 25]; isset($person["name"]); // true array_key_exists("name", $person); // true in_array(25, $person); // false (25 is not a value) in_array("Alice", $person); // true array_keys($person); // ["name", "age"] array_values($person); // ["Alice", 25] ?> ``` ### Searching ```php ``` ### Slicing ```php 10, 1 => 20, 2 => 30, 3 => 40, 4 => 50]; array_slice($numbers, 2); // [30, 40, 50] array_slice($numbers, 2, 2); // [30, 40] array_slice($numbers, -2); // [40, 50] array_slice($numbers, 0, 3); // [10, 20, 30] ?> ``` ### Combining ```php 1, "b" => 2, "c" => 3] // Merge arrays array_merge($a, $b); // [0 => "a", 1 => "b", 2 => "c", 3 => 1, 4 => 2, 5 => 3] // For associative: later values overwrite earlier $x = ["a" => 1, "b" => 2]; $y = ["a" => 10, "c" => 3]; array_merge($x, $y); // ["a" => 10, "b" => 2, "c" => 3] ?> ``` ### Spread Operator (PHP 7.4+) ```php ``` ## Sorting Arrays ### Sort Functions ```php 25, "Bob" => 20, "Charlie" => 30]; asort($person); // Sort by value, keep keys arsort($person); // Sort by value descending ksort($person); // Sort by key krsort($person); // Sort by key descending ?> ``` ### Custom Sort ```php "Alice", "age" => 25], ["name" => "Bob", "age" => 20], ["name" => "Charlie", "age" => 30] ]; usort($people, function($a, $b) { return $a["age"] <=> $b["age"]; }); print_r($people); // Sorted by age ascending ?> ``` ### Array Reverse ```php ``` ## Filtering and Mapping ### array_filter() ```php $n % 2 === 0, ARRAY_FILTER_USE_KEY); // [1 => 2, 3 => 4, 5 => 6, 7 => 8, 9 => 10] // Both key and value $filtered = array_filter($numbers, function($value, $key) { return $value > 5 && $key < 8; }, ARRAY_FILTER_USE_BOTH); ?> ``` ### array_map() ```php $a + $b, [1, 2], [3, 4]); // [4, 6] // With keys preserved $indexed = array_map(fn($n) => $n * 2, $numbers); // Preserves original keys ?> ``` ### array_reduce() ```php $carry . " " . $item); // " Hello World PHP" ?> ``` ## Array Walk ### array_walk() ```php Apple, [1] => Banana, [2] => Cherry ?> ``` ## Useful Array Functions ### Checking All/Any ```php $n % 2 === 0); // true // Any divisible by 3 $anyDiv3 = array_any($numbers, fn($n) => $n % 3 === 0); // false // PHP 7: use array_filter + count ?> ``` ### Chunking ```php [1, 2, 3], 1 => [4, 5, 6], 2 => [7]] ?> ``` ### Flipping ```php "apple", "b" => "banana"]; $flipped = array_flip($fruits); // ["apple" => "a", "banana" => "b"] // Useful for quick lookup $prices = ["apple" => 1.99, "banana" => 0.79]; $flippedPrices = array_flip($prices); // [1.99 => "apple", 0.79 => "banana"] ?> ``` ### Pad Array ```php ``` ### Sum and Product ```php 1, "b" => 2, "c" => 3]; array_sum($assoc); // 6 ?> ``` ### Unique Values ```php ``` ### Range ```php ``` ## Summary - Arrays: `$arr = [1, 2, 3]` or `array(1, 2, 3)` - Indexed: auto-keys 0, 1, 2... - Associative: custom keys `$arr = ["name" => "Alice"]` - Multidimensional: arrays within arrays - foreach for iteration: `foreach ($arr as $key => $value)` - `count()`, `sizeof()` for size - `array_key_exists()`, `in_array()` for checking - `array_keys()`, `array_values()` for extraction - `sort()`, `asort()`, `ksort()` for sorting - `array_filter()`, `array_map()`, `array_reduce()` for transformation - `array_slice()`, `array_chunk()`, `array_merge()` for manipulation - Spread operator: `[...$a, ...$b]`

Comments

Comments powered by Giscus

To enable comments, add your Giscus embed code here.

Learn more about Giscus →