2

次のような配列があるとします。

$array = array
("this","is","me","and","I","am","an","array","hello","beautiful","world");

0 を「A」に、1 を「B」に変換する関数を作成するにはどうすればよいですか? 使うForeachか…?

そのため、代わりに

 Array
(
    [0] => this
    [1] => is
    [2] => me
    [3] => and
    [4] => I
    [5] => am
    [6] => an
    [7] => array
    [8] => hello
    [9] => beautiful
    [10] => world
)

私は得るだろう

 Array
(
    ['A'] => this
    ['B'] => is
    ['C'] => me
    ['D'] => and
    ['E'] => I
    ['F'] => am
    ['G'] => an
    ['H'] => array
    ['I'] => hello
    ['J'] => beautiful
    ['BA'] => world
)
4

1 に答える 1

6
function digits_to_letters($input) {
    return strtr($input, "0123456789", "ABCDEFGHIJ");
}

$result = array_flip(array_map("digits_to_letters", array_flip($original)));

(実行例:http://ideone.com/TQNYj


+よりも優先する場合はforeach、代わりにこれを使用できます。array_fliparray_map

$result = array();
foreach($original as $k => $v) {
    $result[strtr($k, "0123456789", "ABCDEFGHIJ")] = $v;
}
于 2012-04-05T04:03:08.503 に答える