Php foreach outputs two-dimensional array

Source: Internet
Author: User

Php foreach outputs two-dimensional array

/*

* Title: foreach outputs a two-dimensional array

* Function: foreach ()

* Created on: July 15, May 16, 2015

* Note:

*/

 

// Output a one-dimensional array

$ Array1 = array ();

$ Array1 [0] = 'chu Liuxiang ';

$ Array1 [1] = 'hu tie hua ';

$ Array1 [2] = 'ji bingyan ';

// Only the value is returned, that is, only the value is output.

Foreach ($ array1 as $ value ){

Echo $ value .'';

}

Echo'

';

// Output key-value pairs

Foreach ($ array1 as $ key => $ value ){

Echo $ key. ':'. $ value;

Echo'
';

}

Echo'

';

// Outputs a two-dimensional array.

$ Array2 = array ();

$ Array2 [0] ['autor'] = 'first message ';

$ Array2 [0] ['time'] = 'message time, for example, 20150516 ';

$ Array2 [0] ['content'] = 'message content 1 ';

$ Array2 [1] ['autor'] = 'second message ';

$ Array2 [1] ['time'] = 'message time, for example, 20150517 ';

$ Array2 [1] ['content'] = 'message content 2 ';

Foreach ($ array2 as $ value ){

// $ Value here is actually an array

Echo "-> [". $ value ['autor']. "] On". $ value ['time:
";

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.