24-one-dimensional array definition, traversing

Source: Internet
Author: User
Tags array definition
<? PHP $ names = array (3.5, 5, "hello"); echo "the array size is :". count ($ names); For ($ I = 0; $ I <7; $ I ++) {echo "<br/> the elements in the array are :". $ Names [$ I]. "<br/>" ;}?>


Note: arrays in PHP are different from arrays in Java. arrays defined in PHP can be stored in multiple data types,

In the preceding example, you can add data such as integer data, string data, and input or decimal data.


The first method to define an array is shown above,

The following describes the second method for defining Arrays:

$ ANEM [0] = "zhaoliu ";

$ Name [1] = "zhangsan ";

$ Name [2] = "Lisi ";

$ Name [3] = "wangwu ";




As shown above, the default subscript Number of the array is used to define the array.

Next, let's take a look at the method for creating an array under the custom array:

$ Names ['zs'] = "zhangsan ";

$ Names ['LS'] = "Lisi ";

$ Names ['ww '= "wangwu ";

$ Names ['zl'] = "zhaoliu ";

Or:

$ Names = array ("Zs" => "zhangsan", "ls" => "Lisi ");

<? Phpecho "<HR/> use custom subscript to define an array <br/>"; $ names = array ("Zs" => "zhangsan ", "ls" => "Lisi"); echo "array size :". count ($ names); // Method for Traversing elements in the array foreach ($ names as $ key => $ Val) {echo "<br/> ". $ key. "= ". $ Val. "<br/>" ;}?>

To customize the traversal of the underlying array, as well as the instances output simultaneously by its key.




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.