arraySwap PHP function
I needed an easy way to swap two elements of a PHP array and came up with this function. It returns either the array with the two elements swapped or False if there's a problem.
PHP:
-
/**
-
* Swaps two elements of an array
-
*
-
* This function will return the boolean <i>False</i> if $arr_source is not an array or if $arr_source[$key1] or $arr_source[$key2] do not exist.
-
* @param mixed $arr_source The source array
-
* @param mixed $key1 The key of one of the two elements to swap
-
* @param mixed $key2 The key of the second of the two elements to swap
-
* @return mixed Array with the two elements swapped
-
*/
-
function arraySwap($arr_source, $key1, $key2)
-
{
-
{
-
return false;
-
}
-
{
-
return false;
-
}
-
-
$arr_tmp = $arr_source[$key1];
-
$arr_source[$key1] = $arr_source[$key2];
-
$arr_source[$key2] = $arr_tmp;
-
-
return $arr_source;
-
-
}
Question, Comments...
Do you have more questions. Please either leave a comment below or join us in our new forum.