第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何開始在不同索引處循環(huán)遍歷數(shù)組并完成整個數(shù)組

如何開始在不同索引處循環(huán)遍歷數(shù)組并完成整個數(shù)組

PHP
素胚勾勒不出你 2021-11-13 19:05:39
我試圖弄清楚如何開始以不同的索引遍歷數(shù)組,但是當(dāng)它到達末尾時,它會循環(huán)回到開頭并完成數(shù)組。基本上,我需要能夠動態(tài)更改數(shù)組的偏移量。我想要做的是將一個字母表中的一個字母與一個不同的字母表字母相關(guān)聯(lián),以將字符串混合起來。假設(shè)我有一個像這樣的隨機數(shù)組$arr = array('a' => 'g', 'b' => 'w', 'c' => 'j', 'd' => 'y', 'e' => 'k');然后我有一個像這樣的字符串$string = 'abcde';讓我們說我需要在陣列在指數(shù)開始2這將是'c' => 'j'直到它完成,然后在陣列完成到底,然后循環(huán)回到開始。我想要做的是用數(shù)組中與其關(guān)聯(lián)的相應(yīng)字母替換每個字母。所以替換后的最終字符串看起來像我會用$build = strtr($string,$arr);這會回聲 gwjyk但是我需要從數(shù)組中的一個隨機點開始,然后完成它并返回開始并完成整個數(shù)組。所以也許我有一個偏移量2.$offset = 2;
查看完整描述

3 回答

?
繁花如伊

TA貢獻2012條經(jīng)驗 獲得超12個贊

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


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


/**

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


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

TA貢獻1835條經(jīng)驗 獲得超7個贊

如評論中所述,示例數(shù)據(jù)的另一個選項可能是使用array_slice并設(shè)置偏移量和長度參數(shù)并使用 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

)


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

TA貢獻1806條經(jīng)驗 獲得超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


查看完整回答
反對 回復(fù) 2021-11-13
  • 3 回答
  • 0 關(guān)注
  • 184 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號