在HTML表格中显示php数组

时间:2016-02-16 08:49:00

标签: php html arrays

我的数组看起来像

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
)

我希望以一种我应该得到的方式将这个数组放入HTML表中。

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  

我可以遍历一个数组值

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

这将给予。

id     group    lat      lng
        343     
        727     

 id     group    lat      lng
        555     
        7271   
        888   

如何循环多个数组值,以便我得到我所需的HTML表格格式

5 个答案:

答案 0 :(得分:2)

您需要将explode()用于group,lat和lng为:

$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>";

<强>结果:

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

答案 1 :(得分:1)

//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;
  } 
}

您将在$finalResult

中找到爆炸列表

输入数组

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

输出数组

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

所以你现在可以直接显示它

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>";

请注意,解决方案假设grouplat&amp;中有相同数量的元素可用。 lng

答案 2 :(得分:1)

我认为你可以这样做:

$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>";
}

答案 3 :(得分:1)

以下是您的解决方案: -

// 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

<强>输出: -

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

答案 4 :(得分:0)

那么你可以从我的顶部合并两个数组然后回声,看似最简单的方法Read doc

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