PHP array learning

Source: Internet
Author: User
Tags array definition
For Web programming, the most important thing is to access and read/write data. There may be many storage methods, which can be strings, arrays, files, etc. today I learned arrays. it can be said that it is an important method in PHP Data applications. PHP has a large number of array functions. The following is a summary of my learning, so that you can easily identify them later ......
I. array definition:
The array definition uses the array () method to define an empty array:
The code is as follows:
$ 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:
$ 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:
$ 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:
$ 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"
";
$ Array2 = range ("A", "Z ");
Print_r ($ array2 );
Echo"
";
$ 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:
$ Array = range (1, 10 );
$ Fillarray = range ("a", "d ");
$ ArrayFilled = array_fill (, $ fillarray); // $ fillarray can be a string, such as "test ".
Echo"
"; 
print_r ($arrayFilled);
echo "
";
$ Keys = array ("string", "2", 9, "SDK", "PK ");
$ Array2 = array_fill_keys ($ keys, "testing ");
Echo"
"; 
print_r ($array2);
echo "
";
?>

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:
$ Speed = array (50,120,180,240,380 );
Foreach ($ speed as $ keys => $ values ){
Echo $ keys. "=>". $ values ."
";
}
?>

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:
$ Staff = array (
Array ("name", "gender", "age "),
Array ("Xiao Zhang", "male", 24 ),
Array ("Wang", "female", 25 ),
Array ("Xiao Li", "male", 23)
);
Echo"




";While (list ($ keys, $ value) = each ($ staff )){List ($ name, $ sex, $ age) = $ value;Echo" ";}Echo"
$ Name $ Sex $ Age
";
?>

Running result:
Name Gender Age
Xiao Zhang Male 24
John Female 25
Xiao Li Male 23
3. for loop traversal:
The code is as follows:
$ Speed = range (0,220, 20 );
For ($ I = 0; $ 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.