KH LIM
KH LIM

Reputation: 93

multiply by 2 arrays multi dimension

this the code of multiply by 2 arrays multi dimension:

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        $numberArray = array(
            array(1, 2, 3, 4, 7, 6),
            array(2, 3, 1, 0, 5)
        );
        printTable($numberArray);
        function printTable($numberArray) {
            // Placeholder
            $result = [];

            // Setup the multiplication
            foreach ($numberArray[1] as $key1 => $value1) {
                $tmp = array($value1); // add index y-axis
                foreach ($numberArray[0] as $key0 => $value0) {
                    $tmp[] = $value0 * $value1;
                }
                $result[] = $tmp;
            }

            // Add index the x-axis
            array_unshift($result, array_merge(array(" "), $numberArray[0]));

            // Loop through the $result array and display the table
            echo "<table border='1'>";
            foreach ($result as $key => $value) {
                echo "<tr>";
                foreach ($value as $k => $v) {
                    if ($k == 0 || $key == 0) {
                        echo sprintf("<td><b>%s</b></td>", $v);
                        continue;
                    }
                    echo "<td>$v</td>";
                }
                echo "</tr>";
            }
            echo "</table>";
        }

        ?>

    </body>
</html>
  1. still cannot run well, have some code still not very sure echo sprintf("<td><b>%s</b></td>", $v); anyone can help?
  2. setup all the multiply and array also cannot be display the result.
  3. the output should be like this example: here

Upvotes: 2

Views: 61

Answers (1)

AkshayP
AkshayP

Reputation: 2169

A function is a block of statements that can be used repeatedly in a program. A function will not execute immediately when a page loads. A function will be executed by a call to the function.

You have to call the function like below to execute your code,

printTable($numberArray);

So that your code will look like :

<?php
    $numberArray = array(
        array(1, 2, 3, 4, 7, 6),
        array(2, 3, 1, 0, 5)
    );
    printTable($numberArray); //write this function call here for    your expected result
    function printTable($numberArray) {
        // Placeholder
        $result = [];

Upvotes: 1

Related Questions