Three forms of php array (array) output _ PHP Tutorial

Source: Internet
Author: User
Three forms of php array output are described in detail. Copy the code as follows: $ bbbbarray (11aaa, 22bbb); only the value can be output, and keyforeach ($ bbbbas $ color) {echo $ color ;} both value and key can output foreach ($ bb The code is as follows:


$ Bbbb = array ("11" => "aaa", "22" => "bbb ");
// Only output values, values, and Keys
Foreach ($ bbbb as $ color)
{
Echo $ color;
}
// Both value and key can be output.
Foreach ($ bbbb as $ key => $ value)
{
Echo $ key. "=>". $ value;
}

// Both value and key can be output.
While ($ color = each ($ bbbb )){
Echo $ color ['key'];
}
Or
While (list ($ key, $ value) = each ($ bbbb )){
Echo "$ key: $ value
";
}


Directly access the array elements:

The code is as follows:


$ Arr = array ('W' => 'wen', 'J' => 'jian ',' B '=> 'bao ');
Echo ($ arr ['w']),'
'; // Takes effect
Echo ($ arr [w]),'
'; // Takes effect
Echo ($ arr [0]),'
'; // Does not work. why ???
Echo ($ arr ['J']),'
'; // Takes effect
Echo ($ arr [j]),'
'; // Takes effect
Echo ($ arr [1]),'
'; // Does not work. why ???
Echo ($ arr ['B']),'
'; // Takes effect
Echo ($ arr [B]),'
'; // Takes effect
Echo ($ arr [2]),'
'; // Does not work. why ???
?>


Output:

The code is as follows:


Wen
Wen
Jian
Jian
Bao
Bao


Question:
Access associated array elements,
1. the "key" in [] can also be accessed without quotation marks ???
2. array index access does not work ???

The code is as follows:


$ Arr1 = array ('wen', 'jian ', 'bao ');
Echo $ arr1 [0],'
', $ Arr1 [1],'
', $ Arr1 [2];
?>


Output:

The code is as follows:


Wen
Jian
Bao


The http://www.bkjia.com/PHPjc/327319.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/327319.htmlTechArticle code is as follows: $ bbbb = array ("11" = "aaa", "22" = "bbb "); // only output value cannot output key foreach ($ bbbb as $ color) {echo $ color;} // both value and key can output foreach ($ bb...

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.