PHP compares current array value with previous value

I have an array with images and a date for each. I get the date through the explode array. I want to take this date value and compare it with the previous date value for the previous image. My goal here is to show these dates as headings for sets of images corresponding to each other date.

Here is my code I worked with to try to do this job (sorry for all my slashes):

 $p = 0; $a = -1; echo "<table>"; echo "<tr>"; foreach (array_combine($images,$locs) as $image => $loc) { $p++; $a++; $datesort = explode('>',$image); echo "<style>"; echo "input[label=t" . $p . "] {"; echo "display: none;"; echo "}"; echo "input[label=t" . $p . "] + label {"; echo "border: 5px solid #FFFFFF;"; $image = "background: url('http://blah.com/" . $loctb[$p] . "');"; echo $image; echo "height: 61px;"; echo "width: 92px;"; echo "display: inline-block;"; echo "padding: 0 0 0 0px;"; echo "}"; echo "input[label=t" . $p . "]:checked + label {"; echo "border: 5px solid #FF9900;"; echo "background: url('http://blah.com/" . $loctb[$p] . "');"; echo "height: 61px;"; echo "width: 92px;"; echo "display: inline-block;"; echo "padding: 0 0 0 0px;"; echo "}"; echo "</style>"; $lastdate = array(""); $lastdate[$a] = $datesort[1]; echo "<td>"; if ($lastdate[$a] <> $datesort[1]){ echo "<h2>"; echo $datesort[1]; echo "</h2>"; } echo "<input type=\"checkbox\" label=\"t" . $p . "\" id=\"t" . $p . "\" name =\"boxes[]\" value=\"<img src=http://blah.com/" . $loc . "\" />"; echo "<label for=\"t" . $p . "\"></label>"; echo "</div>"; echo "</td>"; if ($p % 2 == 0) { echo "</tr>"; } } echo "</table>"; 

I tried to save the current date value to another array variable and then compare it with the new value next time through my foreach , but I am doing it wrong: - / ...

I would appreciate any help you can offer. Thanks

+4
source share
1 answer

Here is a quick and dirty way, although you will want to customize it to your needs (I had this problem, but it was worse since I also filled in the blanks between them):

 $previous_dates = array(); foreach (array_combine($images,$locs) as $image => $loc) { $datesort = explode('>',$image); if (end($previous_dates) <> $datesort[1]) { echo "<h2>"; echo $datesort[1]; echo "</h2>"; } // ... more stuff ...// $previous_dates[] = $datesort[1]; } 

In fact, I would rethink the approach you take for a blast in a loop, but first, make sure that this is where you need it.

+2
source

Source: https://habr.com/ru/post/1469345/


All Articles