ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: PHP :: PHP Êý×é

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

PHP
PHP Libxml
PHP Math
PHP Misc
PHP MySQL
PHP SimpleXML
PHP String
PHP XML
PHP Zip
PHP Mail
ÓÃPHP5µÄDirector..
PHP ×èÖ¹SQL×¢ÈëÊ..
PHP5ÃæÏò¶ÔÏó - »..
PHP5ÃæÏò¶ÔÏó - »..
PHP5ÃæÏò¶ÔÏó - »..
PHP5ÃæÏò¶ÔÏó - »..
PHP5ÃæÏò¶ÔÏó - »..
php5ÃæÏò¶ÔÏó - »..
php5ÃæÏò¶ÔÏó - »..
ÓÃPHP¿ØÖÆÓû§µÄä..
PHP PDO ѧϰ±Ê¼Ç

PHP Êý×é


×÷Õß:w3pop.com ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-04-28 ÐÞ¸Ä:2007-08-22 ä¯ÀÀ:8404 :: ::

An array can store one or more values in a single variable name.
Êý×飨array£©µÄ×÷ÓÃÊÇ£ºÎªÒ»¸öͬÃû±äÁ¿´æ´¢Ò»¸ö»ò¶à¸öÖµ¡£


What is an array?
ʲôÊÇÊý×飿

When working with PHP, sooner or later, you might want to create many similar variables.
µ±ÄãÊéдPHPʱ£¬Äã¶¼»á»ò¶à»òÉٵش´½¨¼¸¸ö¹ØÏµÏàÀàËÆµÄ±äÁ¿¡£

Instead of having many similar variables, you can store the data as elements in an array.
ΪÁ˱ÜÃâʹÓöà¸öÀàËÆ±äÁ¿£¨ÕâÑù¶ÔÄãµÄ¼ÇÒäºÍ³ÌÐò±àдÀ´Ëµ»áºÜÂé·³£©£¬Äã¿ÉÒÔ°ÑËùÓеÄÊý¾Ý·Ö±ð×÷Ϊһ¸öÔªËØ´æ´¢ÔÚÒ»¸öÊý×éÀï¡£

Each element in the array has its own ID so that it can be easily accessed.
Êý×éÖÐÿ¸öÔªËØ¶¼ÓÐ×Ô¼º¶ÀÁ¢µÄID£¬ÕâÑù±ãÓÚ·ÃÎÊ¡£

There are three different kind of arrays:
ÏÂÃæÁоÙÁ˲»Í¬ÖÖÀàµÄÊý×飺

  • Numeric array - An array with a numeric ID key
    Êý×ÖÊý×飨Numeric array£©£ºÕâ¸öÊý×é°üº¬Ò»¸öÊý×ÖID£»
  • Associative array - An array where each ID key is associated with a value
    ÁªºÏÊý×飨Associative array£©£ºÕâ¸öÊý×éÖеÄÿ¸öID¶¼ÊÇÓëÒ»¸öÖµÁªÏµÔÚÒ»¿é£¨¼´ËüÃÇÊÇÅäÌ׵ģ¬×éºÏÔÚÒ»¿éµÄ£©£»
  • Multidimensional array - An array containing one or more arrays
    ¶àάÊý×飨Multidimensional array£©£ºÒ»¸ö°üº¬ÓÐÒ»¸ö»ò¶à¸öÊý×éµÄÊý×飻

Numeric Arrays
Êý×ÖÊý×飨Numeric array£©

A numeric array stores each element with a numeric ID key.
Êý×ÖÊý×éÖеÄÿ¸öÔªËØ°üº¬Ò»¸öÊý×ÖID¡£

There are different ways to create a numeric array.
ÏÂÃæÁоټ¸ÖÖ²»Í¬µÄ·½·¨À´´´½¨Ò»¸öÊý×ÖÊý×飺

Example 1
°¸Àý1

In this example the ID key is automatically assigned:
Õâ¸öÀý×Ó£¬ÔªËصÄIDÊÇ×Ô¶¯Éú³ÉµÄ£º

$names = array("Peter","Quagmire","Joe");

Example 2
°¸Àý2

In this example we assign the ID key manually:
Õâ¸öÀý×ÓÖÐÔªËØµÄIDÊÇÎÒÃÇÊÖ¶¯Éú³ÉµÄ£º

$names[0] = "Peter";
$names[1] = "Quagmire";
$names[2] = "Joe";

