Create and traverse arrays in php

Source: Internet
Author: User
: This article describes how to create and traverse arrays in php. For more information about PHP tutorials, see. The establishment of arrays in php can be divided into three types of methods. when traversing an array, we often use the count ($ arr) function, which returns the length of the array:

1.

// Method 1 for array creation $ arr [0] = 11; $ arr [1] = "ee"; $ arr [2] = 11.1; $ arr [3] = true; $ arr [4] = null; for ($ I = 0; $ I <count ($ arr); $ I ++) {echo'
'. $ Arr [$ I];}

2.

// Method 2 for creating an array $ arr = array (1, "ee", true); for ($ I = 0; $ I <count ($ arr ); $ I ++) {echo'
'. $ Arr [$ I];}

3.

// Method 3 for creating an array (store as a key-value pair) $ arr ['name'] = "raid "; $ arr ['pwd'] = "eee"; foreach ($ arr as $ key => $ val) {echo $ key. "= ". $ val."
";}

4. sometimes you can write as follows:

$ Arr = array (5 => "logo", "ee" => 12, "ff" => 13); // display the overall array of print_r ($ arr ); echo"
"; Var_dump ($ arr); echo"
". Is_array ($ arr );

To split the string into arrays, we can use the following explode method:
$str = "e a d d";$arr = explode(" ", $str);foreach($arr as $key=>$val) {echo "
".$key."=".$val;}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above introduces the establishment and traversal of arrays in php, including the content, and hope to be helpful to friends who are interested in PHP tutorials.

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.