Home > Backend Development > PHP Tutorial > 怎么建多维数组

怎么建多维数组

WBOY
Release: 2016-06-23 13:21:52
Original
1043 people have browsed it

大神们,我的数据有3列,id(如a1\a2..d1\d2之类的),name(就是人名),和age(年龄,数字)。怎么用PHP建这个多维数组?

include("IncDB.php"); 

$result=mysql_query("SELECT * FROM lists order by id asc",$link); //数据全在这个表里
$row=mysql_fetch_row($result); 
$abcb=array();//下面怎么办?我看了,没学会。谢谢大神。


回复讨论(解决方案)

while($row=mysql_fetch_row($result)){
    $abcb[] = $row;
}

谢谢!那我要是提取$abcb[]中id=d12,用户的姓名和年龄,该怎么写呢?

用for循环遍历

for(){
if($abcb[$i][id] == 'd12'){
    echo $abcb[$i]['name'].abcb[$i]['age'];
}

}


具体 看输出的数组形式

用for,我知道可以,但如果 用 FOReach,怎么把$abcb[]中的所有姓名和年龄显示出来呢?谢谢大神们。

while($row=mysql_fetch_assoc($result)){    $abcb[] = $row;}foreach($abcd as $v) {  echo "$v[name] $v[age]<br>";}
Copy after login


while($row=mysql_fetch_row($result)){    $abcb[] = $row;}foreach($abcd as $v) {  echo "$v[1] $v[2]<br>";}
Copy after login
注意:使用下标时要注意查询时字段列表的循序

我再追问一句,foreach($abcd as $v),这是什么意思呢?能解释一下么?

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template