PHP ʖ²ἯTH
º/A
Ղ 7. Ѝ
ʽש
ӯ·¨
¶¨ҥ array()
array("foo" = "bar", 12 = true);
ֵ¿ɒԊLjκΖµ¡£
array("somearray" = array(6 = 5, 13 = 9, "a" = 43));
// This array is the same as... array(5 = 43, 32, 56, "b" = 12); // ...this array array(5 = 43, 6 = 32, 7 = 56, "b" = 12);
Ӄ·½(ºŵēЂ½¨£¯О¸ļ/H3
$arr[key] = value; $arr[] = value; // key ²»ʇ string ¾͊ǷǸºµĠinteger // value ¿ɒԎªȎºΖµ¡£
$arr = array(5 = 1, 12 = 2); $arr[] = 56; // This is the same as $arr[13] = 56; // at this point of the script $arr["x"] = 42; // This adds a new element to // the array with key "x" unset($arr[5]); // This removes the element from the array unset($arr); // This deletes the whole array
ʵӃº¯ʽ
ʽשʲôºͲ»ʲô
Ϊʲô $foo[bar] ´큋£¿
$foo[bar] = 'enemy'; echo $foo[bar]; // etc
echo $arr[ foo(true)];
$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";
ćôΪʲôբѹ²»ºã¿
ע: µ±ģ°Ѡerror_reporting ɨΪ E_ALL ʱ£¬ģ½«»ῴµ½Ξ›ºΊ±ʹӃK䶨ҥµĊ index ʱ£¬PHP ¶¼»Ჺɺ֪ͨ¡£ ǫ¿´Ҕς½ű¾£º
?php // Turn on the display of all errors error_reporting(E_ALL); // Define the test array $abc = array("x" = "y"); // Access element with the *bad* method echo $abc[x];?
br / b Notice /b: Use of undefined constant x - assumed 'x' in b /path/to/script.php /b on line b 10 /b br /
ת»»Ϊʽש
=ד
PHP µĊÿ© ͓зdz£¶ൄӃ;£¬Ҳ´˕ ӐһЩ=דչʾʽשµčꕻ;f¡£
// 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')
=ד 7-4. ʹӃ 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();
=ד 7-5. ¼¯ºϼ/B
$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? */
=ד 7-6. ¼¯ºϼ/B
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) */
$firstquarter = array(1 = 'January', 'February', 'March'); print_r($firstquarter); /* output: Array ([1] = 'January' [2] = 'February' [3] = 'March') */
=ד 7-8. ̮³䊽ש
// fill an array with all items from a directory $handle = opendir('.'); while ($file = readdir($handle)) {$files[] = $file;} closedir($handle);
=ד 7-9. ʽשŅв
sort($files); print_r($files);
=ד 7-10. µݹ麍¶άʽש
$fruits = array ("fruits" = array ("a" = "orange", "b" = "banana", "c" = "apple"), "numbers" = array (1, 2, 3, 4, 5, 6,), "holes" = array ("first", 5 = "second", "third")); // Some examples to address values in the array above echo $fruits["holes"][5]; // prints "second" echo $fruits["fruits"]["a"]; // prints "orange" unset($fruits["holes"][0]); // remove "first" // Create a new multi-dimensional array $juices["apple"]["green"] = "good";
$arr1 = array(2, 3); $arr2 = $arr1; $arr2[] = 4; // $arr2 is changed, // $arr1 is still array(2,3) $arr3 = $arr1; $arr3[] = 4; // now $arr1 and $arr3 are the same
º/A
ưµ㼯A
ɏһ¼¶