Home  >  Article  >  Backend Development  >  PHP Array Tutorial Defining Arrays_PHP Tutorial

PHP Array Tutorial Defining Arrays_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:43:17752browse

1. Define an array
You can use the array() language structure to create a new array. It accepts a number of comma-separated key => value parameter pairs.
array( [key =>] value , ... ) // key can be a number or a string // value can be any value
Example 1:

Copy code The code is as follows:

$phpjc = array(
0=>'word',
3=> ;'excel',
'outlook',
'access');
print_r($phpjc);
?>

The output results are as follows:
Array ( [0] => word [3] => excel [4] => outlook [5] => access )
Example 1 defines an array named phpjc, the first element The value is: word, (note: the array starts counting from 0), the second element is empty, the third element is: excel, and the fourth and fifth elements
are automatically generated later and can be passed to the variable Giving array() no arguments creates an empty array, to which values ​​can then be added using the square brackets [] syntax.
Example 2:
Copy code The code is as follows:

$phpjc = array();
$phpjc [] = "one";
$phpjc[] = "two";
echo $phpjc[0]."
";
echo $phpjc[1];

The output results are as follows:
one
two
2. Read array elements
Use string index (or key) to access the value stored in the array
Example 3 :
Copy code The code is as follows:

$phpjc = array("first"=>1,"second"= >2,"third"=>3);
echo $phpjc["second"];
$phpjc["third"]=5; //Change the value of the third element from "3 "Changed to "5"
echo $phpjc["third"];

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320749.htmlTechArticle1. Define an array You can use the array() language structure to create a new array. It accepts a number of comma-separated key = value parameter pairs. array( [key =] value , ... ) // key can be...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn