Home > Backend Development > PHP Tutorial > PHP array usage tutorial_PHP tutorial

PHP array usage tutorial_PHP tutorial

WBOY
Release: 2016-07-13 10:26:52
Original
946 people have browsed it

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.

 The 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

Example

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

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

Output of the above code:

 Quagmire and Joe are Peter's neighbors

Associative array

Each ID key in the array is associated with a value

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

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

Output of the above script:

Peter is 32 years old.

Multidimensional array

An array containing one or more arrays

 $families = array

 (

 "Griffin"=>array

 (

 "Peter",

 "Lois",

 "Megan"

 ),

 "Quagmire"=>array

 (

 "Glenn"

 ),

 "Brown"=>array

 (

 "Cleveland",

 "Loretta",

 "Junior"

 )

 );

If you output this array, it should look like this:

Array

 (

 [Griffin] => Array

 (

 [0] => Peter

 [1] => Lois

 [2] => Megan

 )

 [Quagmire] => Array

 (

 [0] => Glenn

 )

 [Brown] => Array

 (

 [0] => Cleveland

 [1] => Loretta

 [2] => Junior

 )

 )

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/822520.htmlTechArticleIn 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. ...
source:php.cn
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