PHP全局数组正在消失

时间:2015-06-11 00:54:22

标签: php arrays multidimensional-array

这是我网站的菜单生成器。

<?php

$mx_grid = null;
$mx_generated_menu = '';
$mx_menu_colors = [
    'blue',
    'red',
    'blue',
    'green',
    'purple',
    ''
];

define('MENU_PAGE', 0);
define('MENU_ACTION', 1);
define('MENU_USER', 2);
define('MENU_LOCAL', 3);
define('MENU_ADMIN', 4);

for ($i = 0; $i < 32; $i++) {
    $mx_grid[$i] = [];
    for ($j = 0; $j < 6; $j++)
        $mx_grid[$i][$j] = ['null', 6];
}

print_r($mx_grid);

function _menu_add_cell($x, $y, $action, $type) {
    global $mx_grid;
    $arr = array($action, $type);
    $mx_grid[$y][$x] = $arr;
}

function _menu_compile() {
    global $mx_grid;
    global $mx_generated_menu;
    global $mx_menu_colors;
    $mx_menu_rows = 32;
    $mx_menu_cols = 6;

    print_r($mx_grid);

    $s_t = '';
    $s_r = '';
    $s_i = '';
    $s_c = '';
    for ($r = 0; $r < $mx_menu_rows; $r++) {
        $mx_row = $mx_grid[$r];
        $s_r = "\t<div class='mx_grid mx_menu_grid'>\n";
        for ($c = 0; $c < $mx_menu_cols; $c++) {
            $mx_cell = $mx_row[$c];
            $s_c = $mx_menu_colors[$mx_cell[1]];
            $s_i = "\t\t<div class='mx_grid_cell mx_menu_btn $s_c' href='$mx_cell[0]'></div>\n";
            $s_r .= $s_i;
        }
        $s_r .= "\t</div>\n";
        $s_t .= $s_r;
    }
    $mx_generated_menu = $s_t;
}

_menu_add_cell(1, 1, '/api/meh', MENU_PAGE);

print_r($mx_grid);

_menu_compile();

print_r($mx_grid);

问题是,在初始化循环和_menu_compile()调用之间,数组向南移动。

如果我在初始化后粘贴print_r($mx_grid)。循环:

Array ( 
  [0] => Array ( 
    [0] => Array ( 
      [0] => null [1] => 6 ) 
    [1] => Array ( 
      [0] => null [1] => 6 ) 
    [2] => Array ( 
      [0] => null [1] => 6 ) 
    [3] => Array ( 
      [0] => null [1] => 6 ) . . . .

漂亮,健康的阵列?正确?

_menu_add_cell()之后,同样的事情,除了我设置的内容。

_menu_compile()之后,同样的事情。

_menu_compile中,在引入全局变量之后:

Array ( [1] => Array ( [1] => Array ( [0] => /api/meh [1] => 0 ) ) ) 

就是这样。一切都是。内容是正确的,但阵列的其余部分在哪里?

1 个答案:

答案 0 :(得分:0)

不确定这是否会产生您正在寻找的输出......

<?php

$mx_grid = null;
$mx_generated_menu = '';
$mx_menu_colors = array(
    'blue',
    'red',
    'blue',
    'green',
    'purple',
    ''
);

define('MENU_PAGE', 0);
define('MENU_ACTION', 1);
define('MENU_USER', 2);
define('MENU_LOCAL', 3);
define('MENU_ADMIN', 4);

for ($i = 0; $i < 32; $i++) {
    $mx_grid[$i] = array();
    for ($j = 0; $j < 6; $j++)
        $mx_grid[$i][$j] = array('null', $j);
}

function _menu_add_cell($x, $y, $action, $type) {
    global $mx_grid;
    $arr = array($action, $type);
    $mx_grid[$y][$x] = $arr;
}

function _menu_compile() {
    global $mx_grid;
    global $mx_generated_menu;
    global $mx_menu_colors;
    $mx_menu_rows = 32;
    $mx_menu_cols = 6;

    $s_t = '';
    $s_r = '';
    $s_i = '';
    $s_c = '';
    for ($r = 0; $r < $mx_menu_rows; $r++) {
        $mx_row = $mx_grid[$r];
        $s_r = "\t<div class='mx_grid mx_menu_grid'>\n";
        for ($c = 0; $c < $mx_menu_cols; $c++) {
            $mx_cell = $mx_row[$c];
            $s_c = $mx_menu_colors[$mx_cell[1]];
            $s_i = "\t\t<div class='mx_grid_cell mx_menu_btn {$s_c}'   href='{$mx_cell[0]}'></div>\n";
            $s_r .= $s_i;
        }
        $s_r .= "\t</div>\n";
        $s_t .= $s_r;
    }
    $mx_generated_menu = $s_t;
    echo $mx_generated_menu;
 }

 _menu_add_cell(1, 1, '/api/meh', MENU_PAGE);

 _menu_compile();

我使用array()而不是[]因为我有一个旧版本的PHP

原始代码的问题包括一个超出范围的数组下标和其中一个字符串中的不正确格式。