PHP Create definition array

Source: Internet
Author: User

$array = Array ();
$array ["key"] = "values";
?>
There are two main ways to declare an array in PHP:
1. Declare an array with the array () function,
2. Assign a value directly to the array element.

Array arrays
$users = Array (' phone ', ' computer ', ' dos ', ' Linux ');
echo $users;//Only print out data type array
Print_r ($users);//array ([0] = + phone [1] = computer [2] = = DOS [3] = Linux)


$numbers = range (1,5);//Create an array containing the specified range
Print_r ($numbers);//array ([0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = 5)
Print_r (true);//1
Var_dump (false);//bool (false)

Print_r can simply print the string and the number, the array will begin with an array and is represented by the key value, Print_r output Boolean and null results are meaningless, so it is more appropriate to use Var_dump

To display all the values in the array through a loop
for ($i = 0; $i < 5; $i + +) {
echo $users [$i];
Echo '
‘;
}

Count the number of cells in an array or the number of attributes in an object by count/sizeof


for ($i = 0; $i < count ($users); $i + +) {
echo $users [$i];
Echo '
‘;
}
You can also iterate through the array through the Foreach loop, which is the benefit of not having to consider key
foreach ($users as $value) {
echo $value. '
';//dot number is a string join symbol
}
The Foreach loop iterates through the $key and $value; $key and $value are variable names that can be set by themselves
foreach ($users as $key = = $value) {
echo $key. '
';//Output key
}
?>
go see what you know? " color code Daquan

PHP Create definition array

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.