PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Wed Feb 08, 2006 5:20 am
I am stumped on this one.
Here is an example of the array structure:
Array
(
[0] => Array
(
[One] => 1
[Two] => 6
[Three] => 4
[Four] => 38
)
[1] => Array
(
[One] => 16
[Two] => 2
[Three] => 0
[Four] => 156
)
[2] => Array
(
[One] => 4
[Two] => 1
[Three] => 0
[Four] => 19522
)
[3] => Array
(
[One] => 5
[Two] => 1
[Three] => 1
[Four] => 0
)
)
And here is the code I wrote which does not work because the $Field value is always "Array":
Code: Select all
foreach ($Array as $Field) {
foreach ($Field as $Data) {
echo "<b>" . current($Field) . "</b><br />";
switch ($Field) {
case "One":
$Name = get_name($Data);
$Table .= " <tr>\n";
$Table .= " <td>$Name</td>\n";
break;
case "Two":
$Table .= " <td>$Data</td>\n";
break;
case "Three":
$Table .= " <td>$Data</td>\n";
break;
case "Four":
$Table .= " <td>$Data</td>\n";
$Table .= " <td> </td>\n";
$Table .= " </tr>";
break;
}
}
}
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Wed Feb 08, 2006 5:27 am
shouldn't it be something like this ?:
Code: Select all
foreach ($Array as $outerKey => $Field) {
foreach ($Field as $innerKey => $Data) {
echo "<b>" . $outerKey . "</b><br />";
switch ($outerKey) {
case 1:
$Name = get_name($Data);
$Table .= " <tr>\n";
$Table .= " <td>$Name</td>\n";
break;
case 2:
$Table .= " <td>$Data</td>\n";
break;
case 3:
$Table .= " <td>$Data</td>\n";
break;
case 4:
$Table .= " <td>$Data</td>\n";
$Table .= " <td> </td>\n";
$Table .= " </tr>";
break;
}
}
}
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Wed Feb 08, 2006 5:32 am
Well that is closer as case 1 did fire but it still didn't really work. Here is the output:
<tr>
<td>Name 1</td>
<tr>
<td>Name 2</td>
<tr>
<td></td>
<tr>
<td></td>
<td>4</td>
<td>1</td>
<td>0</td>
<td>19522</td>
<td>5</td>
<td>1</td>
<td>1</td>
<td>0</td>
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Wed Feb 08, 2006 6:09 am
Well I don't know if this is the right way to do it or not but it works.
Code: Select all
foreach ($Array as $Field) {
echo "Field 1 " . current($Field) . "<br>";
echo "Field 2 " . next($Field) . "<br>";
echo "Field 3 " . next($Field) . "<br>";
echo "Field 4 " . next($Field) . "<br>";
}
m3mn0n
PHP Evangelist
Posts: 3548 Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada
Post
by m3mn0n » Wed Feb 08, 2006 6:13 am
Don't do a foreach directly inside another foreach... simply use keys for the $Field variable. To get a better understanding of what I mean, run this:
Code: Select all
<?php
foreach ($Array as $Field) {
echo "<pre>";
print_r ($Field);
echo "</pre>";
echo "<br /><br />";
}
?>
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Feb 08, 2006 8:58 am
I prefer doing this kinda stuff like so:
Code: Select all
foreach($Array as $record) {
echo '<tr><td>' . implode('</td><td>' . $record) . '</td><tr>' . "\n";
}
raghavan20
DevNet Resident
Posts: 1451 Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:
Post
by raghavan20 » Wed Feb 08, 2006 3:05 pm
feyd wrote: I prefer doing this kinda stuff like so:
Code: Select all
foreach($Array as $record) {
echo '<tr><td>' . implode('</td><td>' . $record) . '</td><tr>' . "\n";
}
what is so interesting point here is...the style in which he wanted to display is not mentioned unless i terribly read the post...if you want to display in tables as just one row for every single array contained...
I have considered arrays to be of different sizes but this code should work in any case...
Code: Select all
echo "<table>";
foreach($tempArray as $anotherArray){
echo "<tr>";
for ($i = 0; $i < count($anotherArray); $i++){ //considering that there are variable number of elements in each array
echo "<td>".$anotherArray[$i]."</td>";
}
echo "</tr>";
}
echo "</table>";