PHP print capitalized names

0

I'm new to programming, I've been studying the PHP manual to better understand how the language works.

I have to do the following exercise: Print student names in uppercase in order of enrollment.

Here's my code:

$nome_alunos = array('andre' => 1 , 'paulo' =>5, 'fabio' => 2);
asort($nome_alunos);
strtoupper($nome_alunos);
foreach ($nome_alunos as $key => $value) {
  echo "$key = $value\n";

I can not capitalize the names using strtoupper() that are inside the array, I could only put it in order, I also read the PHP manual and did not find a function that helps me beyond the one described above. Can someone help me?

    
asked by anonymous 16.01.2018 / 23:11

2 answers

4

You are trying to convert the array $ student_name, but you should convert each $ key inside the loop

<?php
$nome_alunos = array('andre' => 1 , 'paulo' =>5, 'fabio' => 2);
asort($nome_alunos);

foreach ($nome_alunos as $key => $value) {
   $nome_aluno = strtoupper($key);
   echo "$nome_aluno = $value<br>";
}
?>
    
16.01.2018 / 23:21
2

You need to use the strtoupper() function on each value. Here's the code below to do this.

<?php
    $nome_alunos = array('andre' => 1 , 'paulo' =>5, 'fabio' => 2);

    asort($nome_alunos);
    foreach ($nome_alunos as $key => $value)
        echo strtoupper($key)." = $value<br>";
?>

If you want to leave only the first letter of capital, you can do as follows:

echo ucwords(strtolower($key)). " = value<br>";
    
16.01.2018 / 23:17