0

I am trying to print a 3-dimensional array into a table. But the indexes are kinda fked up. When I use the following (psuedo)code:

...
<<print headers and stuff>>

for ( $i = 0; $i < count( $array ); i++) {
    $itemArray = $array[i];

    for ( $j = 0; $j < count( $itemArray; j++) {
        $innerItem = $itemArray[j];
        echo <<tr start + both indexes in td>>

        foreach ($innerItem as $spec) {
            echo <<td with item>>
        }

        echo <<tr stop>>
    }
}

In this example I am using i as index for the outer array and j as an index for the inner array (pretty obvious). The result I am getting from this is as follows:

| index i | index j | title1 | title2 |
|    0    |    0    |        |        |
|    1    |    0    |        |        |
|    2    |    0    |        |        |
|   ...   |   ...   |        |        |

Whilst I would expect:

| index i | index j | title1 | title2 |
|    0    |    0    |        |        |
|    0    |    1    |        |        |
|    1    |    0    |        |        |
|    1    |    1    |        |        |
|    1    |    2    |        |        |
|    2    |    0    |        |        |
|   ...   |   ...   |        |        |

The (original) full code is:

echo "<h1>Combat analysis</h1>";
echo '<table cellspacing="0" cellpadding="4" border="1"><tbody>';
echo "<tr><td>#Mon</td><td>#Att</td><td>DungLVL</td><td>CharLVL</td><td>Health</td><td>Weapon</td><td>No. potions</td></tr>";
for ($battleIndex  = 0; $battleIndex < count($this->combatLog); $battleIndex++) {
    $battle = $this->combatLog[$battleIndex];
    for ($attackIndex = 0; $attackIndex < sizeof($battle); $attackIndex++) {
        $attack = $battle[$attackIndex];
        echo "<tr><td>" . $battleIndex . "</td><td>" . $attackIndex . "</td>";
        foreach ($attack as $stat) {
            echo "<td>" . $stat . "</td>";
        }
        echo "</tr>";
    }
}
echo "</tbody></table>";

What is going wrong?

1
  • 4
    This just means that all your battles have size 1. Most likely the error is in the way you populate them, not in this code. Commented Feb 3, 2015 at 1:13

1 Answer 1

1

Tested your code and runs as expected. You should do a echo '<pre>'.print_r($this->combatLog).'</pre>'; and debug the array contents.

Also I would recommend you the following:

1) You can use foreach instead of for, example: foreach ($this->combatLog as $battleIndex => $battle)

2) If you're not sure that a array contains values you should first do a: if (is_array($this->combatLog) && count($this->combatLog) > 0)

3) For simplicity and code maintenance I would first loop the multi-dimensional array and turn it into a one dimension called $attacks containing a array per each attack indexed by keys that you can recognize, ej:

$attacks=array();
$attacks[]=array(
'Mon'=>$battleIndex,
'Att'=>$attackIndex,
'DungLVL'=>isset($stat[0])?$stat[0]:null,
'CharLVL'=>isset($stat[1])?$stat[1]:null,
'Health'=>isset($stat[2])?$stat[2]:null,
'Weapon'=>isset($stat[3])?$stat[3]:null,
'Potions'=>isset($stat[4])?$stat[4]:null,
);

Then you could define some columns for example:

$columns=array(
    'Mon',
    'Att',
    'DungLVL',
    'CharLVL',
    'Health',
    'Weapon',
    'Potions',
);

Then print the table header like this:

echo '<tr>';
foreach ($columns as $column) {
  echo '<td>'.$column.'</td>';
}
echo '</tr>';

And print rows like this:

foreach ($attacks as $attack) {
  echo '<tr>';
  foreach ($columns as $column) {
    echo '<td>'.$attack[$column].'</td>';
  }
  echo '</tr>';
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.