Php array application and usage

Source: Internet
Author: User
Tags array definition arrays compact numeric mixed php tutorial

What is an array?
When using PHP for development, you need to create many similar variables either early or late.

Without many similar variables, you can store data as elements in an array.

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

There are three types of arrays:
Numeric array
Array with digit ID key
Join array
Each ID key in the array is associated with a value.
Multi-dimensional array
Array Value array that contains one or more arrays
Each element stored in the value array has a digital 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 allocate the ID key:

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

You can use these ID keys in the script:

<? Php Tutorial

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

Echo $ names [1]. "and". $ names [2]. "are". $ names [0]. "'s neighbors ";
?>
Output of the above code:

Quagmire and Joe are Peter's neighbors

Join array
Join array. Each ID key is associated with a value.

It is not the best practice to use a numeric array to store data related to specific named values.

By associating arrays, we can use values as keys and assign values to them.

Example 1
In this example, we use an array to allocate ages to different people:

$ Ages = array ("Peter" => 32, "Quagmire" => 30, "Joe" => 34); Example 2
This example is the same as example 1, but shows another method to create an array:

$ Ages ['Peter '] = "32 ";
$ Ages ['quagmire'] = "30 ";
$ Ages ['job'] = "34 ";
You can use the ID key in the script:

<? Php

$ Ages ['Peter '] = "32 ";
$ Ages ['quagmire'] = "30 ";
$ Ages ['job'] = "34 ";

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

Output of the above script:

Peter is 32 years old. Multi-dimensional array
In a multi-dimensional array, each element in the main array is also an array. Each element in the sub-array can also be an array, and so on.

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

$ Families = array
(
"Griffin in" => array
  (
"Peter ",
"Lois ",
"Megan"
),
"Quagmire" => array
  (
"Glenn"
),
"Brown" => array
  (
"Cleveland ",
"Loretta ",
"Junior"
  )
);

If this array is output, it should be similar to the following:

