How to compare arrays?

Asked

Viewed 76 times

1

I’m having trouble asking this question:

Make a program that receives ten ages, weights and heights and calculates and shows:

  • The quantity of persons weighing more than 90 kg and less than 1,50 kg;

  • The percentage of persons aged between 10 and 30 among those over 1,90;

I created three input’s to receive the information (age, weight and height). In PHP they are inserted into an array through the explode, but I can’t relate the weight and height arrays to respond to the first point or relate the age and height arrays to answer the second point.

Code I have so far:

<form action="" method="post">

    <label for="idade">Idade</label>
    <input type="text" name="idade">

    <label for="peso">Peso</label>
    <input type="text" name="peso">

    <label for="altura">Altura</label>
    <input type="text" name="altura">


    <input type="submit" name="enviar">
</form>

<?php

if(isset($_POST['enviar'])){

$cont = 0;
$soma = 0;
$media = 0;

$idades = explode(",", $_POST['idade']);
$pesos = explode(",", $_POST['peso']);
$alturas = explode(",", $_POST['altura']);

?>
  • Have you seen how the arrays look? Try to see that you will notice how the information is associated.

  • With array_filter you can remove elements from the array that do not satisfy a certain condition.

1 answer

1

A hint is you and work with the indexes of the array, because as I understood each position of the array will be a person (position 1 == person 1) for example

for($i=0;$i<=10;$i++)
{
  if ($peso[i] > 90) && ($altura[i] < 1,50)
  {
   $count++;//se a pessoa tiver mais que 90kg **E** tiver menos que 1,50 de altura incrimento +1 no meu contador
  }
}
  • 1

    $count = $count++; is redundant, only $count++; is enough

  • Indeed, I will make that change. Thank you for the warning.

Browser other questions tagged

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