Working with arrays and conditions

Asked

Viewed 56 times

0

I have three arrays received via POST:

[campo_habilita]    => Array ( [0] => Habilitado 
                               [1] => Habilitado 
                               [2] => Habilitado ) 

[campo_nome]        => Array ( [0] => Nome 1 
                               [1] => Nome 2 
                               [2] => Nome 3 )

[campo_nascimento] => Array (  [0] => 15-05-1990 
                               [1] => 27-02-1983 
                               [2] => 14-03-1987 )

I need to set some conditions (if) before entering the data in the database, however, I do not know how to mount the foreach integrating the three arrays, to test the submitted data. (I don’t know if this is the command to be used for that)

One of the conditions is:

  • if (enable='enabled' and name=') = "show error";

Another condition is:

  • if the (enable='enabled' field and (function TestaData(field birth)=false) = "show error";

Can you help me?

  • 1

    In this situation a is normal seems better, ai vc indexes the 3 arrays by the same Dice ($i) and makes the comparisons.

  • like I ride this one for, rray?

1 answer

3

Like the @rray said you can do it, indexing beams:

$_POST['campo_habilita'] = Array (
    0 => 'Habilitado',
    1 => 'Habilitado',
    2 => 'Habilitado',
);

$_POST['campo_nome'] = Array (
    0 => 'Nome 1', 
    1 => 'Nome 2', 
    2 => 'Nome 3'
);

$_POST['campo_nascimento'] = Array (
    0 => '15-05-1990',
    1 => '27-02-1983',
    2 => '14-03-1987',
);

Foreach

foreach ($_POST['campo_habilita'] as $k => $value){
    $habilitado = $value;
    $nome       = $_POST['campo_nome'][$k];
    $nascimento = $_POST['campo_nascimento'][$k];

    // aqui você testa os valores;
}

For

for($i = 0; $i < count($_POST['campo_habilita']); $i++){
    $habilitado = $_POST['campo_habilita'][$i];
    $nome       = $_POST['campo_nome'][$i];
    $nascimento = $_POST['campo_nascimento'][$i];

    // aqui você testa os valores;
}
  • Thank you very much, friends!

Browser other questions tagged

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