PHP Learning four: arrays (one)

Source: Internet
Author: User
Tags array length

1. Direct Assignment method:

① indexed arrays: Storing data by index, memory is not contiguous, but JS memory is contiguous

The code is as follows:

$array [0] = "one"; $array [1] = "A";p rint_r ($array); Prints the array echo "<br>" via Print_r ();

  

② Associative arrays: Storing data in the form of keys, and memory is not contiguous

The code is as follows:

$array 2["name"] = "Fusaaki"; $array 2["age"] = 18;print_r ($array 2); echo "<br>";

  

2. defining arrays by array

You can define an array by using the keyword array, and if it is an empty array, you need to add data to the array using the array[] method

The code is as follows:

$array 3 = Array (1,2,3,4, "Demacia");p Rint_r ($array 3), echo "<br>", $array 4 = array (); $array 4[] = 1; Add data to the array by array[] = 2; $array 4[] = 3; $array 4[] = 4;print_r ($array 4); echo "<br>";

  

3. iterating through an array

① Traversal index array: For loop traversal, but the array in PHP does not have the length property, the count number is required

The code is as follows:

for ($i =0; $i <count ($array 4), $i + +) {  //count represents the number of values in the array, there is no array length in PHP,    echo $array 4[$i];}

  

② traversal associative arrays: Use foreach to traverse, and also to traverse associative and indexed functions in a mixed body

The code is as follows:

foreach ($array 2 as $key = + $obj) {    echo $key. = ". $obj." <br> ";}

  

The foreach format at this time is (array name as key = = value);

PHP Learning four: arrays (one)

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.