I need help creating two tables with PHP decision structure

Asked

Viewed 872 times

-2

Create a PHP application that checks word by word of the array below if the total letters is odd or even.

To show the result. assemble a basic HTML page with two tables: One with total odd letters words and the other with total even letters, followed by total letters.

$valores = [ 'estudar', 'educação', 'esforço', 'persistência',
             'dedicação', 'crescimento', 'evolução', 'sabedoria',
             'trabalho', 'entusiasmo', 'alegria', 'vitoria',
             'sucesso', 'profissão', 'conhecimento', 'vida' ];
  • 5

    This question seems to be decontextualized because it seems to be about an exercise of logic which did not demonstrate that there was effort on the part of the author given the absence of a preliminary implementation.

  • 1

    That’s not a doubt, that’s a statement. Try to start your work, as questions appear you can create questions here on the site so we can help you. Take a [tour] by the site and see the guide [Ask].

  • 1

    Have you tried to do anything, if yes, how about posting what you tried and if you made a mistake then yes you can have a more assertive help.

2 answers

2

The first step is to get the word size with the function mb_strlen, done this check whether the word size returns or not the rest of the division($tamanho % 2 == 0).

Now define an array with the following structure:

$resultado = array('impares' => '', 'total_impares' => 0, 'pares' => '', 'total_pares' => 0);

If the rest of the division is zero then it is even, add that word in the key $resultado['pares'] and increment the `$result['total_pairs'] pair counter, if the rest is a do the same process only this time in odd.

With formatted array, make two foreach() in the tables.

Note: another way to calculate the total would be to use Count in odd/pair keys and assign the value in total_impar/par after the foreach which makes the 'separation'

1

Although this is an exercise in logic which, up to the present moment, apparently had no effort on the part of the author demonstrated, I would like to leave my contribution anyway.

The solution is so simple that, apart from the input array, it is solved with only two lines:

$words = [ 'estudar', 'educação', 'esforço', 'persistência',
           'dedicação', 'crescimento', 'evolução', 'sabedoria',
           'trabalho', 'entusiasmo', 'alegria', 'vitoria',
           'sucesso', 'profissão', 'conhecimento', 'vida' ];

$odd = array_filter( $words, function( $word ) { return ( strlen( $word ) % 2 == 0 ); } );

$even = array_diff( $words, $odd );

The arrays $Odd and $Even resulting contains respectively:

array (size=8)
  1 => string 'educação' (length=10)
  2 => string 'esforço' (length=8)
  6 => string 'evolução' (length=10)
  8 => string 'trabalho' (length=8)
  9 => string 'entusiasmo' (length=10)
  13 => string 'profissão' (length=10)
  14 => string 'conhecimento' (length=12)
  15 => string 'vida' (length=4)

array (size=8)
  0 => string 'estudar' (length=7)
  3 => string 'persistência' (length=13)
  4 => string 'dedicação' (length=11)
  5 => string 'crescimento' (length=11)
  7 => string 'sabedoria' (length=9)
  10 => string 'alegria' (length=7)
  11 => string 'vitoria' (length=7)
  12 => string 'sucesso' (length=7)

Unlike friend’s @lost statement, use mb_strlen(), in this case, it results in false-positive because it causes the accents of the words to be considered as additional lengths.

Recommended readings:

  • +1 for the most worked reply. Thanks for the correction tbm. I took a test here with the strlen the word aça he falls into the category of pairs, when you use mb_strlen and passes the encondig of the string it falls into the odd as expected.

  • To this day I’m not quite sure of the usability of mb_strlen().

Browser other questions tagged

You are not signed in. Login or sign up in order to post.