猿问

如何开始在不同索引处循环遍历数组并完成整个数组

我试图弄清楚如何开始以不同的索引遍历数组,但是当它到达末尾时,它会循环回到开头并完成数组。基本上,我需要能够动态更改数组的偏移量。

我想要做的是将一个字母表中的一个字母与一个不同的字母表字母相关联,以将字符串混合起来。

假设我有一个像这样的随机数组

$arr = array('a' => 'g', 'b' => 'w', 'c' => 'j', 'd' => 'y', 'e' => 'k');

然后我有一个像这样的字符串

$string = 'abcde';

让我们说我需要在阵列在指数开始2这将是'c' => 'j'直到它完成,然后在阵列完成到底,然后循环回到开始。

我想要做的是用数组中与其关联的相应字母替换每个字母。所以替换后的最终字符串看起来像

我会用

$build = strtr($string,$arr);

这会回声 gwjyk

但是我需要从数组中的一个随机点开始,然后完成它并返回开始并完成整个数组。

所以也许我有一个偏移量2.

$offset = 2;


素胚勾勒不出你
浏览 143回答 3
3回答

繁花如伊

正如我在评论中提到的,我会使用array_slice来解决这个问题,然后合并两个数组以便简单地获得一个新数组,然后从头到尾循环遍历它。这是一个功能齐全的解决方案(和一个可运行的版本)-尽管我想指出偏移量确实根本不会改变结果:/**&nbsp;* Goes through a string and replaces letters based on an array "map".&nbsp;*&nbsp;&nbsp;* @param string - $string&nbsp;* @param int - $offset&nbsp;*&nbsp;&nbsp;* @return string&nbsp;*/function change_letters( $string, $offset ) {&nbsp; &nbsp; $letters = ['a' => 'g', 'b' => 'w', 'c' => 'j', 'd' => 'y', 'e' => 'k'];&nbsp; &nbsp; // some defensive code to prevent notices or errors&nbsp; &nbsp; if ( (int)$offset > count($letters)) {&nbsp; &nbsp; &nbsp; &nbsp; echo '<p>Error: Offset is larger than the array of letters!</p>';&nbsp; &nbsp; &nbsp; &nbsp; return $string;&nbsp; &nbsp; }&nbsp; &nbsp; // build new array based on passed-in offset&nbsp; &nbsp; $new_array = array_slice($letters, $offset) + array_slice($letters, 0, $offset);&nbsp; &nbsp; // at this point, array is ['c' => 'j', 'd' => 'y', 'e' => 'k', 'a' => 'g', 'b' => 'w']&nbsp; &nbsp; // loop through the letters to replace...&nbsp; &nbsp; foreach($new_array AS $from => $to) {&nbsp; &nbsp; &nbsp; &nbsp; // swaps all instances of the "from" letter to the "to" letter in the string.&nbsp; &nbsp; &nbsp; &nbsp; // NOTE: this could be easily modified to only replace n instances of the "from" letter&nbsp; &nbsp; &nbsp; &nbsp; // like so: $string = str_ireplace( $from, $to, $string, 1); - would only replace 1 instance&nbsp; &nbsp; &nbsp; &nbsp; $string = str_ireplace( $from, $to, $string );&nbsp; &nbsp; }&nbsp; &nbsp; return $string;}// Sample usage:$word = 'abcde';$new_word = change_letters( $word, 2); // "gwjk"var_dump(2, $new_word);$new_word = change_letters( $word, 5); // "gwjk"var_dump(5, $new_word);$new_word = change_letters( $word, 6); // "abcde"var_dump(5, $new_word);

qq_花开花谢_0

如评论中所述,示例数据的另一个选项可能是使用array_slice并设置偏移量和长度参数并使用 array_merge:$arr = array('a' => 'g', 'b' => 'w', 'c' => 'j', 'd' => 'y', 'e' => 'k');$top = array_slice($arr, 0, 2);$rest = array_slice($arr, 2);print_r(array_merge($rest, $top));Array(&nbsp; &nbsp; [c] => j&nbsp; &nbsp; [d] => y&nbsp; &nbsp; [e] => k&nbsp; &nbsp; [a] => g&nbsp; &nbsp; [b] => w)

忽然笑

这将测试字符串的所有可能的偏移量$arr = array('a' => 'g', 'b' => 'w', 'c' => 'j', 'd' => 'y', 'e' => 'k');$str = "abcde";$strlen = strlen($str);$keys = array_keys($arr);for ($j = 0; $j < $strlen; $j++)&nbsp;{&nbsp; &nbsp; $startIndex = $j;&nbsp; &nbsp; echo "offset: " . $startIndex . ": ";&nbsp; &nbsp; for ($i = $startIndex; $i < $strlen; $i++ )&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $char = substr( $str, $i, 1 );&nbsp; &nbsp; &nbsp; &nbsp; echo $arr[$char];&nbsp; &nbsp; }&nbsp; &nbsp; for ($i = 0; $i < $startIndex; $i++ )&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $char = substr( $str, $i, 1 );&nbsp; &nbsp; &nbsp; &nbsp; echo $arr[$char];&nbsp; &nbsp; }&nbsp; &nbsp; echo "\n";}输出:offset: 0: gwjykoffset: 1: wjykgoffset: 2: jykgwoffset: 3: ykgwjoffset: 4: kgwjy
随时随地看视频慕课网APP
我要回答