Values2keys

Converts the values of an array to the keys of it.

function values2keys($arr, $value=1){
    $new = array();
    while (list($k,$v) = each($arr)){
        $v = trim($v);
        if ($v != ''){ 
            $new[$v] = $value;
        }
    }
    return $new;
}
Snippet Details



$array = array('a','b','c','d');
 
print_r(values2keys($array, 7));
 
/*
returns:
 
Array
(
    [a] => 7
    [b] => 7
    [c] => 7
    [d] => 7
)
 
*/

Sorry folks, comments have been deactivated for now due to the large amount of spam.

Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!


Older comments:

Interested Visitor December 23, 2009 at 21:26
array_flip()
wheel reuser May 26, 2009 at 07:37
array_keys() :)