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
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
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments