cvs: phpdoc-zh /language/types array.xml
| From: | Gang Chen | Date: | Thu, 25 Dec 2008 01:21:43 +0000 |
| Subject: | cvs: phpdoc-zh /language/types array.xml | ||
| Groups: | php.doc.zh | ||
| Request: | Send a blank email to doc-zh+get-3136@lists.php.net to get a copy of this message | ||
eniac2008 Thu Dec 25 01:21:43 2008 UTC
Added files:
/phpdoc-zh/language/types array.xml
Log:
Sync with EN-Revision 1.7
http://cvs.php.net/viewvc.cgi/phpdoc-zh/language/types/array.xml?view=markup&rev=1.1 Index: phpdoc-zh/language/types/array.xml +++ phpdoc-zh/language/types/array.xml <?xml version="1.0" encoding="utf-8"?> <!-- $Revision: 1.1 $ --> <!-- $Author: eniac2008 --> <!-- EN-Revision: 1.7 Maintainer: Altair Status: ready --> <!-- CREDITS: Geogory dallas --> <sect1 xml:id="language.types.array"> <title>æ°ç»</title> <para> PHP ä¸ç <type>æ°ç»</type> å®é 䏿¯ä¸ä¸ªæåºæ å°ãæ å°æ¯ä¸ç§æ <emphasis>values</emphasis> å ³èå° <emphasis>keys</emphasis> çç±»åãæ¤ç±»åå¨å¾å¤æ¹é¢åäºä¼åï¼å æ¤å¯ä»¥æå®å½æçæ£çæ°ç»ï¼æå表ï¼åéï¼ï¼æ£åè¡¨ï¼æ¯æ å°çä¸ç§å®ç°ï¼ï¼åå ¸ï¼éåï¼æ ï¼éå以忴å¤å¯è½æ§ãæ°ç»å ç´ çå¼ä¹å¯ä»¥æ¯å¦ä¸ä¸ªæ°ç»ãæ å½¢ç»æåå¤ç»´æ°ç»ä¹æ¯å 许çã </para> <para> è§£éè¿äºç»æè¶ åºäºæ¬æåçèå´ï¼ä½å¯¹äºæ¯ç§ç»æè³å°ä¼æä¾ä¸ä¸ªä¾åãè¦å¾å°è¿äºç»æçæ´å¤ä¿¡æ¯ï¼å»ºè®®åèæå ³æ¤å¹¿é主é¢çå ¶å®èä½ã </para> <sect2 xml:id="language.types.array.syntax"> <title>è¯æ³</title> <sect3 xml:id="language.types.array.syntax.array-func"> <title>å®ä¹æ°ç» <function>array</function></title> <para> å¯ä»¥ç¨ <function>array</function> è¯è¨ç»ææ¥æ°å»ºä¸ä¸ª <type>array</type>ã宿¥åä»»ææ°éç¨éå·åéç <literal><replaceable>é®(key)</replaceable> => <replaceable>å¼(value)</replaceable></literal> 对ã </para> <synopsis> array( <optional> <replaceable>key</replaceable> => </optional> <replaceable>value</replaceable> , ... ) // <replaceable>é®(key)</replaceable> 坿¯æ¯ä¸ä¸ª <type>æ´æ°(integer)</type> æ <type>å符串(string)</type> // <replaceable>å¼(value)</replaceable> å¯ä»¥æ¯ä»»æç±»åçå¼</synopsis> <!-- Do not fix the whitespace for the synopsis end element. A limitation of PhD prevents proper trimming --> <informalexample> <programlisting role="php"> <![CDATA[ <?php $arr = array("foo" => "bar", 12 => true); echo $arr["foo"]; // bar echo $arr[12]; // 1 ?> ]]> </programlisting> </informalexample> <para> <varname>key</varname> å¯ä»¥æ¯ <type>integer</type> æè <type>string</type>ã妿keyæ¯ä¸ä¸ª <type>integer</type> çæ å表示ï¼å被解éä¸ºæ´æ°ï¼ä¾å¦ <literal>"8"</literal> å°è¢«è§£é为 <literal>8</literal>ï¼è <literal>"08"</literal> å°è¢«è§£é为 <literal>"08"</literal>ï¼ã<varname>key</varname> ä¸çæµ®ç¹æ°è¢«åæ´ä¸º <type>integer</type>ãå¨ PHP ä¸ç´¢å¼æ°ç»ä¸å ³è <type>æ°ç»</type> æ¯ç¸åçï¼å®ä»¬é½å¯ä»¥åæ¶å å« <type>æ´å</type> å <type>å符串</type> ç䏿 ã </para> <para> å¼å¯ä»¥æ¯ä»»æç PHP ç±»åã </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php $arr = array("somearray" => array(6 => 5, 13 => 9, "a" => 42)); echo $arr["somearray"][6]; // 5 echo $arr["somearray"][13]; // 9 echo $arr["somearray"]["a"]; // 42 ?> ]]> </programlisting> </informalexample> <para> 妿坹ç»åºç弿²¡ææå®é®åï¼ååå½åæå¤§çæ´æ°ç´¢å¼å¼ï¼èæ°çé®åå°æ¯è¯¥å¼å ä¸ã妿æå®çé®åå·²ç»æäºå¼ï¼å该å¼ä¼è¢«è¦çã </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php // è¿ä¸ªæ°ç»ä¸ä¸é¢çæ°ç»ç¸å ... array(5 => 43, 32, 56, "b" => 12); // ... array(5 => 43, 6 => 32, 7 => 56, "b" => 12); ?> ]]> </programlisting> </informalexample> <warning> <simpara> èª PHP 4.3.0 èµ·ï¼ä¸è¿°çç´¢å¼çææ¹æ³æ¹åäºãå¦ä»å¦æç»ä¸ä¸ªå½åæå¤§é®åæ¯è´å¼çæ°ç»æ·»å ä¸ä¸ªæ°å¼ï¼åæ°çæççç´¢å¼å°ä¸ºé¶ï¼<literal>0</literal>ï¼ã以忰çæçç´¢å¼ä¸ºå½åæå¤§ç´¢å¼å ä¸ï¼åæ£å¼çç´¢å¼ç¸åã </simpara> </warning> <para> ä½¿ç¨ &true; ä½ä¸ºé®åå°ä½¿ <type>integer</type> <literal>1</literal> æä¸ºé®åãä½¿ç¨ &false; ä½ä¸ºé®åå°ä½¿ <type>integer</type> <literal>0</literal> æä¸ºé®åãä½¿ç¨ <literal>NULL</literal> ä½ä¸ºé®åå°çåäºä½¿ç¨ç©ºå符串ã使ç¨ç©ºå符串ä½ä¸ºé®åå°æ°å»ºï¼æè¦çï¼ä¸ä¸ªç¨ç©ºå符串ä½ä¸ºé®åçå¼ï¼è¿åç¨ç©ºçæ¹æ¬å·ä¸ä¸æ ·ã </para> <para> ä¸è½ç¨æ°ç»å对象ä½ä¸ºé®(key)ãè¿æ ·åä¼å¯¼è´ä¸ä¸ªè¦åï¼<literal>Illegal offset type</literal>ã </para> </sect3> <sect3 xml:id="language.types.array.syntax.modifying"> <title>ç¨æ¹æ¬å·çè¯æ³æ°å»ºï¼ä¿®æ¹</title> <para> å¯ä»¥éè¿æç¤ºå°è®¾å®å¼æ¥æ¹åä¸ä¸ªç°æçæ°ç»ã </para> <para> è¿æ¯éè¿å¨æ¹æ¬å·å æå®é®åæ¥ç»æ°ç»èµå¼å®ç°çãä¹å¯ä»¥çç¥é®åï¼å¨è¿ç§æ åµä¸ç»åéåå ä¸ä¸å¯¹ç©ºçæ¹æ¬å·ï¼â<literal>[]</literal>âï¼ã </para> <synopsis> $arr[<replaceable>key</replaceable>] = <replaceable>value</replaceable>; $arr[] = <replaceable>value</replaceable>; // <replaceable>key</replaceable> å¯ä»¥æ¯ <type>integer</type> æ <type>string</type> // <replaceable>value</replaceable> å¯ä»¥æ¯ä»»æç±»åçå¼</synopsis> <para> 妿 <varname>$arr</varname> è¿ä¸åå¨ï¼å°ä¼æ°å»ºä¸ä¸ªãè¿ä¹æ¯ä¸ç§å®ä¹æ°ç»çæ¿æ¢æ¹æ³ãè¦æ¹åä¸ä¸ªå¼ï¼åªè¦ç»å®èµä¸ä¸ªæ°å¼ã妿è¦å é¤ä¸ä¸ªé®åï¼å¼å¯¹ï¼è¦å¯¹å®ç¨ <function>unset</function>ã </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php $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 ?> ]]> </programlisting> </informalexample> <note> <para> å¦ä¸æè¿°ï¼å¦æç»åºæ¹æ¬å·ä½æ²¡ææå®é®åï¼ååå½åæå¤§æ´æ°ç´¢å¼å¼ï¼æ°çé®åå°æ¯è¯¥å¼ + 1ã妿å½åè¿æ²¡ææ´æ°ç´¢å¼ï¼åé®åå°ä¸º <literal>0</literal>ã妿æå®çé®åå·²ç»æå¼äºï¼è¯¥å¼å°è¢«è¦çã </para> <para> 注æè¿éæä½¿ç¨çæå¤§æ´æ°é®å<emphasis>ä¸ä¸å®</emphasis>å½å就卿°ç»ä¸ãå®åªè¦å¨ä¸æ¬¡æ°ç»éæ°çæç´¢å¼åæ¾ç»åå¨è¿å°±è¡äºã以ä¸é¢çä¾åæ¥è¯´æï¼ </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php // å建ä¸ä¸ªç®åçæ°ç» $array = array(1, 2, 3, 4, 5); print_r($array); // ç°å¨å é¤å ¶ä¸çææå ç´ ï¼ä½ä¿ææ°ç»æ¬èº«ä¸å: foreach ($array as $i => $value) { unset($array[$i]); } print_r($array); // æ·»å ä¸ä¸ªåå ï¼æ³¨ææ°çé®åæ¯ 5ï¼è䏿¯ä½ å¯è½ä»¥ä¸ºç 0ï¼ $array[] = 6; print_r($array); // éæ°ç´¢å¼ï¼ $array = array_values($array); $array[] = 7; print_r($array); ?> ]]> </programlisting> &example.outputs; <screen> <![CDATA[ Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) Array ( ) Array ( [5] => 6 ) Array ( [0] => 6 [1] => 7 ) ]]> </screen> </informalexample> </note> </sect3> </sect2><!-- end syntax --> <sect2 xml:id="language.types.array.useful-funcs"> <title>å®ç¨å½æ°</title> <para> æå¾å¤æä½æ°ç»ç彿°ï¼åè§<link linkend="ref.array">æ°ç»å½æ°</link>ä¸èã </para> <note> <para> <function>unset</function> 彿°å 许å 餿°ç»ä¸çæä¸ªé®ãä½è¦æ³¨ææ°ç»å°ä¸ä¼é建索å¼ã If a true "remove and shift" behavior is desired, the <type>array</type> can be reindexed using the <function>array_values</function> function. </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php $a = array(1 => 'one', 2 => 'two', 3 => 'three'); unset($a[2]); /* will produce an array that would have been defined as $a = array(1 => 'one', 3 => 'three'); and NOT $a = array(1 => 'one', 2 =>'three'); */ $b = array_values($a); // Now $b is array(0 => 'one', 1 =>'three') ?> ]]> </programlisting> </informalexample> </note> <para> <link linkend="control-structures.foreach">foreach</link> æ§å¶ç»ææ¯ä¸é¨ç¨äºæ°ç»çã宿ä¾äºä¸ä¸ªç®åçæ¹æ³æ¥éåæ°ç»ã </para> </sect2> <sect2 xml:id="language.types.array.donts"> <title>æ°ç»åä»ä¹åä¸åä»ä¹</title> <sect3 xml:id="language.types.array.foo-bar"> <title>为ä»ä¹ <literal>$foo[bar]</literal> éäºï¼</title> <para> åºè¯¥å§ç»å¨ç¨åç¬¦ä¸²è¡¨ç¤ºçæ°ç»ç´¢å¼ä¸å ä¸å¼å·ãä¾å¦ç¨ $foo['bar'] è䏿¯ $foo[bar]ã使¯ä¸ºä»ä¹ $foo[bar] éäºå¢ï¼å¯è½å¨èçèæ¬ä¸è§è¿å¦ä¸è¯æ³ï¼ </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php $foo[bar] = 'enemy'; echo $foo[bar]; // etc ?> ]]> </programlisting> </informalexample> <para> è¿æ ·æ¯éçï¼ä½å¯ä»¥æ£å¸¸è¿è¡ãé£ä¹ä¸ºä»ä¹éäºå¢ï¼åå æ¯æ¤ä»£ç 䏿ä¸ä¸ªæªå®ä¹ç常éï¼barï¼è䏿¯å符串ï¼'bar'ï¼æ³¨æå¼å·ï¼ï¼è PHP å¯è½ä¼å¨ä»¥åå®ä¹æ¤å¸¸éï¼ä¸å¹¸çæ¯ä½ ç代ç ä¸æåæ ·çååãå®è½è¿è¡ï¼æ¯å 为 PHP èªå¨å°<emphasis>裸å符串</emphasis>ï¼æ²¡æå¼å·çå符串ä¸ä¸å¯¹åºäºä»»ä½å·²ç¥ç¬¦å·ï¼è½¬æ¢æä¸ä¸ªå ¶å¼ä¸ºè¯¥è£¸åç¬¦ä¸²çæ£å¸¸å符串ãä¾å¦ï¼å¦ææ²¡æå¸¸éå®ä¹ä¸º <constant>bar</constant>ï¼PHP 尿宿¿ä»£ä¸º <literal>'bar'</literal> 并使ç¨ä¹ã </para> <note> <simpara> è¿å¹¶ä¸æå³ç<emphasis>æ»æ¯</emphasis>ç»é®åå ä¸å¼å·ãç¨ä¸çç»é®å为<link linkend="language.constants">常é</link>æ<link linkend="language.variables">åé</link>çå ä¸å¼å·ï¼å¦åä¼ä½¿ PHP ä¸è½è§£æå®ä»¬ã </simpara> <informalexample> <programlisting role="php"> <![CDATA[ <?php error_reporting(E_ALL); ini_set('display_errors', true); ini_set('html_errors', false); // Simple array: $array = array(1, 2); $count = count($array); for ($i = 0; $i < $count; $i++) { echo "\nChecking $i: \n"; echo "Bad: " . $array['$i'] . "\n"; echo "Good: " . $array[$i] . "\n"; echo "Bad: {$array['$i']}\n"; echo "Good: {$array[$i]}\n"; } ?> ]]> </programlisting> </informalexample> &example.outputs; <screen> <![CDATA[ Checking 0: Notice: Undefined index: $i in /path/to/script.html on line 9 Bad: Good: 1 Notice: Undefined index: $i in /path/to/script.html on line 11 Bad: Good: 1 Checking 1: Notice: Undefined index: $i in /path/to/script.html on line 9 Bad: Good: 2 Notice: Undefined index: $i in /path/to/script.html on line 11 Bad: Good: 2 ]]> </screen> </note> <para> æ¼ç¤ºæ¤è¡ä¸ºçæ´å¤ä¾åï¼ </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php // Show all errors error_reporting(E_ALL); $arr = array('fruit' => 'apple', 'veggie' => 'carrot'); // Correct print $arr['fruit']; // apple print $arr['veggie']; // carrot // Incorrect. This works but also throws a PHP error of level E_NOTICE because // of an undefined constant named fruit // // Notice: Use of undefined constant fruit - assumed 'fruit' in... print $arr[fruit]; // apple // This defines a constant to demonstrate what's going on. The value 'veggie' // is assigned to a constant named fruit. define('fruit', 'veggie'); // Notice the difference now print $arr['fruit']; // apple print $arr[fruit]; // carrot // The following is okay, as it's inside a string. Constants are not looked for // within strings, so no E_NOTICE occurs here print "Hello $arr[fruit]"; // Hello apple // With one exception: braces surrounding arrays within strings allows constants // to be interpreted print "Hello {$arr[fruit]}"; // Hello carrot print "Hello {$arr['fruit']}"; // Hello apple // This will not work, and will result in a parse error, such as: // Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING' // This of course applies to using superglobals in strings as well print "Hello $arr['fruit']"; print "Hello $_GET['foo']"; // Concatenation is another option print "Hello " . $arr['fruit']; // Hello apple ?> ]]> </programlisting> </informalexample> <para> å½æå¼ <link linkend="ini.error-reporting">error_reporting</link> æ¥æ¾ç¤º <constant>E_NOTICE</constant> 级å«çé误ï¼ä¾å¦å°å ¶è®¾ä¸º <constant>E_ALL</constant>ï¼æ¶å°çå°è¿äºé误ãé»è®¤æ åµä¸ <link linkend="ini.error-reporting">error_reporting</link> è¢«å ³é䏿¾ç¤ºè¿äºã </para> <para> åå¨<link linkend="language.types.array.syntax">è¯æ³</link>ä¸èä¸è§å®ç䏿 ·ï¼å¨æ¹æ¬å·ï¼â<literal>[</literal>âåâ<literal>]</literal>âï¼ä¹é´å¿ é¡»æä¸ä¸ªè¡¨è¾¾å¼ãè¿æå³çå¯ä»¥è¿æ ·åï¼ </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php echo $arr[somefunc($bar)]; ?> ]]> </programlisting> </informalexample> <para> è¿æ¯ä¸ä¸ªç¨å½æ°è¿åå¼ä½ä¸ºæ°ç»ç´¢å¼çä¾åãPHP ä¹å¯ä»¥ç¨å·²ç¥å¸¸éï¼å¯è½ä¹åå·²ç»è§è¿ </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php $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"; ?> ]]> </programlisting> </informalexample> <para> 注æ <literal>E_ERROR</literal> 乿¯ä¸ªåæ³çæ è¯ç¬¦ï¼å°±å第ä¸ä¸ªä¾åä¸ç <literal>bar</literal> 䏿 ·ã使¯ä¸ä¸ä¸ªä¾åå®é ä¸åå¦ä¸åæ³æ¯ä¸æ ·çï¼ </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php $error_descriptions[1] = "A fatal error has occured"; $error_descriptions[2] = "PHP issued a warning"; $error_descriptions[8] = "This is just an informal notice"; ?> ]]> </programlisting> </informalexample> <para> å 为 <literal>E_ERROR</literal> çäº <literal>1</literal>, çç. </para> <sect4 xml:id="language.types.array.foo-bar.why"> <title>é£ä¹ä¸ºä»ä¹è¿æ ·åä¸å¥½ï¼</title> <para> ä¹è®¸æä¸å¤©ï¼PHP å¼åå°ç»å¯è½ä¼æ³æ°å¢ä¸ä¸ªå¸¸éæè å ³é®åï¼æè ç¨æ·å¯è½å¸æä»¥åå¨èªå·±çç¨åºä¸å¼å ¥æ°ç常éï¼é£å°±æéº»ç¦äºãä¾å¦å·²ç»ä¸è½è¿æ ·ç¨ <literal>empty</literal> å <literal>default</literal> è¿ä¸¤ä¸ªè¯äºï¼å 为ä»ä»¬æ¯<link linkend="reserved">ä¿çå</link>ã </para> <note> <simpara> éç³ä¸æ¬¡ï¼å¨åå¼å·å符串ä¸ï¼ä¸ç»ç´¢å¼å ä¸å¼å·æ¯åæ³çå æ¤ <literal>"$foo[bar]"</literal>æ¯åæ³çãè³äºä¸ºä»ä¹åè§ä»¥ä¸çä¾åå<link linkend="language.types.string.parsing">å符串ä¸çåéè§£æ</link>ä¸çè§£éã </simpara> </note> </sect4> </sect3> </sect2> <sect2 xml:id="language.types.array.casting"> <title>转æ¢ä¸ºæ°ç»</title> <para> 对äºä»»æç±»å: <type>integer</type>, <type>float</type>, <type>string</type>, <type>boolean</type> and <type>resource</type>,妿å°ä¸ä¸ªå¼è½¬æ¢ä¸ºæ°ç»ï¼å°å¾å°ä¸ä¸ªä» æä¸ä¸ªå ç´ çæ°ç»ï¼å ¶ä¸æ 为 0ï¼ï¼è¯¥å ç´ å³ä¸ºæ¤æ éçå¼ãæ¢å¥è¯è¯´ï¼ <literal>(array)$scalarValue</literal> ä¸ <literal>array($scalarValue)</literal> å®å ¨ä¸æ ·ã </para> <para> If an <type>object</type> is converted to an <type>array</type>, the result is an <type>array</type> whose elements are the <type>object</type>'s properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side. This can result in some unexpected behaviour: </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php class A { private $A; // This will become '\0A\0A' } class B extends A { private $A; // This will become '\0B\0A' public $AA; // This will become 'AA' } var_dump((array) new B()); ?> ]]> </programlisting> </informalexample> <para> The above will appear to have two keys named 'AA', although one of them is actually named '\0A\0A'. </para> <para> å° &null; 转æ¢å° <type>æ°ç»(array)</type> ä¼å¾å°ä¸ä¸ªç©ºçæ°ç»ã </para> </sect2> <sect2 xml:id="language.types.array.comparing"> <title>æ¯è¾</title> <para> å¯è½ä½¿ç¨ <function>array_diff</function> å<link linkend="language.operators.array">æ°ç»è¿ç®ç¬¦</link>æ¥æ¯è¾æ°ç»ã </para> </sect2> <sect2 xml:id="language.types.array.examples"> <title>Examples</title> <para> PHP ä¸çæ°ç»ç±»åæé常å¤çç¨éï¼å æ¤è¿éæä¸äºä¾åå±ç¤ºæ°ç»ç宿´å¨åã </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php // This: $a = array( 'color' => 'red', 'taste' => 'sweet', 'shape' => 'round', 'name' => 'apple', 4 // key will be 0 ); $b = array('a', 'b', 'c'); // . . .is completely equivalent with this: $a = array(); $a['color'] = 'red'; $a['taste'] = 'sweet'; $a['shape'] = 'round'; $a['name'] = 'apple'; $a[] = 4; // key will be 0 $b = array(); $b[] = 'a'; $b[] = 'b'; $b[] = 'c'; // After the above code is executed, $a will be the array // array('color' => 'red', 'taste' => 'sweet', 'shape' => 'round', // 'name' => 'apple', 0 => 4), and $b will be the array // array(0 => 'a', 1 => 'b', 2 => 'c'), or simply array('a', 'b', 'c'). ?> ]]> </programlisting> </informalexample> <example> <title>Using array()</title> <programlisting role="php"> <![CDATA[ <?php // 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(); ?> ]]> <!-- TODO example of - overwriting keys - using vars/functions as key/values - warning about references --> </programlisting> </example> <example xml:id="language.types.array.examples.loop"> <title>éå</title> <programlisting role="php"> <![CDATA[ <?php $colors = array('red', 'blue', 'green', 'yellow'); foreach ($colors as $color) { echo "Do you like $color?\n"; } ?> ]]> </programlisting> &example.outputs; <screen> <![CDATA[ Do you like red? Do you like blue? Do you like green? Do you like yellow? ]]> </screen> </example> <para> ç´æ¥æ¹åæ°ç»çå¼å¨ PHP 5 ä¸å¯ä»¥éè¿å¼ç¨ä¼ 鿥åå°ãä¹åççæ¬éè¦éè¦éååéçæ¹æ³ï¼ </para> <example xml:id="language.types.array.examples.changeloop"> <title>éå</title> <programlisting role="php"> <![CDATA[ <?php // PHP 5 foreach ($colors as &$color) { $color = strtoupper($color); } unset($color); /* ensure that following writes to $color will not modify the last array element */ // Workaround for older versions foreach ($colors as $key => $color) { $colors[$key] = strtoupper($color); } print_r($colors); ?> ]]> </programlisting> &example.outputs; <screen> <![CDATA[ Array ( [0] => RED [1] => BLUE [2] => GREEN [3] => YELLOW ) ]]> </screen> </example> <para> æ¬ä¾çæä¸ä¸ªä¸æ ä»1å¼å§çæ°ç»ãThis example creates a one-based array. </para> <example> <title>䏿 ä»1å¼å§çæ°ç»</title> <programlisting role="php"> <![CDATA[ <?php $firstquarter = array(1 => 'January', 'February', 'March'); print_r($firstquarter); ?> ]]> </programlisting> &example.outputs; <screen> <![CDATA[ Array ( [1] => 'January' [2] => 'February' [3] => 'March' ) ]]> </screen> </example> <example> <title>å¡«å æ°ç»</title> <programlisting role="php"> <![CDATA[ <?php // fill an array with all items from a directory $handle = opendir('.'); while (false !== ($file = readdir($handle))) { $files[] = $file; } closedir($handle); ?> ]]> </programlisting> </example> <para> æ°ç»æ¯æåºçãä¹å¯ä»¥ä½¿ç¨ä¸åçæåºå½æ°æ¥æ¹å顺åºãæ´å¤ä¿¡æ¯åè§<link linkend="ref.array">æ°ç»å½æ°</link>ãå¯ä»¥ç¨ <function>count</function> 彿°æ¥æ°åºæ°ç»ä¸å ç´ ç个æ°ã </para> <example> <title>æ°ç»æåº</title> <programlisting role="php"> <![CDATA[ <?php sort($files); print_r($files); ?> ]]> </programlisting> </example> <para> å 为æ°ç»ä¸çå¼å¯ä»¥ä¸ºä»»æå¼ï¼ä¹å¯æ¯å¦ä¸ä¸ªæ°ç»ãè¿æ ·å¯ä»¥äº§çé彿å¤ç»´æ°ç»ã </para> <example> <title>éå½åå¤ç»´æ°ç»</title> <programlisting role="php"> <![CDATA[ <?php $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"; ?> ]]> </programlisting> </example> <para> <type>æ°ç»(Array)</type> çèµå¼æ»æ¯ä¼æ¶åå°å¼çæ·è´ãä½¿ç¨ <link linkend="language.operators">å¼ç¨æä½ç¬¦</link> éè¿å¼ç¨æ¥æ·è´æ°ç»ã </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php $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 ?> ]]> </programlisting> </informalexample> </sect2> </sect1> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t indent-tabs-mode:nil sgml-parent-document:nil sgml-default-dtd-file:"../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 -->
http://cvs.php.net/viewvc.cgi/phpdoc-zh/language/types/array.xml?view=markup&rev=1.1 Index: phpdoc-zh/language/types/array.xml +++ phpdoc-zh/language/types/array.xml <?xml version="1.0" encoding="utf-8"?> <!-- $Revision: 1.1 $ --> <!-- $Author: eniac2008 --> <!-- EN-Revision: 1.7 Maintainer: Altair Status: ready --> <!-- CREDITS: Geogory dallas --> <sect1 xml:id="language.types.array"> <title>æ°ç»</title> <para> PHP ä¸ç <type>æ°ç»</type> å®é 䏿¯ä¸ä¸ªæåºæ å°ãæ å°æ¯ä¸ç§æ <emphasis>values</emphasis> å ³èå° <emphasis>keys</emphasis> çç±»åãæ¤ç±»åå¨å¾å¤æ¹é¢åäºä¼åï¼å æ¤å¯ä»¥æå®å½æçæ£çæ°ç»ï¼æå表ï¼åéï¼ï¼æ£åè¡¨ï¼æ¯æ å°çä¸ç§å®ç°ï¼ï¼åå ¸ï¼éåï¼æ ï¼éå以忴å¤å¯è½æ§ãæ°ç»å ç´ çå¼ä¹å¯ä»¥æ¯å¦ä¸ä¸ªæ°ç»ãæ å½¢ç»æåå¤ç»´æ°ç»ä¹æ¯å 许çã </para> <para> è§£éè¿äºç»æè¶ åºäºæ¬æåçèå´ï¼ä½å¯¹äºæ¯ç§ç»æè³å°ä¼æä¾ä¸ä¸ªä¾åãè¦å¾å°è¿äºç»æçæ´å¤ä¿¡æ¯ï¼å»ºè®®åèæå ³æ¤å¹¿é主é¢çå ¶å®èä½ã </para> <sect2 xml:id="language.types.array.syntax"> <title>è¯æ³</title> <sect3 xml:id="language.types.array.syntax.array-func"> <title>å®ä¹æ°ç» <function>array</function></title> <para> å¯ä»¥ç¨ <function>array</function> è¯è¨ç»ææ¥æ°å»ºä¸ä¸ª <type>array</type>ã宿¥åä»»ææ°éç¨éå·åéç <literal><replaceable>é®(key)</replaceable> => <replaceable>å¼(value)</replaceable></literal> 对ã </para> <synopsis> array( <optional> <replaceable>key</replaceable> => </optional> <replaceable>value</replaceable> , ... ) // <replaceable>é®(key)</replaceable> 坿¯æ¯ä¸ä¸ª <type>æ´æ°(integer)</type> æ <type>å符串(string)</type> // <replaceable>å¼(value)</replaceable> å¯ä»¥æ¯ä»»æç±»åçå¼</synopsis> <!-- Do not fix the whitespace for the synopsis end element. A limitation of PhD prevents proper trimming --> <informalexample> <programlisting role="php"> <![CDATA[ <?php $arr = array("foo" => "bar", 12 => true); echo $arr["foo"]; // bar echo $arr[12]; // 1 ?> ]]> </programlisting> </informalexample> <para> <varname>key</varname> å¯ä»¥æ¯ <type>integer</type> æè <type>string</type>ã妿keyæ¯ä¸ä¸ª <type>integer</type> çæ å表示ï¼å被解éä¸ºæ´æ°ï¼ä¾å¦ <literal>"8"</literal> å°è¢«è§£é为 <literal>8</literal>ï¼è <literal>"08"</literal> å°è¢«è§£é为 <literal>"08"</literal>ï¼ã<varname>key</varname> ä¸çæµ®ç¹æ°è¢«åæ´ä¸º <type>integer</type>ãå¨ PHP ä¸ç´¢å¼æ°ç»ä¸å ³è <type>æ°ç»</type> æ¯ç¸åçï¼å®ä»¬é½å¯ä»¥åæ¶å å« <type>æ´å</type> å <type>å符串</type> ç䏿 ã </para> <para> å¼å¯ä»¥æ¯ä»»æç PHP ç±»åã </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php $arr = array("somearray" => array(6 => 5, 13 => 9, "a" => 42)); echo $arr["somearray"][6]; // 5 echo $arr["somearray"][13]; // 9 echo $arr["somearray"]["a"]; // 42 ?> ]]> </programlisting> </informalexample> <para> 妿坹ç»åºç弿²¡ææå®é®åï¼ååå½åæå¤§çæ´æ°ç´¢å¼å¼ï¼èæ°çé®åå°æ¯è¯¥å¼å ä¸ã妿æå®çé®åå·²ç»æäºå¼ï¼å该å¼ä¼è¢«è¦çã </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php // è¿ä¸ªæ°ç»ä¸ä¸é¢çæ°ç»ç¸å ... array(5 => 43, 32, 56, "b" => 12); // ... array(5 => 43, 6 => 32, 7 => 56, "b" => 12); ?> ]]> </programlisting> </informalexample> <warning> <simpara> èª PHP 4.3.0 èµ·ï¼ä¸è¿°çç´¢å¼çææ¹æ³æ¹åäºãå¦ä»å¦æç»ä¸ä¸ªå½åæå¤§é®åæ¯è´å¼çæ°ç»æ·»å ä¸ä¸ªæ°å¼ï¼åæ°çæççç´¢å¼å°ä¸ºé¶ï¼<literal>0</literal>ï¼ã以忰çæçç´¢å¼ä¸ºå½åæå¤§ç´¢å¼å ä¸ï¼åæ£å¼çç´¢å¼ç¸åã </simpara> </warning> <para> ä½¿ç¨ &true; ä½ä¸ºé®åå°ä½¿ <type>integer</type> <literal>1</literal> æä¸ºé®åãä½¿ç¨ &false; ä½ä¸ºé®åå°ä½¿ <type>integer</type> <literal>0</literal> æä¸ºé®åãä½¿ç¨ <literal>NULL</literal> ä½ä¸ºé®åå°çåäºä½¿ç¨ç©ºå符串ã使ç¨ç©ºå符串ä½ä¸ºé®åå°æ°å»ºï¼æè¦çï¼ä¸ä¸ªç¨ç©ºå符串ä½ä¸ºé®åçå¼ï¼è¿åç¨ç©ºçæ¹æ¬å·ä¸ä¸æ ·ã </para> <para> ä¸è½ç¨æ°ç»å对象ä½ä¸ºé®(key)ãè¿æ ·åä¼å¯¼è´ä¸ä¸ªè¦åï¼<literal>Illegal offset type</literal>ã </para> </sect3> <sect3 xml:id="language.types.array.syntax.modifying"> <title>ç¨æ¹æ¬å·çè¯æ³æ°å»ºï¼ä¿®æ¹</title> <para> å¯ä»¥éè¿æç¤ºå°è®¾å®å¼æ¥æ¹åä¸ä¸ªç°æçæ°ç»ã </para> <para> è¿æ¯éè¿å¨æ¹æ¬å·å æå®é®åæ¥ç»æ°ç»èµå¼å®ç°çãä¹å¯ä»¥çç¥é®åï¼å¨è¿ç§æ åµä¸ç»åéåå ä¸ä¸å¯¹ç©ºçæ¹æ¬å·ï¼â<literal>[]</literal>âï¼ã </para> <synopsis> $arr[<replaceable>key</replaceable>] = <replaceable>value</replaceable>; $arr[] = <replaceable>value</replaceable>; // <replaceable>key</replaceable> å¯ä»¥æ¯ <type>integer</type> æ <type>string</type> // <replaceable>value</replaceable> å¯ä»¥æ¯ä»»æç±»åçå¼</synopsis> <para> 妿 <varname>$arr</varname> è¿ä¸åå¨ï¼å°ä¼æ°å»ºä¸ä¸ªãè¿ä¹æ¯ä¸ç§å®ä¹æ°ç»çæ¿æ¢æ¹æ³ãè¦æ¹åä¸ä¸ªå¼ï¼åªè¦ç»å®èµä¸ä¸ªæ°å¼ã妿è¦å é¤ä¸ä¸ªé®åï¼å¼å¯¹ï¼è¦å¯¹å®ç¨ <function>unset</function>ã </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php $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 ?> ]]> </programlisting> </informalexample> <note> <para> å¦ä¸æè¿°ï¼å¦æç»åºæ¹æ¬å·ä½æ²¡ææå®é®åï¼ååå½åæå¤§æ´æ°ç´¢å¼å¼ï¼æ°çé®åå°æ¯è¯¥å¼ + 1ã妿å½åè¿æ²¡ææ´æ°ç´¢å¼ï¼åé®åå°ä¸º <literal>0</literal>ã妿æå®çé®åå·²ç»æå¼äºï¼è¯¥å¼å°è¢«è¦çã </para> <para> 注æè¿éæä½¿ç¨çæå¤§æ´æ°é®å<emphasis>ä¸ä¸å®</emphasis>å½å就卿°ç»ä¸ãå®åªè¦å¨ä¸æ¬¡æ°ç»éæ°çæç´¢å¼åæ¾ç»åå¨è¿å°±è¡äºã以ä¸é¢çä¾åæ¥è¯´æï¼ </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php // å建ä¸ä¸ªç®åçæ°ç» $array = array(1, 2, 3, 4, 5); print_r($array); // ç°å¨å é¤å ¶ä¸çææå ç´ ï¼ä½ä¿ææ°ç»æ¬èº«ä¸å: foreach ($array as $i => $value) { unset($array[$i]); } print_r($array); // æ·»å ä¸ä¸ªåå ï¼æ³¨ææ°çé®åæ¯ 5ï¼è䏿¯ä½ å¯è½ä»¥ä¸ºç 0ï¼ $array[] = 6; print_r($array); // éæ°ç´¢å¼ï¼ $array = array_values($array); $array[] = 7; print_r($array); ?> ]]> </programlisting> &example.outputs; <screen> <![CDATA[ Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) Array ( ) Array ( [5] => 6 ) Array ( [0] => 6 [1] => 7 ) ]]> </screen> </informalexample> </note> </sect3> </sect2><!-- end syntax --> <sect2 xml:id="language.types.array.useful-funcs"> <title>å®ç¨å½æ°</title> <para> æå¾å¤æä½æ°ç»ç彿°ï¼åè§<link linkend="ref.array">æ°ç»å½æ°</link>ä¸èã </para> <note> <para> <function>unset</function> 彿°å 许å 餿°ç»ä¸çæä¸ªé®ãä½è¦æ³¨ææ°ç»å°ä¸ä¼é建索å¼ã If a true "remove and shift" behavior is desired, the <type>array</type> can be reindexed using the <function>array_values</function> function. </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php $a = array(1 => 'one', 2 => 'two', 3 => 'three'); unset($a[2]); /* will produce an array that would have been defined as $a = array(1 => 'one', 3 => 'three'); and NOT $a = array(1 => 'one', 2 =>'three'); */ $b = array_values($a); // Now $b is array(0 => 'one', 1 =>'three') ?> ]]> </programlisting> </informalexample> </note> <para> <link linkend="control-structures.foreach">foreach</link> æ§å¶ç»ææ¯ä¸é¨ç¨äºæ°ç»çã宿ä¾äºä¸ä¸ªç®åçæ¹æ³æ¥éåæ°ç»ã </para> </sect2> <sect2 xml:id="language.types.array.donts"> <title>æ°ç»åä»ä¹åä¸åä»ä¹</title> <sect3 xml:id="language.types.array.foo-bar"> <title>为ä»ä¹ <literal>$foo[bar]</literal> éäºï¼</title> <para> åºè¯¥å§ç»å¨ç¨åç¬¦ä¸²è¡¨ç¤ºçæ°ç»ç´¢å¼ä¸å ä¸å¼å·ãä¾å¦ç¨ $foo['bar'] è䏿¯ $foo[bar]ã使¯ä¸ºä»ä¹ $foo[bar] éäºå¢ï¼å¯è½å¨èçèæ¬ä¸è§è¿å¦ä¸è¯æ³ï¼ </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php $foo[bar] = 'enemy'; echo $foo[bar]; // etc ?> ]]> </programlisting> </informalexample> <para> è¿æ ·æ¯éçï¼ä½å¯ä»¥æ£å¸¸è¿è¡ãé£ä¹ä¸ºä»ä¹éäºå¢ï¼åå æ¯æ¤ä»£ç 䏿ä¸ä¸ªæªå®ä¹ç常éï¼barï¼è䏿¯å符串ï¼'bar'ï¼æ³¨æå¼å·ï¼ï¼è PHP å¯è½ä¼å¨ä»¥åå®ä¹æ¤å¸¸éï¼ä¸å¹¸çæ¯ä½ ç代ç ä¸æåæ ·çååãå®è½è¿è¡ï¼æ¯å 为 PHP èªå¨å°<emphasis>裸å符串</emphasis>ï¼æ²¡æå¼å·çå符串ä¸ä¸å¯¹åºäºä»»ä½å·²ç¥ç¬¦å·ï¼è½¬æ¢æä¸ä¸ªå ¶å¼ä¸ºè¯¥è£¸åç¬¦ä¸²çæ£å¸¸å符串ãä¾å¦ï¼å¦ææ²¡æå¸¸éå®ä¹ä¸º <constant>bar</constant>ï¼PHP 尿宿¿ä»£ä¸º <literal>'bar'</literal> 并使ç¨ä¹ã </para> <note> <simpara> è¿å¹¶ä¸æå³ç<emphasis>æ»æ¯</emphasis>ç»é®åå ä¸å¼å·ãç¨ä¸çç»é®å为<link linkend="language.constants">常é</link>æ<link linkend="language.variables">åé</link>çå ä¸å¼å·ï¼å¦åä¼ä½¿ PHP ä¸è½è§£æå®ä»¬ã </simpara> <informalexample> <programlisting role="php"> <![CDATA[ <?php error_reporting(E_ALL); ini_set('display_errors', true); ini_set('html_errors', false); // Simple array: $array = array(1, 2); $count = count($array); for ($i = 0; $i < $count; $i++) { echo "\nChecking $i: \n"; echo "Bad: " . $array['$i'] . "\n"; echo "Good: " . $array[$i] . "\n"; echo "Bad: {$array['$i']}\n"; echo "Good: {$array[$i]}\n"; } ?> ]]> </programlisting> </informalexample> &example.outputs; <screen> <![CDATA[ Checking 0: Notice: Undefined index: $i in /path/to/script.html on line 9 Bad: Good: 1 Notice: Undefined index: $i in /path/to/script.html on line 11 Bad: Good: 1 Checking 1: Notice: Undefined index: $i in /path/to/script.html on line 9 Bad: Good: 2 Notice: Undefined index: $i in /path/to/script.html on line 11 Bad: Good: 2 ]]> </screen> </note> <para> æ¼ç¤ºæ¤è¡ä¸ºçæ´å¤ä¾åï¼ </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php // Show all errors error_reporting(E_ALL); $arr = array('fruit' => 'apple', 'veggie' => 'carrot'); // Correct print $arr['fruit']; // apple print $arr['veggie']; // carrot // Incorrect. This works but also throws a PHP error of level E_NOTICE because // of an undefined constant named fruit // // Notice: Use of undefined constant fruit - assumed 'fruit' in... print $arr[fruit]; // apple // This defines a constant to demonstrate what's going on. The value 'veggie' // is assigned to a constant named fruit. define('fruit', 'veggie'); // Notice the difference now print $arr['fruit']; // apple print $arr[fruit]; // carrot // The following is okay, as it's inside a string. Constants are not looked for // within strings, so no E_NOTICE occurs here print "Hello $arr[fruit]"; // Hello apple // With one exception: braces surrounding arrays within strings allows constants // to be interpreted print "Hello {$arr[fruit]}"; // Hello carrot print "Hello {$arr['fruit']}"; // Hello apple // This will not work, and will result in a parse error, such as: // Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING' // This of course applies to using superglobals in strings as well print "Hello $arr['fruit']"; print "Hello $_GET['foo']"; // Concatenation is another option print "Hello " . $arr['fruit']; // Hello apple ?> ]]> </programlisting> </informalexample> <para> å½æå¼ <link linkend="ini.error-reporting">error_reporting</link> æ¥æ¾ç¤º <constant>E_NOTICE</constant> 级å«çé误ï¼ä¾å¦å°å ¶è®¾ä¸º <constant>E_ALL</constant>ï¼æ¶å°çå°è¿äºé误ãé»è®¤æ åµä¸ <link linkend="ini.error-reporting">error_reporting</link> è¢«å ³é䏿¾ç¤ºè¿äºã </para> <para> åå¨<link linkend="language.types.array.syntax">è¯æ³</link>ä¸èä¸è§å®ç䏿 ·ï¼å¨æ¹æ¬å·ï¼â<literal>[</literal>âåâ<literal>]</literal>âï¼ä¹é´å¿ é¡»æä¸ä¸ªè¡¨è¾¾å¼ãè¿æå³çå¯ä»¥è¿æ ·åï¼ </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php echo $arr[somefunc($bar)]; ?> ]]> </programlisting> </informalexample> <para> è¿æ¯ä¸ä¸ªç¨å½æ°è¿åå¼ä½ä¸ºæ°ç»ç´¢å¼çä¾åãPHP ä¹å¯ä»¥ç¨å·²ç¥å¸¸éï¼å¯è½ä¹åå·²ç»è§è¿ </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php $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"; ?> ]]> </programlisting> </informalexample> <para> 注æ <literal>E_ERROR</literal> 乿¯ä¸ªåæ³çæ è¯ç¬¦ï¼å°±å第ä¸ä¸ªä¾åä¸ç <literal>bar</literal> 䏿 ·ã使¯ä¸ä¸ä¸ªä¾åå®é ä¸åå¦ä¸åæ³æ¯ä¸æ ·çï¼ </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php $error_descriptions[1] = "A fatal error has occured"; $error_descriptions[2] = "PHP issued a warning"; $error_descriptions[8] = "This is just an informal notice"; ?> ]]> </programlisting> </informalexample> <para> å 为 <literal>E_ERROR</literal> çäº <literal>1</literal>, çç. </para> <sect4 xml:id="language.types.array.foo-bar.why"> <title>é£ä¹ä¸ºä»ä¹è¿æ ·åä¸å¥½ï¼</title> <para> ä¹è®¸æä¸å¤©ï¼PHP å¼åå°ç»å¯è½ä¼æ³æ°å¢ä¸ä¸ªå¸¸éæè å ³é®åï¼æè ç¨æ·å¯è½å¸æä»¥åå¨èªå·±çç¨åºä¸å¼å ¥æ°ç常éï¼é£å°±æéº»ç¦äºãä¾å¦å·²ç»ä¸è½è¿æ ·ç¨ <literal>empty</literal> å <literal>default</literal> è¿ä¸¤ä¸ªè¯äºï¼å 为ä»ä»¬æ¯<link linkend="reserved">ä¿çå</link>ã </para> <note> <simpara> éç³ä¸æ¬¡ï¼å¨åå¼å·å符串ä¸ï¼ä¸ç»ç´¢å¼å ä¸å¼å·æ¯åæ³çå æ¤ <literal>"$foo[bar]"</literal>æ¯åæ³çãè³äºä¸ºä»ä¹åè§ä»¥ä¸çä¾åå<link linkend="language.types.string.parsing">å符串ä¸çåéè§£æ</link>ä¸çè§£éã </simpara> </note> </sect4> </sect3> </sect2> <sect2 xml:id="language.types.array.casting"> <title>转æ¢ä¸ºæ°ç»</title> <para> 对äºä»»æç±»å: <type>integer</type>, <type>float</type>, <type>string</type>, <type>boolean</type> and <type>resource</type>,妿å°ä¸ä¸ªå¼è½¬æ¢ä¸ºæ°ç»ï¼å°å¾å°ä¸ä¸ªä» æä¸ä¸ªå ç´ çæ°ç»ï¼å ¶ä¸æ 为 0ï¼ï¼è¯¥å ç´ å³ä¸ºæ¤æ éçå¼ãæ¢å¥è¯è¯´ï¼ <literal>(array)$scalarValue</literal> ä¸ <literal>array($scalarValue)</literal> å®å ¨ä¸æ ·ã </para> <para> If an <type>object</type> is converted to an <type>array</type>, the result is an <type>array</type> whose elements are the <type>object</type>'s properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side. This can result in some unexpected behaviour: </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php class A { private $A; // This will become '\0A\0A' } class B extends A { private $A; // This will become '\0B\0A' public $AA; // This will become 'AA' } var_dump((array) new B()); ?> ]]> </programlisting> </informalexample> <para> The above will appear to have two keys named 'AA', although one of them is actually named '\0A\0A'. </para> <para> å° &null; 转æ¢å° <type>æ°ç»(array)</type> ä¼å¾å°ä¸ä¸ªç©ºçæ°ç»ã </para> </sect2> <sect2 xml:id="language.types.array.comparing"> <title>æ¯è¾</title> <para> å¯è½ä½¿ç¨ <function>array_diff</function> å<link linkend="language.operators.array">æ°ç»è¿ç®ç¬¦</link>æ¥æ¯è¾æ°ç»ã </para> </sect2> <sect2 xml:id="language.types.array.examples"> <title>Examples</title> <para> PHP ä¸çæ°ç»ç±»åæé常å¤çç¨éï¼å æ¤è¿éæä¸äºä¾åå±ç¤ºæ°ç»ç宿´å¨åã </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php // This: $a = array( 'color' => 'red', 'taste' => 'sweet', 'shape' => 'round', 'name' => 'apple', 4 // key will be 0 ); $b = array('a', 'b', 'c'); // . . .is completely equivalent with this: $a = array(); $a['color'] = 'red'; $a['taste'] = 'sweet'; $a['shape'] = 'round'; $a['name'] = 'apple'; $a[] = 4; // key will be 0 $b = array(); $b[] = 'a'; $b[] = 'b'; $b[] = 'c'; // After the above code is executed, $a will be the array // array('color' => 'red', 'taste' => 'sweet', 'shape' => 'round', // 'name' => 'apple', 0 => 4), and $b will be the array // array(0 => 'a', 1 => 'b', 2 => 'c'), or simply array('a', 'b', 'c'). ?> ]]> </programlisting> </informalexample> <example> <title>Using array()</title> <programlisting role="php"> <![CDATA[ <?php // 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(); ?> ]]> <!-- TODO example of - overwriting keys - using vars/functions as key/values - warning about references --> </programlisting> </example> <example xml:id="language.types.array.examples.loop"> <title>éå</title> <programlisting role="php"> <![CDATA[ <?php $colors = array('red', 'blue', 'green', 'yellow'); foreach ($colors as $color) { echo "Do you like $color?\n"; } ?> ]]> </programlisting> &example.outputs; <screen> <![CDATA[ Do you like red? Do you like blue? Do you like green? Do you like yellow? ]]> </screen> </example> <para> ç´æ¥æ¹åæ°ç»çå¼å¨ PHP 5 ä¸å¯ä»¥éè¿å¼ç¨ä¼ 鿥åå°ãä¹åççæ¬éè¦éè¦éååéçæ¹æ³ï¼ </para> <example xml:id="language.types.array.examples.changeloop"> <title>éå</title> <programlisting role="php"> <![CDATA[ <?php // PHP 5 foreach ($colors as &$color) { $color = strtoupper($color); } unset($color); /* ensure that following writes to $color will not modify the last array element */ // Workaround for older versions foreach ($colors as $key => $color) { $colors[$key] = strtoupper($color); } print_r($colors); ?> ]]> </programlisting> &example.outputs; <screen> <![CDATA[ Array ( [0] => RED [1] => BLUE [2] => GREEN [3] => YELLOW ) ]]> </screen> </example> <para> æ¬ä¾çæä¸ä¸ªä¸æ ä»1å¼å§çæ°ç»ãThis example creates a one-based array. </para> <example> <title>䏿 ä»1å¼å§çæ°ç»</title> <programlisting role="php"> <![CDATA[ <?php $firstquarter = array(1 => 'January', 'February', 'March'); print_r($firstquarter); ?> ]]> </programlisting> &example.outputs; <screen> <![CDATA[ Array ( [1] => 'January' [2] => 'February' [3] => 'March' ) ]]> </screen> </example> <example> <title>å¡«å æ°ç»</title> <programlisting role="php"> <![CDATA[ <?php // fill an array with all items from a directory $handle = opendir('.'); while (false !== ($file = readdir($handle))) { $files[] = $file; } closedir($handle); ?> ]]> </programlisting> </example> <para> æ°ç»æ¯æåºçãä¹å¯ä»¥ä½¿ç¨ä¸åçæåºå½æ°æ¥æ¹å顺åºãæ´å¤ä¿¡æ¯åè§<link linkend="ref.array">æ°ç»å½æ°</link>ãå¯ä»¥ç¨ <function>count</function> 彿°æ¥æ°åºæ°ç»ä¸å ç´ ç个æ°ã </para> <example> <title>æ°ç»æåº</title> <programlisting role="php"> <![CDATA[ <?php sort($files); print_r($files); ?> ]]> </programlisting> </example> <para> å 为æ°ç»ä¸çå¼å¯ä»¥ä¸ºä»»æå¼ï¼ä¹å¯æ¯å¦ä¸ä¸ªæ°ç»ãè¿æ ·å¯ä»¥äº§çé彿å¤ç»´æ°ç»ã </para> <example> <title>éå½åå¤ç»´æ°ç»</title> <programlisting role="php"> <![CDATA[ <?php $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"; ?> ]]> </programlisting> </example> <para> <type>æ°ç»(Array)</type> çèµå¼æ»æ¯ä¼æ¶åå°å¼çæ·è´ãä½¿ç¨ <link linkend="language.operators">å¼ç¨æä½ç¬¦</link> éè¿å¼ç¨æ¥æ·è´æ°ç»ã </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php $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 ?> ]]> </programlisting> </informalexample> </sect2> </sect1> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t indent-tabs-mode:nil sgml-parent-document:nil sgml-default-dtd-file:"../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 -->