The Misunderstanding About "Keyless" Arrays in PHP
The confusion often stems from a fundamental misunderstanding of how PHP arrays work under the hood. You are asking for an array that looks like Array(19, 18, 21), but strictly speaking, every PHP array has keys. The numeric indices [0], [1], and [2] in your source data are associative keys that happen to be integers starting from zero.
When you iterate through a standard PHP array using foreach(), the loop variable captures both the value and the key. If you simply assign $row[] = $val; inside the loop, you are explicitly telling PHP: "Give me the next available integer key." Since the previous keys were 0, 1, and 2, the new element gets key 3. This results in Array ( [3] => 19, [4] => 18, ... ).
The reason this feels like a failure to remove keys is that you are trying to overwrite existing keys or skip them without a clear mechanism. PHP does not allow you to simply "reset" the internal pointer of an array and continue assigning values sequentially while ignoring the original structure unless you explicitly handle the index assignment.
To achieve the state you desireâwhere the resulting structure behaves exactly like a listâyou must ensure that the new array is built with sequential integer keys starting at zero. The most robust way to do this without relying on fragile internal pointers is to construct a new array where the index is calculated or managed explicitly.
Consider the scenario where you are iterating over $_POST['items']. If you want to create a clean, re-indexed list, you can use the array_values() function. This built-in utility function takes an associative or indexed array and returns a new array containing only the values, with keys reset to 0, 1, 2, etc.
$original = $_POST['items']; // Example: [0] => 19, [1] => 18, [2] => 21
// Note: In reality, $_POST arrays from multiple-selects often retain their original keys if not handled carefully.
// If the original array already has sequential keys 0, 1, 2 but you want to be sure:
$cleanArray = array_values($original);
print_r($cleanArray);
// Output: Array ( [0] => 19 [1] => 18 [2] => 21 )
If your goal is to create a new array from scratch within a loop, simply using $row[] is actually the correct approach for creating a sequential list. The issue you describedâwhere it "leaves you with a numerical index"âis likely a misinterpretation of what a numerical index is. In PHP, [0], [1], and [2] are numerical indices. They are not strings or complex objects; they are integers.
However, if your original input is an associative array where the keys are not sequential (e.g., [5] => 19, [10] => 18), then using $row[] will indeed preserve those gaps if you try to assign by value without care, or it will start counting from zero only if you use the offset operator [].
Let's look at a specific pitfall. If you iterate over an array where keys are not sequential, like:
$original = [5 => 19, 10 => 18];
foreach ($original as $val) {
$row[] = $val;
}
The resulting $row will be [0] => 19, [1] => 18. The loop automatically assigns the next sequential index because you used []. This is exactly what you want. If you are seeing non-sequential keys in your result, it implies that somewhere in your logic, you might be assigning values to specific keys or merging arrays incorrectly.
Another common pattern involves array_combine or array_map, but for simple re-indexing, array_values() remains the most readable and performant choice. It operates on the entire dataset at once rather than requiring a loop with manual index management.
In a Laravel context, if you are dealing with form requests, the only() or except() methods on the request object can also help filter payloads, but they do not re-index keys unless combined with array_values(). For instance:
$cleanData = array_values($request->only(['selected_items']));
This ensures that regardless of how the form submitted the data (even if the underlying HTML structure caused non-sequential POST keys), your application logic receives a standard, sequential list.
The key takeaway is that "no keys" in PHP terminology means "sequential integer keys starting from zero." You cannot have a value without some key, but you can have an array where all keys are consecutive integers. By using array_values() or the offset assignment operator [] correctly, you guarantee this structure. Avoid trying to manipulate the internal pointer (reset(), next()) manually unless you have a very specific reason to do so, as it adds unnecessary complexity and potential for bugs like those encountered when keys get misaligned during iteration.