PHP
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Object-Oriented PHP
-
Section 5: Working with Data
-
Section 6: Modern PHP (PHP 8)
-
Section 7: Working with Files and Networking
-
Section 8: Common PHP Frameworks Overview
-
Section 9: Tooling and Ecosystem
-
Section 10: Practical Projects
-
Section 11: Interview Practice
-
Section 12: More Standard Library
-
Section 13: Data Structures and Algorithms in PHP
-
Section 14: More Practice Exercises
-
Section 15: More Security and Best Practices
-
Section 16: More Testing and Tooling
-
Section 17: WordPress-Style CMS Concepts
-
Section 18: Advanced OOP Practice
-
Section 19: More Web Fundamentals
-
Section 20: Database Practice
-
Section 21: PHP Manual: Array Functions
-
Section 22: PHP Manual: Date and Calendar Functions
-
Section 23: PHP Manual: Filesystem and Directory Functions
-
Section 24: PHP Manual: Filter and Var Handling
-
Section 25: PHP Manual: Math Functions
-
Section 26: PHP Manual: JSON and XML
-
Section 27: PHP Manual: Network and Stream Functions
-
Section 28: PHP Manual: Error and Exception Handling
-
Section 29: PHP Manual: Output Control and Misc
-
Section 30: PHP Manual: FTP, Zip, and Mail
-
Section 31: Modern PHP Frameworks Deep Dive
-
Section 32: PHP Design Patterns
-
Section 33: More Practice Exercises
-
Section 34: PHP Performance and Deployment
-
Section 35: More Interview Practice
-
Section 36: More PHP Standard Library
-
Section 37: PHP Concurrency and Async
-
Section 38: More Web Development Practice
-
Section 39: PHP Testing Deep Dive
-
Section 40: Composer and Package Development
-
Section 41: PHP Security Deep Dive
-
Section 42: More Practical Projects
-
Section 43: Legacy PHP Maintenance
-
Section 44: More Algorithm Practice
-
Section 45: Final Practice and Review
-
Section 46: PHP for E-Commerce Patterns
-
Section 47: PHP API Design Deep Dive
-
Section 48: PHP Caching Strategies
-
Section 49: PHP Queue and Background Jobs
-
Section 50: PHP Multi-Tenancy Patterns
-
Section 51: PHP Real-Time Features
-
Section 52: PHP CMS and Content Modeling
-
Section 53: PHP Internationalization
-
Section 54: More Framework-Specific Practice
-
Section 55: PHP Legacy Code Refactoring
-
Section 56: More Practice Projects Round 2
-
Section 57: PHP Command-Line Applications
-
Section 58: PHP and Microservices
-
Section 59: More Interview and Review Round 2
91: array_slice and array_splice
I've seen this happen more times than I can count: a developer wants to remove a few items from the middle of an array, they call array_slice(), and then they spend an hour scratching their head wondering why the original array remains completely untouched. It's a classic "pass-by-value" misunderstanding.
The vanishing act that didn't vanish
$inventory = ['Sword', 'Shield', 'Potion', 'Map', 'Key'];
// The goal: Remove the 'Potion' and 'Map' (indices 2 and 3)
array_slice($inventory, 2, 2);
print_r($inventory);
/*
Output:
Array (
[0] => Sword
[1] => Shield
[2] => Potion
[3] => Map
[4] => Key
)
*/
Looking at the code above, it seems logical. You told PHP to start at index 2 and take 2 elements. But if you check the output, the $inventory array hasn't changed. The mistake here is assuming array_slice modifies the source. It doesn't. It's a "read-only" operation that returns a new array containing the slice you requested. It's like taking a photo of a piece of a cake—you have the image of the slice, but the actual cake is still sitting on the table, whole.
Cutting the cake with array_splice
If you actually want to rip elements out of the original array—or replace them with something else—you need array_splice. Think of this as the "surgical" version. It modifies the array in place.
$inventory = ['Sword', 'Shield', 'Potion', 'Map', 'Key'];
// We do the same thing, but with splice
array_splice($inventory, 2, 2);
print_r($inventory);
/*
Output:
Array (
[0] => Sword
[1] => Shield
[2] => Key
)
*/
Now the 'Potion' and 'Map' are gone. The $inventory array has been mutated. One thing to keep in mind: array_splice actually returns the elements that were removed. So, if you wanted to move those items to a "dropped items" array, you could do $dropped = array_splice($inventory, 2, 2);.
When to actually use array_slice
You might be wondering why array_slice even exists if it doesn't change the array. Well, most of the time, you don't want to destroy your original data. Imagine you're building a pagination system. You have 100 products, and you want to show only products 11 through 20 on page 2. You wouldn't want to delete the other 90 products from your memory just to show one page to the user.
$all_products = ['P1', 'P2', 'P3', ..., 'P100'];
$page_size = 10;
$offset = 10; // Starting at the 11th item
$current_page_items = array_slice($all_products, $offset, $page_size);
In this case, array_slice is perfect because it gives you a window into your data without ruining the original list.
The trick to inserting elements mid-array
Here is a pro tip: array_splice isn't just for removing things. You can use it to inject new elements into the middle of an array. You do this by passing a fourth argument—the replacement array.
$players = ['Alice', 'Bob', 'Charlie'];
// Insert 'Dave' and 'Eve' starting at index 1, removing 0 elements
array_splice($players, 1, 0, ['Dave', 'Eve']);
print_r($players);
/*
Output:
Array (
[0] => Alice
[1] => Dave
[2] => Eve
[3] => Bob
[4] => Charlie
)
*/
By setting the length (the second argument) to 0, you're telling PHP: "Don't delete anything; just push everything from this point forward to the right and jam these new items in here."
📋 Practical Task
Exercise: Building a Dynamic Leaderboard Truncator
You are building a gaming leaderboard. You have an array of players and their scores, but the UI only has room for a "Top 3" display and a "Last Place" display. Additionally, if a player is caught cheating, you need to remove them from the middle of the list without shifting the remaining players' indices manually.
Requirements:
- Create an array called
$leaderboardwith at least 6 player names. - Use
array_sliceto create a new array called$top_threecontaining only the first three players. - Use
array_sliceto create a new array called$last_placecontaining only the very last player in the list (Hint: look into using a negative offset). - Use
array_spliceto remove the 4th player (index 3) from the$leaderboardarray because they were disqualified. - Use
array_spliceto insert a new player, 'Guest_User', at the second position (index 1) of the$leaderboardarray. - Print all three arrays to verify the results.
There are no comments for now.