Sophie

Sophie

distrib > Mandriva > 9.1 > ppc > media > main > by-pkgid > 0afeee9cca140e167a996902b9a677c5 > files > 3003

php-manual-en-4.3.0-2mdk.noarch.rpm

<HTML
><HEAD
><TITLE
>Arrays</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="PHP Manual"
HREF="index.html"><LINK
REL="UP"
TITLE="Types"
HREF="language.types.html"><LINK
REL="PREVIOUS"
TITLE="Strings"
HREF="language.types.string.html"><LINK
REL="NEXT"
TITLE="Objects"
HREF="language.types.object.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=ISO-8859-1"></HEAD
><BODY
CLASS="sect1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>PHP Manual</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="language.types.string.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 7. Types</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="language.types.object.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="language.types.array"
></A
>Arrays</H1
><P
>&#13;    An array in PHP is actually an ordered map. A map is a type that
    maps <SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>values</I
></SPAN
> to <SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>keys</I
></SPAN
>. 
    This type is optimized in several ways,
    so you can use it as a real array, or a list (vector), 
    hashtable (which is an implementation of a map), 
    dictionary, collection,
    stack, queue and probably more. Because you can have another
    PHP-array as a value, you can also quite easily simulate
    trees.
   </P
><P
>&#13;    Explanation of those structures is beyond the scope of this manual,
    but you'll find at least one example for each of those structures.
    For more information we refer you to external literature about
    this broad topic.
   </P
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.types.array.syntax"
></A
>Syntax</H2
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="language.types.array.syntax.array-func"
></A
>Specifying with <A
HREF="function.array.html"
><B
CLASS="function"
>array()</B
></A
></H3
><P
>&#13;      An <A
HREF="language.types.array.html"
><B
CLASS="type"
>array</B
></A
> can be created by the <A
HREF="function.array.html"
><B
CLASS="function"
>array()</B
></A
> 
      language-construct. It takes a certain number of comma-separated
      <TT
CLASS="literal"
><TT
CLASS="replaceable"
><I
>key</I
></TT
> =&#62; <TT
CLASS="replaceable"
><I
>value</I
></TT
></TT
>
      pairs.
     </P
