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

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

帶有 Word 的數組值,例如二 - 2、三 - 3

帶有 Word 的數組值,例如二 - 2、三 - 3

PHP
烙印99 2023-09-22 15:06:00
這是我使用的數組$a = array("1"=>2,"2"=>8,"3"=>9,"4"=>8,"5"=>6,"6"=>7,"7"=>9,"8"=>2);當 2 打印 Two-2 時,當 3 打印 Three-3 時,我試圖得到類似的輸出//Desired OutputTwo - 2Eight -8Nine - 9我通過使用另一個數組得到了所需的輸出這段代碼使用 null 作為 array_map() 函數中的回調創建數組數組$b = array("Two", "Eight", "Nine", "Eight", "Six", "Seven", "Nine", "Two");$array = array_map(null, $a, $b);        foreach($array as $value) {                echo $value[1]."-".$value[0]."<br>";}所以,我想做的是獲得所需的輸出而不使用另一個數組 $b。我不知道如何在不使用另一個數組的情況下進行操作。是否可以在任何條件下進行,或者我是否在問愚蠢的問題?我剛剛開始學習php。我嘗試尋找解決方案,但沒有到達那里。如果已經有解決方案,請參考我
查看完整描述

3 回答

?
一只萌萌小番薯

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

// $a = array("1"=>2,"2"=>8,"3"=>9,"4"=>8,"5"=>6,"6"=>7,"7"=>9,"8"=>2); // used short array notation (see references) and removed keys (since not used)

$numbers = [2,8,9,8,6,7,9,2,95890814984141]; // the last number is for my pleasure :P

$numberFormatter = new NumberFormatter('en', NumberFormatter::SPELLOUT);


foreach ($numbers as $number) {

    $numberSpelled = $numberFormatter->format($number);

    $numberSpelledWithFirstCharUpper = ucfirst($numberSpelled);

    

    echo $numberSpelledWithFirstCharUpper . ' - ' . $number . PHP_EOL;

}

輸出

Two - 2

Eight - 8

Nine - 9

Eight - 8

Six - 6

Seven - 7

Nine - 9

Two - 2

Ninety-five trillion eight hundred ninety billion eight hundred fourteen million nine hundred eighty-four thousand one hundred forty-one - 95890814984141

工作示例。



查看完整回答
反對 回復 2023-09-22
?
慕娘9325324

TA貢獻1783條經驗 獲得超4個贊

array_map()是的,您必須為此創建另一個數組或將值放在現有數組上,但如果您不使用此方法,那么這將是很好的練習。

foreach($a as $key=>$value) {    
    echo $b[$key]."-".$value."<br>";


查看完整回答
反對 回復 2023-09-22
?
繁花如伊

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

添加查找數組并:


$lookup = [

    'zero',

    'one',

    'two',

    'three',

    'four',

    // etc...

];

$a = array("1"=>2,"2"=>4,"3"=>1,"4"=>1,"5"=>2,"6"=>3,"7"=>3,"8"=>2);

foreach ($a as $num) {

    echo $lookup[$num] . '-' . $num;

}

在這里擺弄。



查看完整回答
反對 回復 2023-09-22
  • 3 回答
  • 0 關注
  • 143 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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