Difficulty choosing the correct Strings function

0

I have an exercise to solve and I'm having some difficulty.

This is the problem statement:

Reprint student names in order of enrollment, but only those whose initial letter is different from "T".

This is my code.

$nomes = array('andre' => 1 , 'paulo' =>5, 'fabio' => 2,'tiago' => 4);
asort($nomes);
foreach ($nomes as $key => $value) {
  echo 

I read the PHP manual and checked the various functions for string. I tried some of them as count_chars() , str_word_count , without success. Can anyone give me a help and explain me if possible where I'm making the error and what would be the correct function.

    
asked by anonymous 17.01.2018 / 22:07

1 answer

2

At each interaction of your foreach loop the $ key variable will receive the index value of your array and the variable $ value will receive the value contained. Next, you need to check the first letter of each word provided as an index of your array, so we use the $ key variable and the strcasecmp function native to php, which does the comparison of strings without differentiating uppercase or lowercase.

<?php
      $ord_nomes = array();
      $nomes     = array('andre' => 1 , 'paulo' =>5, 'fabio' => 2,'tiago' => 
      asort($nomes);
      foreach ($nomes as $key => $value){  
        if(strcasecmp($key[0],"t") != 0){
           echo  "O valor é: ".$value." e o índice é ".$key."<br/>";
        }
      }
?>

If $ key is given the value of the name, then $ key [0] contains the first letter of the word. Aware of this, we assign the second parameter of the function the letter T and make the comparison. If $ key [0] is different from the T letter we print the student's name, otherwise we ignore the impression.

    
17.01.2018 / 22:44