The ID keys can be used in a script:
ÕâÀïµÄID¹Ø¼ü´Ê¿ÉÒÔÓ¦ÓÃÓÚÒ»¸ö½Å±¾³ÌÐòµ±ÖУº

<?php
$names[0] = "Peter";
$names[1] = "Quagmire";
$names[2] = "Joe";
echo $names[1] . " and " . $names[2] . 
" are ". $names[0] . "'s neighbors";
?>

The code above will output:
ÉÏÊö´úÂëÖ´Ðкó½«Êä³öÏÂÃæµÄ½á¹û£º

Quagmire and Joe are Peter's neighbors

 


Associative Arrays
ÁªºÏÊý×飨Associative array£©

An associative array, each ID key is associated with a value.
Õâ¸öÊý×éÖеÄÿ¸öID¶¼ÊÇÓëÒ»¸öÖµÁªÏµÔÚÒ»¿é£¨¼´ËüÃÇÊÇÅäÌ׵ģ¬×éºÏÔÚÒ»¿éµÄ£©¡£

When storing data about specific named values, a numerical array is not always the best way to do it.
Èç¹ûij¸öÖµÒѱ»ÃüÃû£¬ÄÇôÎÒ¾ÍÔÙʹÓÃÊý×ÖÊý×éÀ´´æ´¢ËüµÄÖµ¾Í²»·½±ãÁË¡£

With associative arrays we can use the values as keys and assign values to them.
ͨ¹ýʹÓÃÁªºÏÊý×飬ÎÒÃÇ¿ÉÒ԰ѹؼü´Ê×÷Ϊ¶ÔÏ󣬲¢¸øËüÃǸ³Öµ¡£

Example 1
°¸Àý1

In this example we use an array to assign ages to the different persons:
ÔÚÕâ¸öÀý×Óµ±ÖУ¬ÎÒÃÇͨ¹ýʹÓÃÒ»¸öÊý×鏸²»Í¬µÄÈËÖ¸¶¨Á˲»Í¬µÄÄêÁ䣺

$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);

Example 2
°¸Àý2

This example is the same as example 1, but shows a different way of creating the array:
Õâ¸öÀý×ӺͰ¸Àý1ÀàËÆ£¬ËüʹÓÃÁËÁíÍâÒ»ÖÖ´´½¨Êý×éµÄ·½·¨£º

$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";

The ID keys can be used in a script:
ÕâÀïµÄID¹Ø¼ü´Ê¿ÉÒÔÔڽű¾³ÌÐòÖÐʹÓãº

<?php
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
echo "Peter is " . $ages['Peter'] . " years old.";
?>

The code above will output:
ÉÏÊö´úÂëÖ´Ðкó½«Êä³öÏÂÃæµÄ½á¹û£º

Peter is 32 years old.

 


Multidimensional Arrays
¶àάÊý×飨Multidimensional array£©

In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on.
¶ÔÓÚÒ»¸ö¶àλÊý×éÀ´ËµÃ»£¬ÆäÖеÄÿ¸öÔªËØ¶¼¿ÉÒÔÊÓΪһ¸öÊý×飻ÕâЩ¶þ¼¶Êý×éÖеÄÿ¸öÔªËØÓÖ¿ÉÒÔÊÓΪһ¸öÊý×飬ÒÔ´ËÀàÍÆ¡£

Example
°¸Àý

In this example we create a multidimensional array, with automatically assigned ID keys:
ÔÚÕâ¸ö°¸Àýµ±ÖУ¬ÎÒÃǽ¨Á¢ÁËÒ»¸ö¶àλÊý×飬ËüµÄIDÊÇ×Ô¶¯Éú³ÉµÄ£º

$families = array
(
"Griffin"=>array
(
"Peter",
"Lois",
"Megan",
),
"Quagmire"=>array
(
"Glenn"

),
"Brown"=>array
(
"Cleveland",
"Loretta",
"Junior"
)
);

The array above would look like this if written to the output:
ÉÏÊöÊý×éµÄÖ´Ðнá¹ûÈçÏ£º

Array
(
[Griffin] => Array
(
[0] => Peter
[1] => Lois
[2] => Megan
)
[Quagmire] => Array
(
[0] => Glenn
)
[Brown] => Array
(
[0] => Cleveland
[1] => Loretta
[2] => Junior
)
)

ÆÀÂÛ (1) 1 All