Create a foreach with a specific condition in PHP

Asked

Viewed 658 times

-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 array entry I need to check the condition and show on the screen the position data, 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 parole like that?

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 way it 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 it to happen...

  • The first if is when you have bank data, and the second when you don’t, right? All the records must be in one state or another? I don’t understand why you’re talking about foreach.

  • 1

    @Alceu And where is the array What did you say you have? By the code of the question I see that you have lines from a database. You want a lot of empty fields when it’s different day, and a lot of fields filled in when it’s the current date?

  • And I changed the other question, maybe it’s what you want. This question talks about things that are not present in the code, it’s hard to understand. If it’s something else, you need to explain it better.

  • Yes! I need several empty fields when the day is different. For each record I need several other empty ones...

1 answer

0


There is a native PHP function called Array_map, it applies a defined function (in this case its validation) to each element of the array, see her handbook 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 sort of 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.

  • 3

    Can you understand the question? Explain it to me :)

  • 1

    From what I understand, it needs to check every position it reads in the array, if the check is true then the array returns the value of the array itself, otherwise it would be null.

  • I’ve understood something completely different to the point where I think this answer is nowhere near what he wants.

  • 1

    It’s just that he says, "I also have an array. For each array entry I need to check the condition and show on the screen the position data, and if not, I need to show something with the same space only with the empty items". It needs to check for each array item.

  • Exactly that! That’s what I need, as I walk through the $arrayBancas positions I need to test what’s inside and do something else for every time the ride happens.... I’m going to try to simplify the code there... By the way, I’m trying to apply this array_map here and I’m not getting... Would I have to add the two if inside the function? It’s not working...

  • In this case, while is going through $arrayBancas, which brings multiple fields and multiple Mysql entries, and for each of these entries, I need empty fields where the day is different

  • You can replace Else with the other if. Remember that the function is separated, it has nothing to do with the line of the array_map

Show 2 more comments

Browser other questions tagged

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