How to initialize an array in PHP

Source: Internet
Author: User
Array

There are generally 2 ways to initialize an array: One is to assign values to the elements in the array, and one is to initialize all the elements together.
The following is a brief description of 2 ways to initialize an array:
The first method:
$a ["Color"]= "red";
$a ["Taste"]= "sweet";
$a ["Shape"]= "round";
$a ["Name"]= "Apple";
$a [3]=4;
The second method:
$a =array (
"Color" => "red",
"Taste" => "sweet",
"Shape" => "Round",
"Name" => "Apple",
3=>4);

The effects of the two methods are the same, but the different methods of assigning values to the elements are different.

As an example, it is worth noting that the assignment of an element in an array begins with the second element.

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> Initialization Array </title>

<body>
<font size= "5" >
<?php
$monthName =array (1=> "January", "February", "March", "April", "may", "June", "July", "August", "September", "October" , "November", "December");
Print ("English" May "is <b> $monthName [5]</b>.<br>\n");
?>
</font>
</body>




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.