Array
(
[Griffin in] => 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:

Echo "Is". $ families ['grigging'] [2].
"A part of the Griffin family? ";
Output of the above code:

Is Megan a part of the Griffin family?

I. Array definition:
The array definition uses the array () method to define an empty array:
The code is as follows:

<? Php
$ Number = array (1, 3, 5, 7, 9 );
// Define an empty array
$ Result = array ();
$ Color = array ("red", "blue", "green ");
// Custom key value
$ Language = (1 => "English", 3 => "Chinese", 5 => "Franch ");
// Define a two-dimensional array
$ Two = array (
"Color" => array ("red", "blue"), // end with a comma
"Week" => array ("Monday", "Friday") // The last sentence contains no punctuation.
);
?>

2. Create an array:
Create the functions included in the array: compact (),
1. compact () function -- convert one or more variables (including arrays) to an array:
Array compact (mixed $ varname [, mixed $...])
The code is as follows:

<? PHP
$ Number = "1, 3, 5, 7, 9 ";
$ String = "I'm PHPer ";
$ Array = array ("And", "You? ");
$ NewArray = compact ("number", "string", "array ");
Print_r ($ newArray );
?>

The compact () function is used to convert two or more variables to an array. Of course, it also contains array variables. The parameter is the variable name, not the $ full name.
The opposite function is extract (). As the name suggests, it is to convert an array into a single string. The key value is used as the string name, and the array value is used as the string value.
Running result:
The code is as follows:

Array ([number] => 1, 3, 5, 7, 9 [string] => I'm PHPer [array] => Array ([0] => And [1] => You? ))

2. array_combine () -- combines two arrays into an array, one as the key value and the other as the value:
Array array_combine (array $ keys, array $ values)
The code is as follows:

<? PHP
$ Number = array ("1", "3", "5", "7", "9 ");
$ Array = array ("I", "Am", "A", "PHP", "er ");
$ NewArray = array_combine ($ number, $ array );
Print_r ($ newArray );
?>

No more about the array_combine function.
Running result:

Array ([1] => I [3] => Am [5] => A [7] => PHP [9] => er)
3. range () function -- create an array of the specified range:
Let's just move on to the instance directly --
The code is as follows:

<? PHP
$ Array1 = range (0,100, 10); // 0 indicates the start value, 100 indicates the end value, and 10 indicates the step value (the default step value is 1 ).
Print_r ($ array1 );
Echo "<br/> ";
$ Array2 = range ("A", "Z ");
Print_r ($ array2 );
Echo "<br/> ";
$ Array3 = range ("z", "");
Print_r ($ array3 );
?>

The default step value of the range () function is 1!
Running result:
The code is as follows:

Array ([0] => 0 [1] => 10 [2] => 20 [3] => 30 [4] => 40 [5] => 50 [6] = & gt; 60 [7] = & gt; 70 [8] = & gt; 80 [9] = & gt; 90 [10] = & gt; 100)
Array ([0] => A [1] => B [2] => C [3] => D [4] => E [5] => F [6] => G [7] => H [8] => I [9] => J [10] => K [11] => L [12] => M [13] => N [14] => O [15] => P [16] => Q [17] => R [18] => S [19] => T [20] => U [21] => V [22] => W [23] => X [24] => Y [25] => Z)
Array ([0] => z [1] => y [2] => x [3] => w [4] => v [5] => u [6] => t [7] => s [8] => r [9] => q [10] => p [11] => o [12] => n [13] => m [14] => l [15] => k [16] => j [17] => I [18] => h [19] => g [20] => f [21] => e [22] => d [23] => c [24] => B [25] =>)

4. array_fill () function -- fill the array function:
The code is as follows:

<? PHP
$ Array = range (1, 10 );
$ Fillarray = range ("a", "d ");
$ ArrayFilled = array_fill (, $ fillarray); // $ fillarray can be a string, such as "test ".
Echo "<pre> ";
Print_r ($ arrayFilled );
Echo "</pre> ";
$ Keys = array ("string", "2", 9, "SDK", "PK ");
$ Array2 = array_fill_keys ($ keys, "testing ");
Echo "<pre> ";
Print_r ($ array2 );
Echo "</pre> ";
?>

Running result:
The code is as follows:

Array
(
[0] => Array
(
[0] =>
[1] => B
[2] => c
[3] => d
)
[1] => Array
(
[0] =>
[1] => B
[2] => c
[3] => d
)
[2] => Array
(
[0] =>
[1] => B
[2] => c
[3] => d
)
[3] => Array
(
[0] =>
[1] => B
[2] => c
[3] => d
)
[4] => Array
(
[0] =>
[1] => B
[2] => c
[3] => d
)
)
Array
(
[String] => testing
[2] => testing
[9] => testing
[SDK] => testing
[PK] => testing
)

II. Array traversal:

1. foreach traversal:
Foreach (array_expression as $ value ){}
Foreach (array_expression as $ key => $ value ){}
For example:
The code is as follows:
<? PHP
$ Speed = array (50,120,180,240,380 );
Foreach ($ speed as $ keys => $ values ){
Echo $ keys. "=>". $ values. "<br/> ";
}
?>

Running result:
The code is as follows:
0 => 50
1 => 120
2 => 180
3 => 240
4 => 380

2. while loop traversal:
While loop traversal is generally combined with the list function, the following is an instance
The code is as follows:
<? PHP
$ Staff = array (
Array ("name", "gender", "age "),
Array ("Xiao Zhang", "male", 24 ),
Array ("Wang", "female", 25 ),
Array ("Xiao Li", "male", 23)
);
Echo "<table border = 2> ";
While (list ($ keys, $ value) = each ($ staff )){
List ($ name, $ sex, $ age) = $ value;
Echo "<tr> <td> $ name </td> <td> $ sex </td> <td> $ age </td> </tr> ";
}
Echo "</table> ";
?>

Running result:
Name gender age
John 24
Wang female 25
Mr. Lee 23
3. for loop traversal:
The code is as follows:
<? PHP
$ Speed = range (0,220, 20 );
For ($ I = 0; $ I <count ($ speed); $ I ++ ){
Echo $ speed [$ I]. "";
}
?>

Running result:
The code is as follows:
0 20 40 60 80 100 120 140 160 180 200

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.