PHP Array Usage Tutorial

Source: Internet
Author: User
Tags arrays numeric

In the process of developing using PHP, sooner or later, you will need to create many similar variables.

Without a lot of similar variables, you can store the data as an element in an array.

The elements in an array have their own IDs, so you can easily access them.

There are three types of arrays:

Numeric array

An array with a numeric ID key

Example

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

echo $names [1]. "and". $names [2]. "Are". $names [0]. "' s Neighbors";

The output of the above code:

Quagmire and Joe are Peter ' s neighbors

Associative arrays

Each ID key in the array associates a value

$ages = Array ("Peter" =>32, "quagmire" =>30, "Joe" =>34);

echo "Peter is". $ages [' Peter ']. "Years old."

Output from the above script:

Peter is years old.

Multidimensional arrays

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 something like this:

Array

(

[Griffin] => Array

(

[0] => Peter

[1] => Lois

[2] => Megan

)

[Quagmire] => Array

(

[0] => Glenn

)

[Brown] => Array

(

[0] => Cleveland

[1] => Loretta

[2] => Junior

)

)

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.