亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何開始在不同索引處循環遍歷數組并完成整個數組

如何開始在不同索引處循環遍歷數組并完成整個數組

PHP
素胚勾勒不出你 2021-11-13 19:05:39
我試圖弄清楚如何開始以不同的索引遍歷數組,但是當它到達末尾時,它會循環回到開頭并完成數組?;旧?,我需要能夠動態更改數組的偏移量。我想要做的是將一個字母表中的一個字母與一個不同的字母表字母相關聯,以將字符串混合起來。假設我有一個像這樣的隨機數組$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;
查看完整描述

3 回答

?
繁花如伊

TA貢獻2012條經驗 獲得超12個贊

正如我在評論中提到的,我會使用array_slice來解決這個問題,然后合并兩個數組以便簡單地獲得一個新數組,然后從頭到尾循環遍歷它。


這是一個功能齊全的解決方案(和一個可運行的版本)-盡管我想指出偏移量確實根本不會改變結果:


/**

 * Goes through a string and replaces letters based on an array "map".

 * 

 * @param string - $string

 * @param int - $offset

 * 

 * @return string

 */

function change_letters( $string, $offset ) {

    $letters = ['a' => 'g', 'b' => 'w', 'c' => 'j', 'd' => 'y', 'e' => 'k'];

    // some defensive code to prevent notices or errors

    if ( (int)$offset > count($letters)) {

        echo '<p>Error: Offset is larger than the array of letters!</p>';

        return $string;

    }

    // build new array based on passed-in offset

    $new_array = array_slice($letters, $offset) + array_slice($letters, 0, $offset);

    // at this point, array is ['c' => 'j', 'd' => 'y', 'e' => 'k', 'a' => 'g', 'b' => 'w']

    // loop through the letters to replace...

    foreach($new_array AS $from => $to) {

        // swaps all instances of the "from" letter to the "to" letter in the string.

        // NOTE: this could be easily modified to only replace n instances of the "from" letter

        // like so: $string = str_ireplace( $from, $to, $string, 1); - would only replace 1 instance

        $string = str_ireplace( $from, $to, $string );

    }


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


查看完整回答
反對 回復 2021-11-13
?
qq_花開花謝_0

TA貢獻1835條經驗 獲得超7個贊

如評論中所述,示例數據的另一個選項可能是使用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

(

    [c] => j

    [d] => y

    [e] => k

    [a] => g

    [b] => w

)


查看完整回答
反對 回復 2021-11-13
?
忽然笑

TA貢獻1806條經驗 獲得超5個贊

這將測試字符串的所有可能的偏移量


$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++) 

{

    $startIndex = $j;

    echo "offset: " . $startIndex . ": ";


    for ($i = $startIndex; $i < $strlen; $i++ ) 

    {

        $char = substr( $str, $i, 1 );

        echo $arr[$char];

    }

    for ($i = 0; $i < $startIndex; $i++ ) 

    {

        $char = substr( $str, $i, 1 );

        echo $arr[$char];

    }

    echo "\n";

}

輸出:


offset: 0: gwjyk

offset: 1: wjykg

offset: 2: jykgw

offset: 3: ykgwj

offset: 4: kgwjy


查看完整回答
反對 回復 2021-11-13
  • 3 回答
  • 0 關注
  • 183 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號