PHP foreach output two-dimensional array

Source: Internet
Author: User
Tags foreach array key php foreach

/*

* Title: foreach Output two-dimensional array

* Function: foreach ()

* Date Created: May 16, 2015

* Precautions:

*/

Output one-dimensional array

$array 1 = array ();

$array 1[0] = ' chu Liu Xiang ';

$array 1[1] = ' hu iron flower ';

$array 1[2] = ' Guibing yan ';

Only the case of value, which is the output value only.

foreach ($array 1 as $value) {

echo $value. ' ';

}

Echo '

';

Output key value pairs

foreach ($array 1 as $key => $value) {

echo $key. ': $value;

Echo '
';

}

Echo '

';

Output two-dimensional array

$array 2 = array ();

$array 2[0][' author '] = ' first message person ';

$array 2[0][' time '] = ' Leave a message like 20150516 12:00 ';

$array 2[0][' content '] = ' message contents 1 ';

$array 2[1][' author '] = ' second message person ';

$array 2[1][' time '] = ' Leave a message like 20150517 13:00 ';

$array 2[1][' content '] = ' message contents 2 ';

foreach ($array 2 as $value) {

The $value here is actually an array.

echo "->[". $value [' author ']. "] To ". $value [' time ']." Said:
";

echo "". $value [' content ']. "
";

}



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.