Reprint PHP Array (ii)

Source: Internet
Author: User
Reprint PHP Array (2)

Original: http://taobeibei.iteye.com/blog/614857

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:

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

Example 2

In this example, we manually assign the ID key:

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

You can use these ID keys in your script:

 
  

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:

$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:

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

You can use the ID key in your script:

 
  

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:

$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  ))

Example 2

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

The output of the above code:

Is Megan a part of the Griffin family?

  • Related Article

    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.