><P
>&#13;      <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="synopsis"
>array( [<TT
CLASS="replaceable"
><I
>key</I
></TT
> =&#62;] <TT
CLASS="replaceable"
><I
>value</I
></TT
>
     , ...
     )
// <TT
CLASS="replaceable"
><I
>key</I
></TT
> is either <A
HREF="language.types.string.html"
><B
CLASS="type"
>string</B
></A
> or nonnegative <A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>
// <TT
CLASS="replaceable"
><I
>value</I
></TT
> can be anything</PRE
></TD
></TR
></TABLE
>
     </P
><P
>&#13;      <DIV
CLASS="informalexample"
><A
NAME="AEN3634"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
$arr = array("foo" =&#62; "bar", 12 =&#62; true);

echo $arr["foo"]; // bar
echo $arr[12];    // 1
?&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
     </P
><P
>&#13;      A <TT
CLASS="varname"
>key</TT
> is either an <A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
> 
      or a <A
HREF="language.types.string.html"
><B
CLASS="type"
>string</B
></A
>. If a key is the standard representation
      of an <A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>, it will be interpreted as such (i.e.
      <TT
CLASS="literal"
>"8"</TT
> will be interpreted as <TT
CLASS="literal"
>8</TT
>,
      while <TT
CLASS="literal"
>"08"</TT
> will be interpreted as
      <TT
CLASS="literal"
>"08"</TT
>). There are no different indexed and
      associative array types in PHP, there is only one array type,
      which can both contain integer and string indices.
     </P
><P
>&#13;      A value can be of any PHP type.
      <DIV
CLASS="informalexample"
><A
NAME="AEN3646"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
$arr = array("somearray" =&#62; array(6 =&#62; 5, 13 =&#62; 9, "a" =&#62; 42));

echo $arr["somearray"][6];    // 5
echo $arr["somearray"][13];   // 9
echo $arr["somearray"]["a"];  // 42
?&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
     </P
><P
>&#13;      If you omit a key, the maximum of the integer-indices is taken, and
      the new key will be that maximum + 1. As integers can be negative,
      this is also true for negative indices. Having e.g. the highest index
      being <TT
CLASS="literal"
>-6</TT
> will result in <TT
CLASS="literal"
>-5</TT
> being 
      the new key. If no integer-indices exist
      yet, the key will be <TT
CLASS="literal"
>0</TT
> (zero). If you specify a key
      that already has a value assigned to it, that value will be overwritten.
      <DIV
CLASS="informalexample"
><A
NAME="AEN3652"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
// This array is the same as ...
array(5 =&#62; 43, 32, 56, "b" =&#62; 12);

// ...this array
array(5 =&#62; 43, 6 =&#62; 32, 7 =&#62; 56, "b" =&#62; 12);
?&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
     </P
><P
>&#13;      Using <TT
CLASS="constant"
><B
>TRUE</B
></TT
> as a key will evalute to
      <A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
> <TT
CLASS="literal"
>1</TT
> as key. Using
      <TT
CLASS="constant"
><B
>FALSE</B
></TT
> as a key will evalute to <A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>
      <TT
CLASS="literal"
>0</TT
> as key. Using <TT
CLASS="literal"
>NULL</TT
> as a key
      will evaluate to an empty string. Using an emptry string as key will
      create (or overwrite) a key with an empty string and its value, it is
      not the same as using empty brackets.
     </P
><P
>&#13;      You cannot use arrays or objects as keys. Doing so will result in a
      warning: <TT
CLASS="literal"
>Illegal offset type</TT
>.
     </P
></DIV
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="language.types.array.syntax.modifying"
></A
>Creating/modifying with square-bracket syntax</H3
><P
>&#13;      You can also modify an existing array, by explicitly setting
      values in it.
     </P
><P
>&#13;      This is done by assigning values to the array while specifying the 
      key in brackets. You can also omit the key, add an empty pair
      of brackets ("<TT
CLASS="literal"
>[]</TT
>") to the variable-name in that case.
      <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="synopsis"
>$arr[<TT
CLASS="replaceable"
><I
>key</I
></TT
>] = <TT
CLASS="replaceable"
><I
>value</I
></TT
>;
$arr[] = <TT
CLASS="replaceable"
><I
>value</I
></TT
>;
// <TT
CLASS="replaceable"
><I
>key</I
></TT
> is either <A
HREF="language.types.string.html"
><B
CLASS="type"
>string</B
></A
> or nonnegative <A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>
// <TT
CLASS="replaceable"
><I
>value</I
></TT
> can be anything</PRE
></TD
></TR
></TABLE
>
      If <TT
CLASS="varname"
>$arr</TT
> doesn't exist yet, it will be created. 
      So this is also an alternative way to specify an array.
      To change a certain value, just assign a new value
      to an element specified with its key. If you want to
      remove a key/value pair, you need to <A
HREF="function.unset.html"
><B
CLASS="function"
>unset()</B
></A
> it. 
      <DIV
CLASS="informalexample"
><A
NAME="AEN3679"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
$arr = array(5 =&#62; 1, 12 =&#62; 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
?&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
     </P
></DIV
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.types.array.useful-funcs"
></A
>Useful functions</H2
><P
>&#13;     There are quite some useful function for working
     with arrays, see the <A
HREF="ref.array.html"
>array
     functions</A
> section.
    </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Note: </B
>
      The <A
HREF="function.unset.html"
><B
CLASS="function"
>unset()</B
></A
> function allows unsetting keys of an 
      array. Be aware that the array will NOT be reindexed. If you only
      use "usual integer indices" (starting from zero, increasing by one),
      you can achive the reindex effect by using <A
HREF="function.array-values.html"
><B
CLASS="function"
>array_values()</B
></A
>.
      <DIV
CLASS="informalexample"
><A
NAME="AEN3689"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
$a = array(1 =&#62; 'one', 2 =&#62; 'two', 3 =&#62; 'three');
unset($a[2]);
/* will produce an array that would have been defined as
   $a = array(1 =&#62; 'one', 3 =&#62; 'three');
   and NOT
   $a = array(1 =&#62; 'one', 2 =&#62;'three');
*/

$b = array_values($a);
// Now b is array(1 =&#62; 'one', 2 =&#62;'three')
?&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
     
     </P
></BLOCKQUOTE
></DIV
><P
>&#13;     The <A
HREF="control-structures.foreach.html"
>foreach</A
> 
     control structure exists specifically for arrays. It 
     provides an easy way to traverse an array.
    </P
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.types.array.donts"
></A
>Array do's and don'ts</H2
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="language.types.array.foo-bar"
></A
>Why is <TT
CLASS="literal"
>$foo[bar]</TT
> wrong?</H3
><P
>&#13;      You should always use quotes around an associative array index.
      For example, use $foo['bar'] and not $foo[bar]. But why is $foo[bar]
      wrong? You might have seen the following syntax in old scripts:
      <DIV
CLASS="informalexample"
><A
NAME="AEN3699"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
$foo[bar] = 'enemy';
echo $foo[bar];
// etc
?&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
      This is wrong, but it works. Then, why is it wrong? The reason is that
      this code has an undefined constant (bar) rather than a string ('bar' -
      notice the quotes), and PHP may in future define constants which,
      unfortunately for your code, have the same name.  It works, because the
      undefined constant gets converted to a string of the same name
      automatically for backward compatibility reasons. 
     </P
><P
>&#13;      More examples to demonstrate this fact:
      <DIV
CLASS="informalexample"
><A
NAME="AEN3702"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
// Let's show all errors
error_reporting(E_ALL);

$arr = array('fruit' =&#62; 'apple', 'veggie' =&#62; '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

// Let's define a constant to demonstrate what's going on.  We
// will assign value 'veggie' 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 error here
print "Hello $arr[fruit]";      // Hello apple

// With one exception, braces surrounding arrays within strings
// allows constants to be looked for
print "Hello {$arr[fruit]}";    // Hello carrot
print "Hello {$arr['fruit']}";  // Hello apple

// This will not work, results 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 autoglobals in strings as well
print "Hello $arr['fruit']";
print "Hello $_GET['foo']";

// Concatenation is another option
print "Hello " . $arr['fruit']; // Hello apple
?&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
     </P
><P
>&#13;      When you turn <A
HREF="function.error-reporting.html"
><B
CLASS="function"
>error_reporting()</B
></A
> up to show
      <TT
CLASS="constant"
><B
>E_NOTICE</B
></TT
> level errors (such as setting
      it to <TT
CLASS="constant"
><B
>E_ALL</B
></TT
>) then you will see these
      errors.  By default, <A
HREF="ref.errorfunc.html#ini.error-reporting"
>&#13;      error_reporting</A
> is turned down to not show them.
     </P
><P
>&#13;      As stated in the <A
HREF="language.types.array.html#language.types.array.syntax"
>syntax</A
> section, there must be an expression between the 
      square brackets ('<TT
CLASS="literal"
>[</TT
>' and '<TT
CLASS="literal"
>]</TT
>').
      That means that you can write things like this:
      <DIV
CLASS="informalexample"
><A
NAME="AEN3713"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
echo $arr[somefunc($bar)];
?&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
      This is an example of using a function return value
      as the array index. PHP also knows about constants,
      as you may have seen the <TT
CLASS="literal"
>E_*</TT
> ones
      before.      

      <DIV
CLASS="informalexample"
><A
NAME="AEN3716"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?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";
?&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
      Note that <TT
CLASS="literal"
>E_ERROR</TT
> is also a valid identifier, 
      just like <TT
CLASS="literal"
>bar</TT
> in the first example. But the last
      example is in fact the same as writing:
      <DIV
CLASS="informalexample"
><A
NAME="AEN3720"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?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";
?&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
      because <TT
CLASS="literal"
>E_ERROR</TT
> equals <TT
CLASS="literal"
>1</TT
>, etc.
     </P
><P
>&#13;      As we already explained in the above examples, 
      <TT
CLASS="literal"
>$foo[bar]</TT
> still works but is wrong.
      It works, because <TT
CLASS="literal"
>bar</TT
> is due to its syntax
      expected to be a constant expression. However, in this case no 
      constant with the name <TT
CLASS="literal"
>bar</TT
> exists. PHP now
      assumes that you meant <TT
CLASS="literal"
>bar</TT
> literally, 
      as the string <TT
CLASS="literal"
>"bar"</TT
>, but that you forgot
      to write the quotes.
     </P
><DIV
CLASS="sect4"
><H4
CLASS="sect4"
><A
NAME="AEN3730"
></A
>So why is it bad then?</H4
><P
>&#13;       At some point in the future, the PHP team might want to add another
       constant or keyword, or you may introduce another constant into your
       application, and then you get in trouble. For example,
       you already cannot use the words <TT
CLASS="literal"
>empty</TT
> and 
       <TT
CLASS="literal"
>default</TT
> this way, since they are special 
       <A
HREF="reserved.html"
>reserved keywords</A
>.
      </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Note: </B
>
        To reiterate, inside a double-quoted <A
HREF="language.types.string.html"
><B
CLASS="type"
>string</B
></A
>, it's
        valid to not surround array indexes with quotes so
        <TT
CLASS="literal"
>"$foo[bar]"</TT
> is valid.  See the above 
        examples for details on why as well as the section on
        <A
HREF="language.types.string.html#language.types.string.parsing"
>variable parsing 
        in strings</A
>.
       </P
></BLOCKQUOTE
></DIV
></DIV
></DIV
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.types.array.casting"
></A
>Converting to array</H2
><P
>&#13;     For any of the types: <A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>, <A
HREF="language.types.float.html"
><B
CLASS="type"
>float</B
></A
>,
     <A
HREF="language.types.string.html"
><B
CLASS="type"
>string</B
></A
>, <A
HREF="language.types.boolean.html"
><B
CLASS="type"
>boolean</B
></A
> and <A
HREF="language.types.resource.html"
><B
CLASS="type"
>resource</B
></A
>,
     if you convert a value to an <A
HREF="language.types.array.html"
><B
CLASS="type"
>array</B
></A
>, you get an array 
     with one element (with index 0), which is the scalar value you 
     started with.
    </P
><P
>&#13;     If you convert an <A
HREF="language.types.object.html"
><B
CLASS="type"
>object</B
></A
> to an array, you get the
     properties (member variables) of that object as the array's elements. 
     The keys are the member variable names.
    </P
><P
>&#13;     If you convert a <TT
CLASS="constant"
><B
>NULL</B
></TT
> value to an array, you get an empty array.
    </P
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.types.array.examples"
></A
>Examples</H2
><P
>&#13;     The array type in PHP is very versatile, so here will be some 
     examples to show you the full power of arrays.
    </P
><P
>&#13;      <DIV
CLASS="informalexample"
><A
NAME="AEN3758"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
// this
$a = array( 'color' =&#62; 'red',
            'taste' =&#62; 'sweet',
            'shape' =&#62; 'round',
            'name'  =&#62; '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 =&#62; 'a' , 1 =&#62; 'b' , 2 =&#62; 'c'),
// or simply array('a', 'b', 'c')
?&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
     </P
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN3760"
></A
><P
><B
>Example 7-4. Using array()</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
// Array as (property-)map
$map = array( 'version'    =&#62; 4,
              'OS'         =&#62; 'Linux',
              'lang'       =&#62; 'english',
              'short_tags' =&#62; true
            );
            
// strictly numerical keys
$array = array( 7,
                8,
                0,
                156,
                -10
              );
// this is the same as array(0 =&#62; 7, 1 =&#62; 8, ...)

$switching = array(         10, // key = 0
                    5    =&#62;  6,
                    3    =&#62;  7, 
                    'a'  =&#62;  4,
                            11, // key = 6 (maximum of integer-indices was 5)
                    '8'  =&#62;  2, // key = 8 (integer!)
                    '02' =&#62; 77, // key = '02'
                    0    =&#62; 12  // the value 10 will be overwritten by 12
                  );
                  
// empty array
$empty = array();         
?&#62;</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="language.types.array.examples.loop"
></A
><P
><B
>Example 7-5. Collection</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
$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?
*/
?&#62;</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><P
>&#13;     Note that it is currently not possible to change the values of the array
     directly in such a loop. 
      
     A workaround is the following: 
     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="language.types.array.examples.changeloop"
></A
><P
><B
>Example 7-6. Collection</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
foreach ($colors as $key =&#62; $color) {
    // won't work:
    //$color = strtoupper($color);
    
    // works:
    $colors[$key] = strtoupper($color);
}
print_r($colors);

/* output:
Array
(
    [0] =&#62; RED
    [1] =&#62; BLUE
    [2] =&#62; GREEN
    [3] =&#62; YELLOW
)
*/
?&#62;</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     This example creates a one-based array.
     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN3771"
></A
><P
><B
>Example 7-7. One-based index</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
$firstquarter  = array(1 =&#62; 'January', 'February', 'March');
print_r($firstquarter);

/* output:
Array 
(
    [1] =&#62; 'January'
    [2] =&#62; 'February'
    [3] =&#62; 'March'
)
*/</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN3774"
></A
><P
><B
>Example 7-8. Filling an array</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>// fill an array with all items from a directory
$handle = opendir('.');
while ($file = readdir($handle)) {
    $files[] = $file;
}
closedir($handle); 
?&#62;</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><P
>&#13;     Arrays are ordered. You can also change the order using various
     sorting-functions. See the <A
HREF="ref.array.html"
>array
     functions</A
> section for more information. You can count
     the number of items in an array using the
     <A
HREF="function.count.html"
><B
CLASS="function"
>count()</B
></A
> function.
    </P
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN3780"
></A
><P
><B
>Example 7-9. Sorting array</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
sort($files);
print_r($files);
?&#62;</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><P
>&#13;     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.
    </P
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN3784"
></A
><P
><B
>Example 7-10. Recursive and multi-dimensional arrays</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
$fruits = array ( "fruits"  =&#62; array ( "a" =&#62; "orange",
                                       "b" =&#62; "banana",
                                       "c" =&#62; "apple"
                                     ),
                  "numbers" =&#62; array ( 1,
                                       2,
                                       3,
                                       4,
                                       5,
                                       6,
                                     ),
                  "holes"   =&#62; array (      "first",
                                       5 =&#62; "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"; 
?&#62;</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><P
>&#13;     You should be aware, that array assignment always involves
     value copying. You need to use the reference operator to copy
     an array by reference.
     <DIV
CLASS="informalexample"
><A
NAME="AEN3788"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
$arr1 = array(2, 3);
$arr2 = $arr1;
$arr2[] = 4; // $arr2 is changed,
             // $arr1 is still array(2,3)
             
$arr3 = &#38;$arr1;
$arr3[] = 4; // now $arr1 and $arr3 are the same
?&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
    </P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="language.types.string.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="language.types.object.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Strings</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="language.types.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Objects</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>