Array_Swap function in PHP In PHP/Laravel  I have the following function. Can you please review my approach?

private function array_swap_assoc($key1, $key2, $array)
    {
        $newArray = array();
        foreach ($array as $key =&gt; $value) {
            if ($key == $key1) {
                $newArray[$key2] = $array[$key2];
            } elseif ($key == $key2) {
                $newArray[$key1] = $array[$key1];
            } else {
                $newArray[$key] = $value;
            }
        }
        return $newArray;
    }
This will swap the values but is not very robust. If for instance one of the two keys is not defined this will generate notices and might cause sneaky errors further down in the code.

private function array_swap_assoc($key1, $key2, $array)


I don't see why that would be in a class. This is not realated to a specific group of activities. A simple function would do very well, and possibly be better (as in, respect the Single Responsibility Principle), as the class that contains it almost certainly is not a class containing exclusively array functions.

$newArray = array();


Creating a new array is a valid approach, but a different approach could be to pass the array as a reference to swap the values in the original array, unless you specifically need not to modify the original array. This will most of the time not do any noticeable difference, but it is more memory efficient:

private function array_swap_assoc($key1, $key2, &amp;$array) {}


Notice the &amp; in front of the variable. The $array local to this method will change the original one.

foreach ($array as $key =&gt; $value) {


It is a bit heavy handed to recreate the whole array. We only want to swap two values. Since we passed it by reference, we could just swap those two values without needing to reassign each and every key.

if ($key == $key1) {
    $newArray[$key2] = $array[$key2];
} elseif ($key == $key2) {
    $newArray[$key1] = $array[$key1];
} else {
    $newArray[$key] = $value;
}


The problem here is that there is no validation that both of the keys exist. We should validate that they do, and throw an exception if they don't.

if (array_key_exists($key1, $array) &amp;&amp; array_key_exists($key2, $array) {}


Notice that I use array_key_exists and not isset, because the values could be null (which is a totally valid value) and null values will return false with isset.

So taking into account adding validation and using a passed by reference array, you could make that function more robust like this:

function array_swap_assoc($key1, $key2, &amp;$array)
{
    if (array_key_exists($key1, $array) &amp;&amp; array_key_exists($key2, $array)) {
        $value1 = $array[$key1];
        $value2 = $array[$key2];
        $array[$key1] = $value2;
        $array[$key2] = $value1;
    } else {
        throw new InvalidKeys;
    }
}

class InvalidKeys extends \Exception {}




array_swap_assoc('first', 'second', $array);
// no need to use the void return, the original array is modified