0

I'm trying to show some headers and footers to numbers, the array contains dates and what I want is that these are separated by date.

the ideal would only declare one header and one footer.

I need to do 2 foreach ?? or anyone has any idea?, thx

<?php 
$datos = array(
  array("date" => "2018"),

  array("date" => "2016"),
  array("date" => "2016"),

  array("date" => "2015"),
  array("date" => "2015"),
  array("date" => "2015"),
  array("date" => "2015"),      

  array("date" => "2014"),
  array("date" => "2014"),

  array("date" => "2005"),
);

echo "<pre>";
print_r($datos);
echo "</pre>";

echo "HEADER TOP<br>";
foreach ($datos as $row) {
  if ($datos['date'] != @$lezte_jahr){
    echo "FOOTER <br><br><br> HEADER ";
  }
  echo $row['date'] . "  ";
  $lezte_jahr = $row['date'];
}
echo "FOOTER TOP<br>";
?>

i like result, but i dont know how XD

--a--
2018
--b--

--a--
2016
2016
--b--

--a--
2015
2015
2015
2015
--b--

--a--
2014
2014
--b--

--a--
2005
--b--

1 Answer 1

2

You can simply loop through and store the last echoed date and compare the same with next date in array.

<?php
$datos = array
  (
  array("date" => "2018"),

  array("date" => "2016"),
  array("date" => "2016"),

  array("date" => "2015"),
  array("date" => "2015"),
  array("date" => "2015"),
  array("date" => "2015"),      

  array("date" => "2014"),
  array("date" => "2014"),

  array("date" => "2005"),
  );

foreach($datos as $key => $dt) {
    if(isset($last) && $last != $dt['date'])
        echo '--b--' . PHP_EOL . '--a--' . PHP_EOL;
    if(!isset($last))
        echo '--a--'. PHP_EOL;

    echo $dt['date']. PHP_EOL;

    if($key == count($datos) -1)
        echo '--b--';

    $last = $dt['date'];
}  

Demo: https://eval.in/570468

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.