如何在php中自动生成给定两个变量的多维数组


How to auto generate a multidimensional array given two variables in php

我正试图通过一个函数生成一个多维数组,该函数接收两个变量($a和$b),并应返回一个多维阵列,其中$a和$b决定多维阵列的宽度和高度。我做错了什么,但我不知道是什么。提前感谢answe

<?php
$map = array();
print_r($map);
function mapGenerator($a, $b) {
    $hex = '<div class="hex"><div class="top"></div><div class="middle"></div><div class="bottom"></div></div>';
    $hexEven = '<div class="row even">';
    $hexOdd = '<div class="row">';
    $rowEnd = '</div><!-- END ROW -->';
    for ($row = 0; $row < $b; ++$row) {
        if ($row % 2 == 0) {    
            $map[$row][0] = "$hexEven";
        } else {
            $map[$row][0] = "$hexOdd";
        }
        for ($cell = 0; $cell <= $a; ++$cell) {
            $map[$row][$cell] = "$hex";
        }
        ++$cell;
        $map[$row][$cell] = "$rowEnd";
    }
    return $map;
}
mapGenerator(4,7);
echo "<br>";
print_r($map);
?>

最终结果应该是这样的:

$map = array(
    array("$hexOdd","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$rowEnd"),
    array("$hexEven","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$rowEnd"),
    array("$hexOdd","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$rowEnd"),
    array("$hexEven","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$rowEnd"),
    array("$hexOdd","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$rowEnd"),
    array("$hexEven","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$rowEnd"),
    array("$hexOdd","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$rowEnd"),
    array("$hexEven","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$hex","$rowEnd"),
    );

您的代码中有一些错误:

(旁注:由于您为数组元素分配了html标签,右键单击->查看源代码即可查看您的数组)

$map = array();
print_r($map);
function mapGenerator($a, $b) {
    $hex = '<div class="hex"><div class="top"></div><div class="middle"></div><div class="bottom"></div></div>';
    $hexEven = '<div class="row even">';
    $hexOdd = '<div class="row">';
    $rowEnd = '</div><!-- END ROW -->';
    for ($row = 0; $row < $a*2; ++$row) {
                        //^^^^ Use $a and multiple it with 2 if you want 8 innerArrays with your current function call
        if ($row % 2 == 0) {    
            $map[$row][0] = "$hexOdd";
                           //^^^^^^^ Switched the variables
        } else {
            $map[$row][0] = "$hexEven";
        }
        for ($cell = 1; $cell <= $b*2-1; ++$cell) {
                   //^           ^^^^^^ Use $b and multiple it with 2 minus 1 if you want 13 array elements with your current function call
                   //| Begin with 1 since 0 already has a value
            $map[$row][$cell] = "$hex";
        }
        //removed unnecessary increment of $cell 
        $map[$row][$cell] = "$rowEnd";
    }
    return $map;
}
$map = mapGenerator(4,7);
//^ Don't forget to assign the function call
echo "<br>";
print_r($map);

您只需要使用两个for循环,一个创建"行",另一个在"行"内创建"列"。像这样的东西可以做到:

$array  = array();
$size   = 10;
$filler = "VALUE";
for ($i = 0; $i < $size; $i++) {
    // Create "rows"
    $array[$i] = array();
    for ($j = 0; $j < $size; $j++) {
        // Add "columns" to the "rows"
        $array[$i][$j] = $filler;
    }
}
function mapGenerator($a, $b) {
    global $map; /* add this or just define $map=array(); inside the function (better) */
    $hex = '<div class="hex"><div class="top"></div><div class="middle"></div><div class="bottom"></div></div>';
    $hexEven = '<div class="row even">';
    $hexOdd = '<div class="row">';
    $rowEnd = '</div><!-- END ROW -->';
    for ($row = 0; $row < $b; $row++) { /* Inverted ++$row with $row++ */
        $map[$row] = array(); /* Add this line */
        if ($row % 2 == 0) {    
            $map[$row][0] = "$hexEven";
        } else {
            $map[$row][0] = "$hexOdd";
        }
        for ($cell = 0; $cell <= $a; $cell++) { /* Inverted ++$cell with $cell++ */
            $map[$row][$cell] = "$hex";
        }
        $cell++;
        $map[$row][$cell] = "$rowEnd";
    }
    return $map;
}