PHP4 User Manual: Data Type -> Arrays

zhaozj2021-02-08  210

Arrays

One array in PHP is actually a sequence mapping. One mapping is the mapping value to the keyword. This type is optimized in a separate method, you can use it as a real array or a list (vector), HashTable (a mapping execution), dictionary, aggregation, stack, queue, and more to use it. Because you may have another PHP-array as a value, you can also imitate the tree structure.

This interpretation of this structure exceeds the scope of this manual, but you will find the smallest example of this structure. For more information on this structure, please check with other documents.

grammar

Use array () specified array

An array can be constructed by an array (). It consists of a pair of Key => Value consists of a series of comma-divided series numbers.

A Key is any non-negative integer or a string composition. If one is expressed by a standard non-negative integer, it will be interpreted so (I.E. '8' will be interpreted as 8, '08 'will be explained to' 08 ').

A value can be arbitrary.

Ignore keys. If you ignore a key, then the new button will use the largest integer index plus one. If an integer index does not exist, this button will be 0. If you already specify a value to a key, then this will be covered.

Array ([key =>] Value

, ...

)

// The key is any string or non-negative integer

/ / Value can be arbitrary

New / modification with the syntax of the strip bracket

You can modify an existing array through a clear setting value.

You can use the keys of brackets to the array. You can also ignore this button, add a pair of empty brackets after the variable name.

$ arr [key] = value;

$ Arr [] = Value;

// Key is any string or non-negative integer

// Value can be any if

$ ARR does not exist, it will be new. This is also possible to selectively specify an array. To change a certain value, just assign a new value to it. If you want to delete a pair of keys / values, you need to use

unset ().

Useful function

For the work of array, there is a useful function, see the array function paragraph.

The Foreach process control explicitly provides an easy way to loop a array.

Array do's and don'ts

Why is $ foo [bar] wrong?

In the old script you may see the grammar below:

$ foo [bar] = 'Enemy';

Echo $ FOO [BAR];

// ETC

This is wrong, but it will work. However, why is it wrong? After this

The Syntax fragment is specified that the expression must be between square brackets. . This means you can do just the next side:

Echo $ ARR [foo (true)];

This example uses a return value of a function as an index of an array. PHP also knows that it is constant, you can see

E_ *.

$ Error_Descriptions [E_ERROR] = "A Fatal Error Has Occured";

$ Error_Descriptions [e_warning] = "php issued a warning";

$ error_descriptions [e_notice] = "this is just an informal notice";

note,

E_ERROR is a valid identifier, just in the first example

Bar. But The Last Example IS in Fact The Same As Writing:

$ Error_Descriptions [1] = "A Fatal Error Has Occured;

$ Error_Descriptions [2] = "PHP Issued A Warning"; $ ERROR_DESCRIPTIONS [8] = "this is just an informal notice";

Because

E_ERROR Equals

1, ETC.

Then, how is it possible that $ foo [bar] works? It works, because bar is due to its syntax expected to be a constant expression. However, in this case no constant with the name bar exists. PHP now assumes that you meant Bar Literally, as the string "bar", but it. Forgot to write.

SO why is it bad the?

At some point in the future, the PHP team might want to add another constant or keyword, and then you get in trouble. For example, you already can not use the words empty and default this way, since they are special keywords.

And, IF these arguments don't help: this syntax is simply deprecated, and it might stop working some day.

Tip: When you turn error_reporting to E_ALL, you will see that PHP generates warnings whenever this construct is used This is also valid for other deprecated 'features' (put the line error_reporting (E_ALL); in your script)..

NOTE: INSIDE A DOUBLE-Quoted String, An Other Syntax Is Valid. See Variable Parsing in strings for more details.

Examples

The array type in pHP is very Versatile, So Here Will Be Some Examples To show you the full power of arrays.

// this

$ a = array ('color' => 'red'

, 'Taste' => 'Sweet'

, 'Shape' => 'Round'

, 'name' => 'apple'

, 4 // Key Will BE 0

);

// is Completely Equivalent with

$ a ['color'] = 'red';

$ A ['Taste'] = 'Sweet';

$ A ['Shape'] = 'Round';

$ A ['Name'] = 'Apple'; $ a [] = 4; // Key Will BE 0

$ b [] = 'a';

$ b [] = 'b';

$ b [] = 'c';

// Will Result in the array array (0 => 'a', 1 => 'b', 2 => 'c'),

// OR SIMPLY Array ('a', 'b', 'c')

EXAMPLE 6-4. Using array ()

// Array As (Property-) MAP

$ map = array ('version' => 4

, 'OS' => 'Linux'

, 'lang' => 'english'

, 'Short_Tags' => TRUE

);

// strictly numerical keys

$ array = array (7

, 8

0

156

, -10

);

// this is the same as array (0 => 7, 1 => 8, ...)

$ Sitching = Array (10 // Key = 0

, 5 => 6

, 3 => 7

, 'a' => 4

11 // Key = 6 (Maximum of Integer-Indices WAS 5)

, '8' => 2 // Key = 8 (Integer!)

, '02' => 77 // Key = '02'

, 0 => 12 // The value 10 Will Be overwritten by 12

);

- Mixed Keys

- Overwriting Keys

- Integer Keys As String

- USING VARS / FUNCTIONS AS Key / VALUES

- Mixed Skipping

->

// EMPTY ARRAY

$ EMPTY = array ();

EXAMPLE 6-5. Collection

$ colors = array ('red', 'blue', 'green', 'Yellow');

Foreach ($ Colors AS $ Color) {

Echo "Do You Like $ Color? / N";

}

/ * OUTPUT:

Do You Like Red?

Do You Like Blue?

Do You Like Green?

Do you like yeellow?

* /

Note That It is currently NOT POSIBLE TO CHANGE The VALUES OF THE ARRAY DIRECTLY in Such a loop. A Workaround is The Following:

EXAMPLE 6-6. COLLECTION

Foreach ($ colors as $ key => $ color) {// Won't work:

// $ color = strtoupper ($ color);

// Works:

$ colors [$ key] = strtoupper ($ color);

}

Print_r ($ colors);

/ * OUTPUT:

Array

(

[0] => red

[1] => Blue

[2] => Green

[3] => Yellow

)

* /

This Example Creates a one-based arch.

Example 6-7. One-based Index

$ firstquarter = array (1 => 'January', 'February', 'March');

Print_R ($ firstquarter);

/ * OUTPUT:

Array

(

[1] => 'January'

[2] => 'February'

[3] => 'March'

)

* /

EXAMPLE 6-8. Filling Real Array

// Fill An Array With All Items from A Directory

$ Handle = Opendir ('.');

While ($ file = readdir ($ hand))

{

$ Files [] = $ file;

}

CloseDir ($ handle);

Arrays are ordered. You can also change-functions. See array-functions for more information.

Example 6-9. Sorting array

Sort ($ files);

Print_r ($ files);

.

Example 6-10. Recursive and Multi-Dimensional Arrays

$ fruits = array ("fruits" => array ("a" => "orange"

, "b" => "banana"

, "c" => "apple"

)

, "NumBers" => Array (1

, 2

3

4

, 5

, 6

)

, "holes" => Array ("first"

, 5 => "Second"

, "third"

)

);

转载请注明原文地址:https://www.9cbs.com/read-3445.html

New Post(0)