带有 Word 的数组值,例如二 - 2、三 - 3

这是我使用的数组


$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 Output

Two - 2

Eight -8

Nine - 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。


我尝试寻找解决方案,但没有到达那里。如果已经有解决方案,请参考我


烙印99
浏览 101回答 3
3回答

一只萌萌小番薯

// $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) {&nbsp; &nbsp; $numberSpelled = $numberFormatter->format($number);&nbsp; &nbsp; $numberSpelledWithFirstCharUpper = ucfirst($numberSpelled);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; echo $numberSpelledWithFirstCharUpper . ' - ' . $number . PHP_EOL;}输出Two - 2Eight - 8Nine - 9Eight - 8Six - 6Seven - 7Nine - 9Two - 2Ninety-five trillion eight hundred ninety billion eight hundred fourteen million nine hundred eighty-four thousand one hundred forty-one - 95890814984141工作示例。

慕娘9325324

array_map()是的,您必须为此创建另一个数组或将值放在现有数组上,但如果您不使用此方法,那么这将是很好的练习。foreach($a&nbsp;as&nbsp;$key=>$value)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$b[$key]."-".$value."<br>";

繁花如伊

添加查找数组并:$lookup = [&nbsp; &nbsp; 'zero',&nbsp; &nbsp; 'one',&nbsp; &nbsp; 'two',&nbsp; &nbsp; 'three',&nbsp; &nbsp; 'four',&nbsp; &nbsp; // etc...];$a = array("1"=>2,"2"=>4,"3"=>1,"4"=>1,"5"=>2,"6"=>3,"7"=>3,"8"=>2);foreach ($a as $num) {&nbsp; &nbsp; echo $lookup[$num] . '-' . $num;}在这里摆弄。
打开App,查看更多内容
随时随地看视频慕课网APP