Last active
August 29, 2015 14:22
-
-
Save tanftw/50ed5e520764661a08b6 to your computer and use it in GitHub Desktop.
array_swap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( ! function_exists( 'array_swap' ) ) | |
{ | |
/** | |
* This function works like array_flip but allows users use values as array | |
* For example, | |
* [ foo => ['bar', 'baz'] ] | |
* will becomes | |
* [ | |
* 'bar' => 'foo', | |
* 'baz' => 'foo' | |
* ] | |
* | |
* @param Array $array Array to be swapped | |
* @return Array array output | |
*/ | |
function array_swap( array $array ) | |
{ | |
$swapped = array(); | |
foreach ( $array as $key => $nested ) | |
{ | |
foreach ( (array) $nested as $index => $value ) | |
{ | |
$swapped[$value] = $key; | |
} | |
} | |
return $swapped; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment