Check if an array has empty values

0

I want this foreach to check for empty values inside the array, and if it exists, display the "error" message. How can I do this correctly?

if (isset($_POST['start'])) {
foreach ($_POST['start'] as $key => $value) {
echo '<br>';
    $start2 = mysql_real_escape_string($value);  // data da saida
    $cod = mysql_real_escape_string($_POST['cod'][$key]); 
    $id_cotacao = mysql_real_escape_string($_POST['id_cotacao'][$key]);// id         
}}

For example:

 linha 1 ==> 2017-12-01 | 10 | 21
 linha 2 ==> 2017-12-01 |    | 21
 linha 3 ==> 2017-12-01 | 10 | 21

If it finds an empty variable, it displays the error message.

    
asked by anonymous 20.05.2017 / 18:23

1 answer

1

It was not very clear from the example you gave , but you can do a comparison by counting the received array with the filtered array without the empty indexes, just use array_filter as in the example below. Watch running on ideone .

$post = [ 'a' => 'valor A' , 'b' => '' , 'c' => 'valor C' ];


if( count( $post ) !== count( array_filter( $post ) ) )
{
    echo 'erro!';
}
else
{
    echo 'ok!';
}
count( $post ) // output 3
count( array_filter( $post ) ) // output 2
    
20.05.2017 / 20:45