PHP array learning

Source: Internet
Author: User
For Web programming, the most important thing is to access and read/write data. There may be many storage methods, such as strings, arrays, and files. I learned arrays today, which 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 we can easily learn SyntaxHighlighter. all () in the future ();

For Web programming, the most important thing is to access and read/write data. There may be many storage methods, such as strings, arrays, and files. I learned arrays today, which 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:

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

$ Number = "1, 3, 5, 7, 9 ";

$ String = "Im 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:

Array ([number] => 1, 3, 5, 7, 9 [string] => Im 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)

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

$ 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", "a"); print_r ($ array3 );

?>

The default step value of the range () function is 1!

Running result:


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:

$ Array = range (1, 10 );

$ Fillarray = range ("a", "d ");

$ ArrayFilled = array_fill (0, 5, $ 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:


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

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.