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

时间:2015-02-22 08:35:49

标签: php arrays multidimensional-array

我试图通过一个接收两个变量($ 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"),
    );

3 个答案:

答案 0 :(得分:1)

您的代码中有一些错误:

(旁注:由于您为数组元素指定了html标记,请右键单击 - &gt;查看源代码以查看数组)

$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);

答案 1 :(得分:0)

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;
}

答案 2 :(得分:0)

您只需使用两个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;
    }
}