这个代码乘以2个数组多维:
<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>
答案 0 :(得分:1)
函数是可以在程序中重复使用的语句块。页面加载时,函数不会立即执行。函数将通过调用函数来执行。
你必须调用下面的函数来执行你的代码,
printTable($numberArray);
这样您的代码将如下所示:
<?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 = [];