Difficulty in choosing the function Correct Strings
-
I'm having an exercise to work out and I'm having some trouble.
This is the statement of the problem:
Again print the names of the students by order of enrollment, but only those whose initial letter is different from “T”.
That's 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 like
count_chars()
,str_word_count
without success. Someone can give me a hand and explain if possible where I'm making the mistake and what would be the right function.
-
To each interaction of your foreach loop the variable ♪ receive the value of your array index and the variable $value received the value contained. Then you need to check the first letter of each word provided as an index of your array, so we use the variable ♪ and function strcasecmp native to php, which makes the comparison of strings without differentiating capital letters or minusculas.
<?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 ♪ receives the value of the index which is the name, then $key[0] contains the first letter of the word. Cients of this, we assign to the second parameter of the function the letter T and make comparison. If $key[0] is different from the letter T We printed the student's name, otherwise we ignored the impression.