将数字数据从数组更改为字母

时间:2018-01-23 04:40:00

标签: php arrays

我有像这样的数据

$corect_position=array(
        'A' => '0',
        'B' => '1',
        'C' => '2',
        'D' => '3',
        'E' => '4'
        );

我有这样的数字数据

 $corect_key = '012301230123';

如何将字符串中的数据与数组中的数据等同,以便稍后显示的是 A B C D

5 个答案:

答案 0 :(得分:0)

您是否尝试生成包含来自var client = tcpListener.AcceptTcpClient(); var remoteEndPoint = _currentClient.Client.RemoteEndPoint; 的密钥的新字符串?

$corect_position

输出:

<?php
$corect_position=array(
    'A' => '0',
    'B' => '1',
    'C' => '2',
    'D' => '3',
    'E' => '4'
    );

$corect_key = '012301230123';

$corect_arr = str_split($corect_key);
$new_corect = '';

foreach($corect_arr as $corect){
    if(($key = array_search($corect, $corect_position)) !== false){
        $new_corect .= $key;
    }else{
        // Not found in array
    }
}

print_r($new_corect);

告诉我你是否还想要其他东西。

答案 1 :(得分:0)

   <?php
    $corect_position=array(
        'A' => '0',
        'B' => '1',
        'C' => '2',
        'D' => '3',
        'E' => '4'
    );

    $corect_key = '012301230123';

    $array_corect_key  = array_map(null, str_split($corect_key));

    foreach($array_corect_key as $key){
        foreach($corect_position as $position) {
            if($key == $position){
               echo array_keys($corect_position, $position)[0];
               continue;
            }
        }
    }
    ?>

答案 2 :(得分:0)

这是一个简单的单行解决方案:

$corect_position=array(
        'A' => '0',
        'B' => '1',
        'C' => '2',
        'D' => '3',
        'E' => '4'
        );
$corect_key = '012301230123';

echo str_replace(array_values($corect_position), array_keys($corect_position), $corect_key );

输出:

ABCDABCDABCD

另请注意“正确”一词应拼写为2个R。

答案 3 :(得分:0)

$output = '';
foreach($corect_position as $key) {
	$output.=$key;
}

echo $output;

答案 4 :(得分:0)

翻转第一个阵列后可以使用strtr功能

 echo strtr($corect_key, array_flip($corect_position)); // ABCDABCDABCD

demo