0
$thearray = array
(
    //(0)ID, (1)NAME, (2)LOCATION, (3)PHONE
    array("0","Name 1","Nowhere 11","0004444"),
    array("1","Name 2","Everywhere 11","0005555"),
    array("2","Name 3","ThisPlace 11","0002222"),
    array("3","Name 4","NoPlace 11","0003333"),
    array("4","Name 5","ThatPlace 11","0001111")
);

这就是我用来存储我的信息的方式,然后我会遍历它们以找到我需要的内容并使用例如显示它们

echo $thearray[$i][4]

除了将信息存储在 Mysql 中之外,我想做同样的事情这是我已经走了多远,但我不断收到奇怪的错误,我无法从数组中输出

这就是我已经走了多远

$result = $db->query("SELECT * FROM table");
$thearray = array();
while($thearray = $result->fetch_assoc()){
   $thearray[] = $thearray;
}

出于某种原因,这对我不起作用,它就像我上面所说的那样不在二维数组中:SI 不能像以前那样简单地回显它。

4

1 回答 1

0

好吧,首先

while($thearray = $result->fetch_assoc()){
    $thearray[] = $thearray;
}

将继续覆盖数组并变得非常困惑尝试

while($row = $result->fetch_assoc()){
    $thearray[] = $row;
}

从您的查询返回的数组不再是一个简单的数组,它是一个关联数组

即它看起来像这样:

$new_array = array
(
//(0)ID, (1)NAME, (2)LOCATION, (3)PHONE
    array('ID' => "0", 'NAME' => "Name 1", 'LOCATION' => "Nowhere 11", 'PHONE' => "0004444"),
    array('ID' => "1", 'NAME' => "Name 2", 'LOCATION' => "Nowhere 22", 'PHONE' => "0005555"),
    etc
);

所以现在当你想使用它时,你将不得不使用这种构造

$name     = $new_array[$i]['NAME']
$location = $new_array[$i]['LOCATION']
etc

或者

<?php echo $new_array[$i]['NAME']; ?>
<?php echo $new_array[$i]['LOCATION'] ?>
etc
于 2013-10-05T16:04:31.597 回答