PHP 专栏收录该内容
22 篇文章 0 订阅

# Arrays

## 语法

### 用array()指定数组

 array( [key =>] value , ... ) // 键是任意的字符串或非负整数 // 值可以是任意的

### 用带方括号的语法新建/修改

 $arr[key] = value;$arr[] = value; // key 是任意字符串或非负整数 // value 可以是任意的

 $foo[bar] = 'enemy'; echo$foo[bar]; // etc

 echo $arr[ foo(true) ]; 这个例子使用一个函数的返回值作为数组的索引。PHP也知道是常量，你可以见 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";

 $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 that you forgot to write the quotes.

#### So why is it bad then?

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 cannot 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, ...)$switching = 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 ); // 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 yellow? */ Note that it is currently not possible 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 array.

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($handle)) {$files[] = $file; } closedir($handle);

Arrays are ordered. You can also change the order using various sorting-functions. See array-functions for more information.

Example 6-9. Sorting array

 sort($files); print_r($files);

Because the value of an array can be everything, it can also be another array. This way you can make recursive and multi-dimensional arrays.

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" ) );
• 0
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

10-31 22
09-23 654

07-19 67
12-02 1278
08-31 41
10-31 427
02-08 4746
11-03 28
03-22 970
01-05 729
08-29 1419
01-19 161

hqywork

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。