Sophie

Sophie

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

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

<HTML
><HEAD
><TITLE
>array_map</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="Array Functions"
HREF="ref.array.html"><LINK
REL="PREVIOUS"
TITLE="array_keys"
HREF="function.array-keys.html"><LINK
REL="NEXT"
TITLE="array_merge_recursive"
HREF="function.array-merge-recursive.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=ISO-8859-1"></HEAD
><BODY
CLASS="refentry"
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="function.array-keys.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.array-merge-recursive.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="function.array-map"
></A
>array_map</H1
><DIV
CLASS="refnamediv"
><A
NAME="AEN7627"
></A
><P
>    (PHP 4 &#62;= 4.0.6)</P
>array_map&nbsp;--&nbsp;
     Applies the callback to the elements of the given arrays
    </DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN7630"
></A
><H2
>Description</H2
>array <B
CLASS="methodname"
>array_map</B
> ( callback function, array arr1 [, array arr2...])<BR
></BR
><P
>&#13;     <B
CLASS="function"
>array_map()</B
> returns an array containing all
     the elements of <TT
CLASS="parameter"
><I
>arr1</I
></TT
> after applying the
     callback <TT
CLASS="parameter"
><I
>function</I
></TT
> to each one.  
     The number of parameters that the callback 
     <TT
CLASS="parameter"
><I
>function</I
></TT
> accepts 
     should match the number of arrays
     passed to the <B
CLASS="function"
>array_map()</B
>
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN7651"
></A
><P
><B
>Example 1. <B
CLASS="function"
>array_map()</B
> example</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
function cube($n) {
    return $n*$n*$n;
}

$a = array(1, 2, 3, 4, 5);
$b = array_map("cube", $a);
print_r($b);
?&#62;</PRE
></TD
></TR
></TABLE
><P
>&#13;       This makes <TT
CLASS="varname"
>$b</TT
> have:
       <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>Array
(
    [0] =&#62; 1
    [1] =&#62; 8
    [2] =&#62; 27
    [3] =&#62; 64
    [4] =&#62; 125
)</PRE
></TD
></TR
></TABLE
>
      </P
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN7659"
></A
><P
><B
>Example 2. <B
CLASS="function"
>array_map()</B
> - using more arrays</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
function show_Spanish($n, $m) {
    return "The number $n is called $m in Spanish";
}

function map_Spanish($n, $m) {
    return array ($n =&#62; $m);
}

$a = array(1, 2, 3, 4, 5);
$b = array("uno", "dos", "tres", "cuatro", "cinco");

$c = array_map("show_Spanish", $a, $b);
print_r($c);

$d = array_map("map_Spanish", $a , $b);
print_r($d);
?&#62;</PRE
></TD
></TR
></TABLE
><P
>&#13;       This results:
       <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>// printout of $c
Array
(
    [0] =&#62; The number 1 is called uno in Spanish
    [1] =&#62; The number 2 is called dos in Spanish
    [2] =&#62; The number 3 is called tres in Spanish
    [3] =&#62; The number 4 is called cuatro in Spanish
    [4] =&#62; The number 5 is called cinco in Spanish
)

// printout of $d
Array
(
    [0] =&#62; Array
        (
            [1] =&#62; uno
        )

    [1] =&#62; Array
        (
            [2] =&#62; dos
        )

    [2] =&#62; Array
        (
            [3] =&#62; tres
        )

    [3] =&#62; Array
        (
            [4] =&#62; cuatro
        )

    [4] =&#62; Array
        (
            [5] =&#62; cinco
        )

)</PRE
></TD
></TR
></TABLE
>
      </P
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     Usually when using two or more arrays, they should be of equal length
     because the callback function is applied in parallel to the corresponding
     elements.
     If the arrays are of unequal length, the shortest one will be extended
     with empty elements.
    </P
><P
>&#13;     An interesting use of this function is to construct an array of arrays,
     which can be easily performed by using <TT
CLASS="constant"
><B
>NULL</B
></TT
>
     as the name of the callback function
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN7669"
></A
><P
><B
>Example 3. Creating an array of arrays</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
$a = array(1, 2, 3, 4, 5);
$b = array("one", "two", "three", "four", "five");
$c = array("uno", "dos", "tres", "cuatro", "cinco");

$d = array_map(null, $a, $b, $c);
print_r($d);
?&#62;</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     The printout of the program above will be:
     <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>Array
(
    [0] =&#62; Array
        (
            [0] =&#62; 1
            [1] =&#62; one
            [2] =&#62; uno
        )

    [1] =&#62; Array
        (
            [0] =&#62; 2
            [1] =&#62; two
            [2] =&#62; dos
        )

    [2] =&#62; Array
        (
            [0] =&#62; 3
            [1] =&#62; three
            [2] =&#62; tres
        )

    [3] =&#62; Array
        (
            [0] =&#62; 4
            [1] =&#62; four
            [2] =&#62; cuatro
        )

    [4] =&#62; Array
        (
            [0] =&#62; 5
            [1] =&#62; five
            [2] =&#62; cinco
        )

)</PRE
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     See also <A
HREF="function.array-filter.html"
><B
CLASS="function"
>array_filter()</B
></A
>, 
     <A
HREF="function.array-reduce.html"
><B
CLASS="function"
>array_reduce()</B
></A
>, and
     <A
HREF="function.array-walk.html"
><B
CLASS="function"
>array_walk()</B
></A
>.
    </P
></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="function.array-keys.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="function.array-merge-recursive.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>array_keys</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="ref.array.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>array_merge_recursive</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>