Append similar date record in one td

Viewed 73

I have following array of record which contains user as well as location information:

$arr =
   [
      [
         'user_id' => 5,
         'l_id' => 10,
         'Name' => 'John Doe',
         'Location' => 'Chicago',
         'date' => '2021-10-02'
      ],
      [
         'user_id' => 5,
         'l_id' => 11,
         'Name' => 'John Doe',
         'Location' => 'Houston',
         'date' => '2021-10-02'
      ],
      [
         'user_id' => 6,
         'l_id' => 12,
         'Name' => 'Rob Doe',
         'Location' => 'Dallas',
         'date' => '2021-10-02'
      ],
      [
         'user_id' => 6,
         'l_id' => 13,
         'Name' => 'Rob Doe',
         'Location' => 'Philadelphia',
         'date' => '2021-10-02'
      ],

   ];

I'm trying to display same user/day location record in one td by using following code:

$html = '';
foreach ($arr as $ar) {

   if ($ar['date'] == $ar['date']) {

      $html .= '<tr>
                     <td>' . $ar['Name'] . '</td>
                     <td>' . $ar['date'] . '</td>
                     <td>' . $ar['Location'] . '</td>
                </tr>';
   }
}

HTML:

<table>
      <thead>
         <tr>
            <th>Name</th>
            <th>Date</th>
            <th>Location</th>
         </tr>
      </thead>
      <tbody>
         <?= $html; ?>
      </tbody>
   </table>

But it's repeating same date record multiple times (Name, Date):

Name        Date          Location
John Doe    2021-10-02    Chicago
John Doe    2021-10-02    Houston
Rob Doe     2021-10-02    Dallas
Rob Doe     2021-10-02    Philadelphia

And I want result like this:

Name        Date            Location
John Doe    2021-10-02      Chicago
                            Houston
--------------------------------------------
Rob Doe     2021-10-02      Dallas
                            Philadelphia
3 Answers

Try this code which print name and date only the first time:

$html = '';
$count=0;
foreach ($arr as $ar) {
 
 if ($ar['date'] == $ar['date']) {
 if($count == 0){
  $html .= '<tr>
                 <td>' . $ar['Name'] . '</td>
                 <td>' . $ar['date'] . '</td>
                 <td>' . $ar['Location'] . '</td>
            </tr>';
 $count++;
 }
else{
     $html .= '<tr>
                 <td></td>
                 <td></td>
                 <td>' . $ar['Location'] . '</td>
            </tr>';
$count=0;
}
}
}

Use this function for creating your required data:

function arrayChanger($oldArray) {
    $newArray = array();
    foreach ($oldArray as $v) {
        if (!isset($newArray[$v['user_id']])) {
            $newArray[$v['user_id']] = array(
                'user_id' => $v['user_id'],
                'dataArray' => array()
            );
        }
        $newArray[$v['user_id']]['dataArray'][] = array(
            'l_id' => $v['l_id'],
            'Name' => $v['Name'],
            'Location' => $v['Location'],
            'date' => $v['date'],
        );
    }
    return $newArray;
}

And then you can use that by your array:

$html = '<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Date</th>
            <th>Location</th>
        </tr>
    </thead>
<tbody>';
foreach (arrayChanger($arr) as $newArrayKey => $newArrayValue) {
    if (count($newArrayValue['dataArray']) <= 1) {
        $html .= '<tr>
                    <td>' . $value['Name'] . '</td>
                    <td>' . $value['date'] . '</td>
                    <td>' . $value['Location'] . '</td>
                </tr>';
    } else {
        $i = 0;
        foreach ($newArrayValue['dataArray'] as $key => $value) {
            $html .= '<tr>';
                ($i === 0) ? $html .= '<td>' . $value['Name'] . '</td>' : $html .= '<td>  </td>';
                ($i === 0) ? $html .= '<td>' . $value['date'] . '</td>' : $html .= '<td>  </td>';
                $html .= '<td>' . $value['Location'] . '</td>';
            $html .= '</tr>';
            $i = $i + 1;
        }
    }
}
$html .= '</tbody>
</table>';

The output would be as your need:

Name Date Location
John Doe 2021-10-02 Chicago
Houston
Rob Doe 2021-10-02 Dallas
Philadelphia

Edit:

As changes in question, I changed my answer so we have the function:

function arrayChanger($oldArray)
{
    $newArray = array();
    foreach ($oldArray as $v) {
        if (!isset($newArray[$v['user_id']])) {
            $newArray[$v['user_id']] = array(
                'user_id' => $v['user_id'],
                'l_id' => $v['l_id'],
                'Name' => $v['Name'],
                'date' => $v['date'],
                'UserLocations' => array()
            );
        }
        $newArray[$v['user_id']]['UserLocations'][] = array(
            'Location' => $v['Location'],
        );
    }
    return $newArray;
}

And after that creating the HTML:

$html = '<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Date</th>
            <th>Location</th>
        </tr>
    </thead>
<tbody>';
foreach (arrayChanger($arr) as $newArrayKey => $newArrayValue) {

    $html .= '<tr>
        <td>' . $newArrayValue['Name'] . '</td>
        <td>' . $newArrayValue['date'] . '</td>
        <td>';

        foreach ($newArrayValue['UserLocations'] as $key => $value) {
            $html .= $value['Location'] . "<br>";
        }

    $html .= '</td>
        </tr>';
}

$html .= '</tbody>
</table>';

The output will:

Name Date Location
John Doe 2021-10-02 Chicago
Houston
Rob Doe 2021-10-02 Dallas
Philadelphia

You can make an new Array where you merge the date and location.

$newArr = [];
foreach($arr as $item) {    
    if(isset($newArr[$item['Name']])) {
        $newArr[$item['Name']]['Location'][] = $item['Location'];
        $newArr[$item['Name']]['date'][] = $item['date'];        
    } else {
        $newArr[$item['Name']]['user_id'] = $item['user_id'];
        $newArr[$item['Name']]['l_id'] = $item['l_id'];
        $newArr[$item['Name']]['Name'] = $item['Name'];
        $newArr[$item['Name']]['Location'] = [$item['Location']];
        $newArr[$item['Name']]['date'] = [$item['date']];
    }
}

print_r($newArr);

output would be:

Array
(
    [John Doe] => Array
        (
            [user_id] => 5
            [l_id] => 10
            [Name] => John Doe
            [Location] => Array
                (
                    [0] => Chicago
                    [1] => Houston
                )

            [date] => Array
                (
                    [0] => 2021-10-02
                    [1] => 2021-10-02
                )

        )

    [Rob Doe] => Array
        (
            [user_id] => 6
            [l_id] => 12
            [Name] => Rob Doe
            [Location] => Array
                (
                    [0] => Dallas
                    [1] => Philadelphia
                )

            [date] => Array
                (
                    [0] => 2021-10-02
                    [1] => 2021-10-02
                )

        )

)

Then you can use your loop. And for the elements date and location you can use the php implode (implode(',', [])) function. That will be the easiest way.

Related