Skip to content

Instantly share code, notes, and snippets.

@tanftw
Last active August 29, 2015 14:22
Show Gist options
  • Save tanftw/50ed5e520764661a08b6 to your computer and use it in GitHub Desktop.
Save tanftw/50ed5e520764661a08b6 to your computer and use it in GitHub Desktop.
array_swap
<?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