Tuesday, 23 January 2018

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;
}

$array = array('a','b','c','d');

print_r(values2keys($array, 7));

/*
returns:

Array
(
    [a] => 7
    [b] => 7
    [c] => 7
    [d] => 7
)

*/

0 comments:

Post a Comment