2016-12-13 84 views
1

我想要做一些加密和写数字
我用:PHP数组元素取代

$a = [100,101,102,103,104,105] 
function decrition (array $a){ 
return preg_replace('/101/','a',$a); 
} 

而且它的返回我的所有字母排列“一”为每个101。 接下来我该如何改变? 101为 “B”,102 “C” 等

return preg_replace('[101|102|103|104|105]','a',$a); 

这种方法代替这一切的号码字母 “a”

return preg_replace('[101|102|103|104|105','a|b|c|d|e',$a); 

遗憾的是它不工作

+0

'我想做出一些encryption'请不要试图[推倒重来(http://security.stackexchange.com/questions/18197/为什么 - 不应该,我们侧倾我们-自己)。 – Martin

回答

0

为什么你试试把它当作一个字符串?

<?php 

$a = [ 101, 102, 103 ]; 

$replace_array = array(101 => "a", 102 => "b"); 
$b = array_map(function($val) use ($replace_array) { 
    return (isset($replace_array[$val]) ? $replace_array[$val] : $val); 
}, $a); 

var_dump($a, $b); 

提供了以下的输出:

array(3) { 
    [0]=> 
    int(101) 
    [1]=> 
    int(102) 
    [2]=> 
    int(103) 
} 
array(3) { 
    [0]=> 
    string(1) "a" 
    [1]=> 
    string(1) "b" 
    [2]=> 
    int(103) 
} 
0

也许您正在寻找这样的事情?

$test = str_replace($a, array('a','b','c','d','e','f'), $a); 
print_r($test); 
0

此解决方案

return str_replace(['101', '102', '103', '104', '105'], ['a', 'b', 'c', 'd', 'e'], $a);