Definition and traversal of arrays

Source: Internet
Author: User

Expand
Common functions
Generate random numbers
echo rand (1,10);//range between 1-10

Date-time functions
Var_dump (Time ());//Unix timestamp of the current time, is a string of numbers

Date ("Y-m-d h:i:s", Time ());//Format date timestamp
Date ("y-m-d h:i:s D");//Omit time (), get a default value for current times, D English display week


Array
Arrays in C # are contiguous
1. No set concept,
Features: can store any type of data, can be discontinuous, can be indexed, or can be associated;

Defining arrays
Define array Mode one
$attr =array (All-in-one);//define a simple indexed array

Define array mode two
$attr []=1;
$attr []=2;

Var_dump ($ATTR);

Define array mode three

$attr = Array ("One" = "Hello",
"=>100",
"Three" =>10.9);
Var_dump ($ATTR);

Defining an associative array


$attr = Array (
"Hello",
"2" = "222",
"World"
);
Var_dump ($ATTR);
View index value, empty auto-Add, decimal as String if key=0, the first value is overwritten,
Index is not duplicated

Array values
$attr =array (
"Hello",
"World",
100,
10.3
);
Echo $attr [0];//based on index value
Echo $attr ["Three"];//value values based on key

Iterating through an array
1.for loops for indexed arrays, unable to use connection arrays

for ($i =0; $i <count ($attr); $i + +)
{
echo $attr [$i]. " <br> ";
}
Stitching up


2.foreach traversal, suitable for all arrays
foreach ($attr as $v)
{
echo $v. " <br> ";

}
foreach ($attr as $k = + $v)//second form
{
echo $k. " --". $v." <br> ";
or echo "{$k}--{$v}<br>"
}


3. Use each () and list () to combine traversal
Var_dump (each ($attr));//returns details on the current element inside the array
Var_dump (each ($attr));
Var_dump (each ($attr));

A special kind of
$attr =array;//assigns each element in the right array to the list () argument
The right array must contain an index
List ($a, $b, $c) = $attr;
echo $a;
Echo $b;
Echo $c;


while (list ($k, $v) =each ($attr));//assignment statement, execution failure returns a Falue

{
echo "{$k}--{$v}<br>";
}


4. Pointer traversal
$attr =array (

"One" = "AAAA",
"Both" and "bbbb",
"Three" = "CCCC"
);
Echo current ($attr);//Take the value of the present element

echo Key ($attr);//Take the key of the current element

Next ($attr);//Move the pointer inside the array down
Prev ($attr);//move the pointer up
End ($attr);//Pointing the pointer to the last
Reset ($attr);//Pointing the pointer to the first one, resetting
Echo current ($attr);

Use it together to iterate over all the elements.
Do
{
Echo current ($attr). " <br> "
}
while (Next ($attr));
Using the Do...while loop

Definition and traversal of 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.