Basic operations for PHP arrays and classic operations for iterating over arrays

Source: Internet
Author: User

<?php

Indexed array
The first definition of an array
$arr = Array (+/-);
Var_dump ($arr);
The second definition of an array
$arr = [n/a];
Var_dump ($arr);
The third definition of an array
$arr [0] = 7;
$arr [1] = "Hello";
Var_dump ($arr);

echo $arr [2];//value

Associative arrays


$arr = Array ("name" = "Xiao Li", "sex" = "female", "age" = "20");
echo $arr ["Sex"];

Iterating through an array

For loop traversal for indexed arrays
$arr = [1,5,6,9,4,6,8];
for ($i =0; $i <count ($arr); $i + +) {
echo $arr [$i]. ' <br> ';
} ;

Foreach Loop traversal applies to indexes and associative arrays
$arr = Array ("name" = "Xiao Li", "sex" = "female", "age" = "20");
foreach ($arr as $k = = $v) {
echo $k. ': ' $v. ' <br> ';
echo "{$k}:{$v}<br>";
};


$arr = [1,5,6,9,4,6,8];
foreach ($arr as $k) {
echo $k. ' <br> ';
};


$arr = Array ("name" = "Xiao Li", "sex" = "female", "age" = "20");
Var_dump (each ($arr));


List ($a, $b) = Array (name, Xiao Li);
echo "{$a},{$b}";


list and each combine traversal
/* $arr = Array ("name" = "Xiao Li", "sex" = "female", "age" = "20");
while (list ($k, $v) =each ($arr)) {
echo "{$k}:{$v}<br>";
} */

pointer Traversal
/* $arr = Array ("name" = "Xiao Li", "sex" = "female", "age" = "20");
Echo current ($arr);//Take value
echo Key ($arr);//Take Key
Next ($arr);
Echo current ($arr);
echo Key ($arr);
Prev ($arr);
Echo current ($arr);
echo Key ($arr);
End ($arr);
Echo current ($arr);
echo Key ($arr);
Reset ($arr);
echo Key ($arr). ': '. Current ($arr). ' <br> '; */

Basic operations for PHP arrays and classic operations for iterating over arrays

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.