Swap the position of two array values without preserving keys

Swap the position of two array values without preserving keys

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Swapping Array Values While Discarding Keys in PHP

When working with indexed arrays in PHP, the distinction between associative and indexed structures often dictates how you manipulate data. If you have an array where the keys represent a strict sequential order—like 0, 1, and 2—and your goal is to swap two specific values while ensuring the resulting array remains purely indexed without preserving the original keys associated with those swapped values, the standard swapping logic must be applied carefully.

Consider a scenario where you start with an array defined as follows:

$arr = [
    0 => 'contact',
    1 => 'home',
    2 => 'projects'
];

In this structure, the keys are integers starting from zero. You want to swap the value 'contact' with 'home'. The desired outcome is:

$arr = [
    0 => 'home',
    1 => 'contact',
    2 => 'projects'
];

The critical requirement here is that the resulting array must strictly maintain its sequential integer keys. If you simply perform a value swap on an associative array where keys are strings or mixed types, PHP will preserve those original keys. However, since your source array uses strict integer indices, the primary risk isn't key preservation per se, but rather ensuring that the operation doesn't inadvertently convert the array into an associative one if you were to use string-based lookups later on.

The most direct way to achieve this in vanilla PHP is by accessing the values by their specific numeric indices and reassigning them. If you know the positions of the items you want to swap, you can do it with a simple two-step assignment:

$indexA = 0; // Position of 'contact'
$indexB = 1; // Position of 'home'

$temp = $arr[$indexA];
$arr[$indexA] = $arr[$indexB];
$arr[$indexB] = $temp;

This approach works perfectly for indexed arrays because it directly manipulates the value at a specific offset. Since the keys 0 and 1 are integers, PHP treats them as part of an indexed array structure. The resulting array will still be indexed numerically.

However, a common pitfall arises when developers try to swap values based on content rather than position using methods like array_search. If you use array_search to find the keys and then swap the values at those keys, you risk altering the underlying data type of the keys if the array is not strictly indexed. In your specific case, since the keys are already sequential integers (0, 1, 2), a simple value swap at known indices is the safest route.

If you were dealing with a more complex scenario where the array wasn't guaranteed to be purely indexed, or if you needed to ensure the result was reset to a pure indexed format regardless of input, you might consider using array_values. This function reindexes the array with numeric keys starting from zero after any modification.

$arr = [0 => 'home', 1 => 'contact', 2 => 'projects'];
$result = array_values($arr);
var_dump($result); // array(0 => 'home', 1 => 'contact', 2 => 'projects')

While array_values is useful for cleaning up arrays that have become associative, it incurs a slight performance cost because it creates a new array. For high-performance applications processing large datasets, direct index manipulation is preferred to avoid unnecessary memory allocation and copy operations.

In the Laravel ecosystem, while you could use native PHP functions within controllers or service classes, it is often cleaner to leverage collection helpers if you are already working within a Blade template or a controller method that receives an Eloquent model converted to an array. However, for raw performance and clarity in logic-heavy scripts, sticking to native array operations remains the best practice unless you specifically need the chainable syntax of Laravel Collections.

Ultimately, swapping values in a strictly indexed array is straightforward: identify the indices, swap the contents using a temporary variable, and verify that the keys remain sequential integers. There is no need for complex reindexing unless your input data structure is unpredictable.

Our network