Create a foreach with a specific condition in PHP

-1

I have this condition:

if (date('Y-m-d', strtotime($arrayBancas['dataHora'])) == $data->format('Y-m-d'))

And I also have an array. For each input of the array I need to check the condition and display the position data on the screen, and if not, I need to show something with the same space only with the empty items ... How could I make a foreach with a conditional like this?

What I have so far is this:

while ($arrayBancas = mysql_fetch_array($oBanca->retorno())){
   if (date('Y-m-d', strtotime($arrayBancas['dataHora'])) == $data->format('Y-m-d')) {
      echo 'Nome:'. $arrayBancas['nome'];
   }                       
}
   if (date('Y-m-d', strtotime($arrayBancas['dataHora'])) != $data->format('Y-m-d')) {
      echo 'Nome:----------VAZIO--------------';
   }                       
}

This works perfectly for a single entry of the true conditional of the first if, but if two or more entries run it, the other does not run for the second or more times, and I need that to happen ...

    
asked by anonymous 14.11.2014 / 02:48

1 answer

0

There is a native PHP function called Array_map, it applies a defined function (in the case of its validation) to each element of the array, see the her manual here .

You can create something like:

function checkCondition($pos)
{
  if (date('Y-m-d', strtotime($arrayBancas['dataHora'])) == $data->format('Y-m-d'))
  {
    return $pos;
  }
  else
  {
   return '';
  }
}

And then you use the map array more or less like this:

$novo_array = array_map("checkCondition",$seuArrayAtual);

If you give a print_r in the variable $novo_array you will see that the positions for which the verification is true are filled, otherwise they are null.

    
14.11.2014 / 03:17