PHP Basic PHP Basics Tutorial _php Tutorial

Source: Internet
Author: User
Tags learn php php basics
This article to give you a detailed introduction to the PHP array of PHP Basic Primer tutorial, you have to learn PHP array use of friends can enter the reference.

What is an array?
You will need to create many similar variables in the process of using PHP for development, either early or late.

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

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

There are three types of arrays:
Array of values
An array with a numeric ID key
Associative arrays
Each ID key in the array associates a value
Multidimensional arrays
An array that contains one or more arrays
Array of values
Each element stored in a numeric array has a numeric ID key.

You can use different methods to create a numeric array:

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

The code is as follows Copy Code

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

Example 2
In this example, we manually assign the ID key:

The code is as follows Copy Code

$names [0] = "Peter";
$names [1] = "quagmire";
$names [2] = "Joe"; you can use these ID keys in your script:

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

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

The output of the above code:

Quagmire and Joe are Peter ' s neighbors

Associative arrays
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 a specific named value.

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 age 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 it shows another way to create an array:

The code is as follows Copy Code
$ages [' Peter '] = "32";
$ages [' quagmire '] = "30";
$ages [' Joe '] = "34";

You can use the ID key in your script:

The code is as follows Copy Code

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

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

Output from the above script:

Peter is the years old.

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

Example 1
In this example, we created a multidimensional array with an auto-assigned ID key:

copy code

$families = array
(
"Griffin" + Array
(
"Peter",
"Lois",
"Megan"
),
"Quagmire" =>array
(
"Glenn"
),
"Brown" =& Gt;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] = Junio R
)
)

Example 2
Let's try to display a single value in the above array:

The code is as follows Copy Code

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

The output of the above code:

Is Megan a part of the Griffin family?

http://www.bkjia.com/PHPjc/628656.html www.bkjia.com true http://www.bkjia.com/PHPjc/628656.html techarticle This article to give you a detailed introduction to the PHP array of PHP Basic Primer tutorial, you have to learn PHP array use of friends can enter the reference. What is an array? In the use of PHP ...

  • 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.