PHP array - PHP basic introductory tutorial_PHP tutorial

WBOY
Release: 2016-07-13 17:16:17
Original
819 people have browsed it

This article will give you a detailed introduction to the PHP basic introductory tutorial on PHP arrays. Friends who need to learn how to use PHP arrays can refer to it.

What is an array?
In the process of developing with PHP, sooner or later, you will need to create many similar variables.

Instead of having many similar variables, you can store data as elements in an array.

Elements in the array have their own IDs, so they can be accessed easily.

There are three array types:
Numeric array
Array with numeric ID keys
Associative array
Each ID key in the array is associated with a value
Multidimensional array
Array containing one or more arrays
Numeric array
Numeric arrays store each element with a numeric ID key.

Different methods can be used to create numeric arrays:

Example 1
In this example, the ID key is automatically assigned:

The code is as follows Copy code
 代码如下 复制代码

$names = array("Peter","Quagmire","Joe");

$names = array("Peter","Quagmire","Joe");


Example 2 In this example, we manually assigned the ID key:
 代码如下 复制代码

$names[0] = "Peter";
$names[1] = "Quagmire";
$names[2] = "Joe";可以在脚本中使用这些 ID 键:

$names[0] = "Peter";
$names[1] = "Quagmire";
$names[2] = "Joe";

echo $names[1] . " and " . $names[2] . " are ". $names[0] . "'s neighbors";
?>

以上代码的输出:

Quagmire and Joe are Peter's neighbors

The code is as follows Copy code

$names[0] = "Peter";
$names[1] = "Quagmire";

$names[2] = "Joe";You can use these ID keys in scripts:

$names[0] = "Peter";

$names[1] = "Quagmire";
$names[2] = "Joe";

代码如下 复制代码

$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);

echo $names[1] . " and " . $names[2] . " are ". $names[0] . "'s neighbors";

?>

Output of the above code:
 代码如下 复制代码
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";

Quagmire and Joe are Peter's neighbors

 代码如下 复制代码

$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";

echo "Peter is " . $ages['Peter'] . " years old.";
?>

以上脚本的输出:

Peter is 32 years old.

Associative array An associative array in which each ID key is associated with a value. Using numeric arrays is not the best practice when storing data about specifically named values. With associative arrays, we can use values ​​as keys and assign values ​​to them. Example 1 In this example, we use an array to assign ages to different people:
The code is as follows Copy code
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
Example 2 This example is the same as Example 1, but shows another way to create an array:
The code is as follows Copy code
$ages['Peter'] = "32 "; $ages['Quagmire'] = "30"; $ages['Joe'] = "34";
ID keys can be used in scripts:
The code is as follows Copy code
<🎜>$ages['Peter'] = "32";<🎜> $ages['Quagmire'] = "30";<🎜> $ages['Joe'] = "34";<🎜> <🎜>echo "Peter is " . $ages['Peter'] . " years old.";<🎜> ?> Output of the above script: Peter is 32 years old.

Multidimensional array
In multidimensional arrays, each element in the main array is also an array. Each element in a subarray can also be an array, and so on.

Example 1
In this example, we create a multidimensional array with automatically assigned ID keys:

"Griffin"=>array (
The code is as follows
 代码如下 复制代码

$families = array
(
  "Griffin"=>array
  (
  "Peter",
  "Lois",
  "Megan"
  ),
  "Quagmire"=>array
  (
  "Glenn"
  ),
  "Brown"=>array
  (
  "Cleveland",
  "Loretta",
  "Junior"
  )
);如果输出这个数组的话,应该类似这样:

Array
(
[Griffin] => Array
  (
  [0] => Peter
  [1] => Lois
  [2] => Megan
  )
[Quagmire] => Array
  (
  [0] => Glenn
  )
[Brown] => Array
  (
  [0] => Cleveland
  [1] => Loretta
  [2] => Junior
  )
)

Copy code


$families = array
 代码如下 复制代码

echo "Is " . $families['Griffin'][2] .
" a part of the Griffin family?";

以上代码的输出:

Is Megan a part of the Griffin family?

(
"Peter", "Lois",

"Megan"

), ( "Glenn" ), "Brown"=>array ( "Cleveland", "Loretta", "Junior" ) ); If this array is output, it should be similar to this:
Array ( [Griffin] => Array
(
[0] => Peter [1] => Lois [2] => Megan ) [Quagmire] => Array ( [0] => Glenn ) [Brown] => Array ( [0] => Cleveland [1] => Loretta [2] => Junior ) ) Example 2 Let’s try to display a single value from the array above:
The code is as follows Copy code
echo "Is " . $families['Griffin'][2] . " a part of the Griffin family?"; Output of the above code: Is Megan a part of the Griffin family? http://www.bkjia.com/PHPjc/628656.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628656.htmlTechArticleThis article will give you a detailed introduction to the PHP basic introductory tutorial on PHP arrays. If you need to learn how to use PHP arrays Friends can enter for reference. What is an array? Using PHP for...
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template