1

been using most of the day to solve my problem - and i am about to give up.

I have an array looking like this:

array(2) {
  [1]=>
  array(5) {
    [0]=>
    array(6) {
      ["name"]=>
      string(15) "Henning smaall"
      ["drivernumber"]=>
      string(4) "8830"
      ["period_start"]=>
      string(10) "2018-07-20"
      ["period_end"]=>
      string(10) "2018-07-31"
      ["status"]=>
      string(1) "0"
      ["note"]=>
      string(9) "Operation"
    }
    [1]=>
    array(6) {
      ["name"]=>
      string(15) "Henning smaall"
      ["drivernumber"]=>
      string(4) "8830"
      ["period_start"]=>
      string(10) "2018-07-17"
      ["period_end"]=>
      string(10) "2018-07-17"
      ["status"]=>
      string(1) "0"
      ["note"]=>
      string(0) ""
    }
    [2]=>
    array(6) {
      ["name"]=>
      string(15) "Henning smaall"
      ["drivernumber"]=>
      string(4) "8830"
      ["period_start"]=>
      string(10) "2018-07-16"
      ["period_end"]=>
      string(10) "2018-07-16"
      ["status"]=>
      string(1) "1"
      ["note"]=>
      string(0) ""
    }
    [3]=>
    array(6) {
      ["name"]=>
      string(15) "Henning smaall"
      ["drivernumber"]=>
      string(4) "8830"
      ["period_start"]=>
      string(10) "2018-07-27"
      ["period_end"]=>
      string(10) "2018-07-27"
      ["status"]=>
      string(1) "0"
      ["note"]=>
      string(0) ""
    }
    [4]=>
    array(6) {
      ["name"]=>
      string(15) "Henning smaall"
      ["drivernumber"]=>
      string(4) "8830"
      ["period_start"]=>
      string(10) "2018-07-31"
      ["period_end"]=>
      string(10) "2018-07-31"
      ["status"]=>
      string(1) "1"
      ["note"]=>
      string(0) ""
    }
  }
  [13]=>
  array(2) {
    [0]=>
    array(6) {
      ["name"]=>
      string(15) "Henrik Hjersing"
      ["drivernumber"]=>
      string(4) "8850"
      ["period_start"]=>
      string(10) "2018-07-10"
      ["period_end"]=>
      string(10) "2018-07-24"
      ["status"]=>
      string(1) "0"
      ["note"]=>
      string(0) ""
    }
    [1]=>
    array(6) {
      ["name"]=>
      string(15) "Henrik Hjersing"
      ["drivernumber"]=>
      string(4) "8850"
      ["period_start"]=>
      string(10) "2018-07-18"
      ["period_end"]=>
      string(10) "2018-08-01"
      ["status"]=>
      string(1) "0"
      ["note"]=>
      string(11) "asdasdasdad"
    }
  }
}

The output i am looking for should be something like

Henning smaall (8830)
2018-07-20 - 2018-07-31
2018-07-17 - 2018-07-17
2018-07-16 - 2018-07-16
2018-07-27 - 2018-07-27
2018-07-31 - 2018-07-31

Henrik Hjersing (8850)
2018-07-10 - 2018-07-24
2018-07-18 - 2018-08-01

For now i am using dummy data and easy php testcode - but i keep getting the name reccuring in the output.

Database function:

public function resultsetGroup(){
    $this->execute();
    return $this->stmt->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_ASSOC);
}

SQL function:

public function getVacations(){
  //$this->db->query('SELECT * FROM vacation ORDER BY user_id, status, period_start');
  $this->db->query('SELECT u.id, u.name, u.drivernumber, v.period_start, v.period_end, v.status, v.note FROM users u, vacation v where u.id = v.user_id');

  $results = $this->db->resultsetGroup();

  return $results;
}

This is the php code i've been trying to alter in 500 different ways, and still i cannot get the result i am after - if i do not pdo group by u.id but u.name - it works, but then again, 2 people can have the same name - and that will break my intention.

<?php var_dump($data['vacations']); ?>
<?php foreach($data['vacations'] as $key => $vac) : ?>
    <?php echo $key; ?>
    <?php foreach($vac as $va) : ?>
    <?php echo key($vac);?>
    <h1><?php echo $va['name'] ?></h1>
    <?php endforeach ; ?>   
<?php endforeach ; ?>

Can you pls help me out? i feel like im running around in cirkles.

Barmar
  • 741,623
  • 53
  • 500
  • 612
fjappe
  • 49
  • 5

1 Answers1

-1
enter code here
Try this:

$vacations = array();
foreach($data['vacations'] as $key => $value){
   foreach($value as $index => $person){
      $vacations[$person['drivernumber']] = array();
      $vacations[$person['drivernumber']]['name'] = $person['name'];
      $vacations[$person['drivernumber']]['drivenumber'] = $person['drivenumber'];
      $vacations[$person['drivernumber']]['vacations'] = "";
   }
}// With this, you will have the array $vacations with the drivernumbers as keys without duplicates and initialized the name, drivenumber and vacations string.

foreach($data['vacations'] as $key => $value){
   foreach($value as $index => $person){
       $vacations[$person[drivenumber]]['vacations'] += $person['period_start'] . "-" . $person['period_end'] . " ";
   }
}//This will concatenate all the period_start and period_end of each person in their respective position in the array.

foreach($vacations as $key => $value){
    echo $value['name'] . " " . $value['drivernumber'] . " " . $value['vacations'];
}//Finally.. print in the format that you are requesting.

Please try.