下面的拉取数据应该返回:
但是最后一行var_dump($result_value);
返回时没有z:
我在这里因为在$ result_value中有'z'而错过了什么?
$pullMapInfo = "SELECT x, y, z, value FROM mapinfo WHERE id='{$player_id}'";
$pullMapInfo2 = mysql_query($pullMapInfo) or die($error[4]);
//create an array with all x, y, z
for ($y = 1; $y <= 16; $y++) $array_y[] = $y;
for ($x = 1; $x <= 16; $x++) $array_x[] = $x;
for ($z = 1; $z <= 3; $z++) $array_z[] = $z;
//create an associative array x, y, z => value
$result_value = array();
while ( $pullMapInfo3 = mysql_fetch_assoc($pullMapInfo2) ) {
$result_value[ $pullMapInfo3['x'] ][ $pullMapInfo3['y'] ][ $pullMapInfo3['z'] ] = $pullMapInfo3['value'];
}
//loop to display output
foreach ($array_z as $z) {
echo '<div class="container">';
foreach ($array_y as $y) {
foreach ($array_x as $x) {
if (array_key_exists($x, $result_value) && array_key_exists($y, $result_value[$x] )) {
echo '<div class="tileBox pos_',$result_value[$x][$y][$z] ,'" id="'.$x.','.$y.','.$z.'"></div>';
} else {
echo '<div class="tileBox pos_0" id="'.$x.','.$y.','.$z.'"></div>
';
}
}
}
echo '</div>';
}
var_dump($result_value);
#outputs: array(1) { [1]=> array(1) { [1]=> array(1) { [1]=> string(1) "3" } } }
#as it pulled data from database: x, y, value - it misses z!
表格结构: id INT(11)
x,y,z TINYINT(2)
值VARCHAR(10)
var_dump($pullMapInfo3)
@Fluffeh:
bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) ... x768 (16x16x3)
答案 0 :(得分:2)
Z在那里。如果将输出拆分,则可以看到它有三个维度,最后一个是Z
array(
[x=1] => array(
[y=1] => array(
[z=1] => "3"
)
)
所以$ result_value [$ x] [$ y] [$ z] = 3
答案 1 :(得分:0)
用于在php中创建多维数组
$newarr[array_pop($arr)] = 0;
foreach (array_reverse($arr) as $i)
$newarr[$i] = $newarr;
检查此SO post有更多解释。