Matarishvan
Matarishvan

Reputation: 2422

Display php array in a HTML Table

My array looks like

Array
(
    [id] => 1
    [group] => 343,727
    [lat] => 12,14
    [lng] => 20,35  
)
Array
(
    [id] => 2
    [group] => 555,7271,888
    [lat] => 55,32,98
    [lng] => 99,74,26
)

I want to put this array into a HTML table in such a way that i should get.

id     group    lat      lng
1       343     12       20
1       727     14       35


id     group    lat   lng
2       555     55    99
2      7271     32    74
2       888     98    26  

i could loop through one array value

$group = $rs[$i]['group'];
$myArray = explode(',', $group);
foreach($myArray as $key) {
    ?><tr><td><?php echo $key;?>"</td></tr><?php
}

This will give.

id     group    lat      lng
        343     
        727     

 id     group    lat      lng
        555     
        7271   
        888   

How do i loop multiple array values so that i should get my required HTML table format

Upvotes: 1

Views: 4252

Answers (5)

Ravi Hirani
Ravi Hirani

Reputation: 6539

Here is your solution:-

// Define a result array
$result = [];
// first loop
foreach($arr as $k=>$val){  
    $id = $val['id'];
    $groupArr = explode(",",$val['group']); // get all group ids
    $latArr = explode(",",$val['lat']); // get lat array
    $langArr = explode(",",$val['lng']); // get lng array
  // second loop for group array
  foreach($groupArr as $key=>$group){
      $temp = []; // define a temp array
      $temp['id'] = $id; // assign id
      $temp['group'] = $groupArr[$key]; // assign group
      $temp['lat'] = $latArr[$key]; // assign latitude
      $temp['lng'] = $langArr[$key]; // assign langtitude
      $result[$k][] = $temp;  // assign record to $result array
  } 
}    

// Table start
echo "<table>";
foreach ($result as $items) { 
  echo "<tr>";
     echo "<th>id</th>";
     echo "<th>group</th>";
     echo "<th>lat</th>";
     echo "<th>lng</th>";
  echo "</tr>";
    foreach($items as $item){      
       echo "<tr>";
         echo "<td>{$item['id']}</td>";
         echo "<td>{$item['group']}</td>";
         echo "<td>{$item['lat']}</td>";
         echo "<td>{$item['lng']}</td>";
       echo "</tr>";
    }
}
echo "</table>";
// Table end

output:-

id  group   lat lng
1   343     12  20
1   727     14  35
id  group   lat lng
2   555     55  99
2   7271    32  74
2   888     98  26

Upvotes: 1

devpro
devpro

Reputation: 16117

You need to use explode() for group, lat and lng as:

$array1 = 
    array(
    array(
        'id'=>1,
        'group'=>'343,727',
        'lat'=>'12,14',
        'lng'=>'20,35'
    ),
    array(
        'id'=>2,
        'group'=>'555,7271,888',
        'lat'=>'55,32,98',
        'lng'=>'99,74,26'
    )    
    );

echo "<table border='1'>";
foreach ($array1 as $key => $value) {
    $explodedGroup[$value['id']] = explode(",",$value['group']);
    $explodedlat[$value['id']] = explode(",",$value['lat']);
    $explodedlng[$value['id']] = explode(",",$value['lng']);    
}
//print_r($explodedGroup);

foreach ($explodedGroup as $key => $value) {
    echo "<tr>";
        echo "<td> ID </td>";
        echo "<td> Group </td>";
        echo "<td> Lat</td>";
        echo "<td> Lng </td>";
    echo "</tr>";
    foreach ($value as $key2 => $finalVal) {
        echo "<tr>";
        echo "<td> {$key}</td>"; // will print the id
        echo "<td> {$finalVal}</td>"; will print the group value
        echo "<td> {$explodedlat[$key][$key2]}</td>"; // will print lat as per index
        echo "<td> {$explodedlng[$key][$key2]}</td>"; // will print lng as per index
        echo "</tr>";
    }

}
echo "</table>";

Result:

ID  Group   Lat Lng
1   343     12  20
1   727     14  35
ID  Group   Lat Lng
2   555     55  99
2   7271    32  74
2   888     98  26

Upvotes: 2

Chey Luna
Chey Luna

Reputation: 70

I think you can do it like this:

$rows = <your array>;

foreach ($rows as $row) {
    echo "<table>";
    echo "<tr><td>id</td><td>group</td><td>lat</td><td>lng</td></tr>";

    $groups = explode(",", $row['group']);
    $lats = explode(",", $row['lat']);
    $lngs = explode(",", $row['lng']);

    foreach ($groups as $key => $group) {
        echo "<tr>";
            echo "<td>" . $row['id'] . "</td>";
            echo "<td>" . $group . "</td>";
            echo "<td>" . $lats[$key] . "</td>";
            echo "<td>" . $lngs[$key] . "</td>";
        echo "</tr>";
    }

    echo "</table>";
}

Upvotes: 1

Jimish Gamit
Jimish Gamit

Reputation: 1024

//PHP CODE
$finalResult = array();

foreach($rs as $result){
  $id = $result['id'];
  $groupArray = explode(",",$result['group']);
  $latArray = explode(",",$result['lat']);
  $lngArray = explode(",",$result['lng']);

  foreach($groupArray as $index=>$group){
    $tempResult = array();
    $tempResult['id'] = $id;
    $tempResult['group'] = $groupArray[$index];
    $tempResult['lat'] = $latArray[$index];
    $tempResult['lng'] = $lngArray[$index];

    $finalResult[] = $tempResult;
  } 
}

You will have exploded list in $finalResult

Input array

Array
(
    [id] => 1
    [group] => 343,727
    [lat] => 12,14
    [lng] => 20,35  
)

Output array

Array
(
    [id] => 1
    [group] => 343
    [lat] => 12
    [lng] => 20
)
Array
(
    [id] => 1
    [group] => 727
    [lat] => 14
    [lng] => 35  
)

So you can display it straight forward now

echo "<table>";
foreach ($finalResult as $item) {
    echo "<tr>";
       echo "<td>".$item['id']."</td>";
       echo "<td>".$item['group']."</td>";
       echo "<td>".$item['lat']."</td>";
       echo "<td>".$item['lng']."</td>";
     echo "</tr>";
}
echo "</table>";

Please note that solution has assumption that same number of elements will be available in group,lat & lng

Upvotes: 1

L.Lawliet
L.Lawliet

Reputation: 113

Well you can merge two arrays from the top of my had then echo , seems like a easiest way Read doc

array array_merge ( array $array1 [, array $... ] )

Upvotes: 0

Related Questions