I need help to create two tables with decision structure in PHP [closed]

-2

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

To display the result. set up a basic HTML page with two tables: one with the odd-numbered total words and one with the total of even letters followed by the total of the letters.

$valores = [ 'estudar', 'educação', 'esforço', 'persistência',
             'dedicação', 'crescimento', 'evolução', 'sabedoria',
             'trabalho', 'entusiasmo', 'alegria', 'vitoria',
             'sucesso', 'profissão', 'conhecimento', 'vida' ];
    
asked by anonymous 11.12.2014 / 15:21

2 answers

2

The first step is to get the size of the word with the function mb_strlen , done that check if the size of the word 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 to the key $resultado['pares'] and increment the counter of the pairs' $ result ['total_pares'], if the rest is a do the same process only odd times.

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

Note: Another way to calculate the total would be to use count on odd / even keys and assign the value to total_impar/par after the foreach doing the 'parse'

    
11.12.2014 / 15:42
1

Although this is an exercise in logic which, to the present moment, apparently has had no effort on the part of the demonstrated author, I would like to make 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 resulting $ odd and $ even arrays contain, 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)

Contrary to the @ friend's misplaced indication, using mb_strlen () , in this case, results in false positives as it causes the word accents are considered as additional lengths.

Recommended readings:

11.12.2014 / 16:07