How to create a multi-dimensional array-php Tutorial

Source: Internet
Author: User
How to create a multi-dimensional array, my data has three columns, id (such as a1 \ a2 .. d1 \ d2), name (name), and age (age, number ). How to use PHP to create this multi-dimensional array?

Include ("IncDB. php ");

$ Result = mysql_query ("SELECT * FROM lists order by id asc", $ link); // all data in this table
$ Row = mysql_fetch_row ($ result );
$ Abcb = array (); // What should I do below? I did not learn it. Thank you.


Reply to discussion (solution)

While ($ row = mysql_fetch_row ($ result )){
$ Abcb [] = $ row;
}

Thank you! If I extract the id = d12 from $ abcb [], what should I do?

Use for loop traversal

For (){
If ($ abcb [$ I] [id] = 'd12 '){
Echo $ abcb [$ I] ['name']. abcb [$ I] ['age'];
}

}


For details, see the output array format.

I can use for, but if FOReach is used, how can I display all the names and ages in $ abcb? Thank you.

while($row=mysql_fetch_assoc($result)){    $abcb[] = $row;}foreach($abcd as $v) {  echo "$v[name] $v[age]
";}

Or
while($row=mysql_fetch_row($result)){    $abcb[] = $row;}foreach($abcd as $v) {  echo "$v[1] $v[2]
";}
Note: When using the lower mark, pay attention to the order of the field list during query.

Let me ask another question: what does foreach ($ abcd as $ v) mean? Can you explain it?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.