cvs: phpdoc /pt_BR/functions array.xml bc.xml /pt_BR/language variables.xml

From: Date: Mon, 16 Oct 2000 21:09:30 +0000
Subject: cvs: phpdoc /pt_BR/functions array.xml bc.xml /pt_BR/language variables.xml
Groups: php.doc 
Request: Send a blank email to phpdoc+get-969332501@lists.php.net to get a copy of this message
eschmid Mon Oct 16 14:09:30 2000 EDT Added files: /phpdoc/pt_BR/language variables.xml /phpdoc/pt_BR/functions array.xml bc.xml Log: Next part of missing files. Index: phpdoc/pt_BR/language/variables.xml +++ phpdoc/pt_BR/language/variables.xml <chapter id="language.variables"> <title>Variables</title> <sect1 id="language.variables.basics"> <title>Basics</title> <simpara> Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive. </simpara> <para> Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*' </para> <note> <simpara> For our purposes here, a letter is a-z, A-Z, and the ASCII characters from 127 through 255 (0x7f-0xff). </simpara> </note> <para> <informalexample> <programlisting role="php"> $var = "Bob"; $Var = "Joe"; echo "$var, $Var"; // outputs "Bob, Joe" $4site = 'not yet'; // invalid; starts with a number $_4site = 'not yet'; // valid; starts with an underscore $täyte = 'mansikka'; // valid; 'ä' is ASCII 228. </programlisting> </informalexample> </para> <para> In PHP 3, variables are always assigned by value. That is to say, when you assign an expression to a variable, the entire value of the original expression is copied into the destination variable. This means, for instance, that after assigning one variable's value to another, changing one of those variables will have no effect on the other. For more information on this kind of assignment, see <link linkend="language.expressions">Expressions</link>. </para> <para> PHP 4 offers another way to assign values to variables: <emphasis>assign by reference</emphasis>. This means that the new variable simply references (in other words, "becomes an alias for" or "points to") the original variable. Changes to the new variable affect the original, and vice versa. This also means that no copying is performed; thus, the assignment happens more quickly. However, any speedup will likely be noticed only in tight loops or when assigning large arrays or objects. </para> <para> To assign by reference, simply prepend an ampersand (&amp;) to the beginning of the variable which is being assigned (the source variable). For instance, the following code snippet outputs 'My name is Bob' twice: <informalexample> <programlisting role="php"> &lt;?php $foo = 'Bob'; // Assign the value 'Bob' to $foo $bar = &amp;$foo; // Reference $foo via $bar. $bar = "My name is $bar"; // Alter $bar... echo $foo; // $foo is altered too. echo $bar; ?&gt; </programlisting> </informalexample> </para> <para> One important thing to note is that only named variables may be assigned by reference. <informalexample> <programlisting role="php"> &lt;?php $foo = 25; $bar = &amp;$foo; // This is a valid assignment. $bar = &amp;(24 * 7); // Invalid; references an unnamed expression. function test() { return 25; } $bar = &amp;test(); // Invalid. ?&gt; </programlisting> </informalexample> </para> </sect1> <sect1 id="language.variables.predefined"> <title>Predefined variables</title> <simpara> PHP provides a large number of predefined variables to any script which it runs. Many of these variables, however, cannot be fully documented as they are dependent upon which server is running, the version and setup of the server, and other factors. Some of these variables will not be available when PHP is run on the command-line. </simpara> <simpara> Despite these factors, here is a list of predefined variables available under a stock installation of PHP 3 running as a module under a stock installation of <ulink url="&url.apache;">Apache</ulink> 1.3.6. </simpara> <simpara> For a list of all predefined variables (and lots of other useful information), please see (and use) <function>phpinfo</function>. </simpara> <note> <simpara> This list is neither exhaustive nor intended to be. It is simply a guideline as to what sorts of predefined variables you can expect to have access to in your script. </simpara> </note> <sect2 id="language.variables.predefined.apache"> <title>Apache variables</title> <simpara> These variables are created by the <ulink url="&url.apache;">Apache</ulink> webserver. If you are running another webserver, there is no guarantee that it will provide the same variables; it may omit some, or provide others not listed here. That said, a large number of these variables are accounted for in the <ulink url="&url.cgispec;">CGI 1.1 specification</ulink>, so you should be able to expect those. </simpara> <simpara> Note that few, if any, of these will be available (or indeed have any meaning) if running PHP on the command line. </simpara> <para> <variablelist> <varlistentry> <term>GATEWAY_INTERFACE</term> <listitem> <simpara> What revision of the CGI specification the server is using; i.e. 'CGI/1.1'. </simpara> </listitem> </varlistentry> <varlistentry> <term>SERVER_NAME</term> <listitem> <simpara> The name of the server host under which the current script is executing. If the script is running on a virtual host, this will be the value defined for that virtual host. </simpara> </listitem> </varlistentry> <varlistentry> <term>SERVER_SOFTWARE</term> <listitem> <simpara> Server identification string, given in the headers when responding to requests. </simpara> </listitem> </varlistentry> <varlistentry> <term>SERVER_PROTOCOL</term> <listitem> <simpara> Name and revision of the information protocol via which the page was requested; i.e. 'HTTP/1.0'; </simpara> </listitem> </varlistentry> <varlistentry> <term>REQUEST_METHOD</term> <listitem> <simpara> Which request method was used to access the page; i.e. 'GET', 'HEAD', 'POST', 'PUT'. </simpara> </listitem> </varlistentry> <varlistentry> <term>QUERY_STRING</term> <listitem> <simpara> The query string, if any, via which the page was accessed. </simpara> </listitem> </varlistentry> <varlistentry> <term>DOCUMENT_ROOT</term> <listitem> <simpara> The document root directory under which the current script is executing, as defined in the server's configuration file. </simpara> </listitem> </varlistentry> <varlistentry> <term>HTTP_ACCEPT</term> <listitem> <simpara> Contents of the <literal>Accept:</literal> header from the current request, if there is one. </simpara> </listitem> </varlistentry> <varlistentry> <term>HTTP_ACCEPT_CHARSET</term> <listitem> <simpara> Contents of the <literal>Accept-Charset:</literal> header from the current request, if there is one. Example: 'iso-8859-1,*,utf-8'. </simpara> </listitem> </varlistentry> <varlistentry> <term>HTTP_ENCODING</term> <listitem> <simpara> Contents of the <literal>Accept-Encoding:</literal> header from the current request, if there is one. Example: 'gzip'. </simpara> </listitem> </varlistentry> <varlistentry> <term>HTTP_ACCEPT_LANGUAGE</term> <listitem> <simpara> Contents of the <literal>Accept-Language:</literal> header from the current request, if there is one. Example: 'en'. </simpara> </listitem> </varlistentry> <varlistentry> <term>HTTP_CONNECTION</term> <listitem> <simpara> Contents of the <literal>Connection:</literal> header from the current request, if there is one. Example: 'Keep-Alive'. </simpara> </listitem> </varlistentry> <varlistentry> <term>HTTP_HOST</term> <listitem> <simpara> Contents of the <literal>Host:</literal> header from the current request, if there is one. </simpara> </listitem> </varlistentry> <varlistentry> <term>HTTP_REFERER</term> <listitem> <simpara> The address of the page (if any) which referred the browser to the current page. This is set by the user's browser; not all browsers will set this. </simpara> </listitem> </varlistentry> <varlistentry> <term>HTTP_USER_AGENT</term> <listitem> <simpara> Contents of the <literal>User_Agent:</literal> header from the current request, if there is one. This is a string denoting the browser software being used to view the current page; i.e. <computeroutput>Mozilla/4.5 [en] (X11; U; Linux 2.2.9 i586)</computeroutput>. Among other things, you can use this value with <function>get_browser</function> to tailor your page's functionality to the capabilities of the user's browser. </simpara> </listitem> </varlistentry> <varlistentry> <term>REMOTE_ADDR</term> <listitem> <simpara> The IP address from which the user is viewing the current page. </simpara> </listitem> </varlistentry> <varlistentry> <term>REMOTE_PORT</term> <listitem> <simpara> The port being used on the user's machine to communicate with the web server. </simpara> </listitem> </varlistentry> <varlistentry> <term>SCRIPT_FILENAME</term> <listitem> <simpara> The absolute pathname of the currently executing script. </simpara> </listitem> </varlistentry> <varlistentry> <term>SERVER_ADMIN</term> <listitem> <simpara> The value given to the SERVER_ADMIN (for Apache) directive in the web server configuration file. If the script is running on a virtual host, this will be the value defined for that virtual host. </simpara> </listitem> </varlistentry> <varlistentry> <term>SERVER_PORT</term> <listitem> <simpara> The port on the server machine being used by the web server for communication. For default setups, this will be '80'; using SSL, for instance, will change this to whatever your defined secure HTTP port is. </simpara> </listitem> </varlistentry> <varlistentry> <term>SERVER_SIGNATURE</term> <listitem> <simpara> String containing the server version and virtual host name which are added to server-generated pages, if enabled. </simpara> </listitem> </varlistentry> <varlistentry> <term>PATH_TRANSLATED</term> <listitem> <simpara> Filesystem- (not document root-) based path to the current script, after the server has done any virtual-to-real mapping. </simpara> </listitem> </varlistentry> <varlistentry> <term>SCRIPT_NAME</term> <listitem> <simpara> Contains the current script's path. This is useful for pages which need to point to themselves. </simpara> </listitem> </varlistentry> <varlistentry> <term>REQUEST_URI</term> <listitem> <simpara> The URI which was given in order to access this page; for instance, '/index.html'. </simpara> </listitem> </varlistentry> </variablelist> </para> </sect2> <sect2 id="language.variables.predefined.environment"> <title>Environment variables</title> <simpara> These variables are imported into PHP's global namespace from the environment under which the PHP parser is running. Many are provided by the shell under which PHP is running and different systems are likely running different kinds of shells, a definitive list is impossible. Please see your shell's documentation for a list of defined environment variables. </simpara> <simpara> Other environment variables include the CGI variables, placed there regardless of whether PHP is running as a server module or CGI processor. </simpara> </sect2> <sect2 id="language.variables.predefined.php"> <title>PHP variables</title> <simpara> These variables are created by PHP itself. The <varname>$HTTP_*_VARS</varname> variables are available only if the <link linkend="ini.track-vars">track_vars</link> configuration is turned on. </simpara> <note> <para> As of PHP 4.0.3, <link linkend="ini.track-vars">track_vars</link> is always turned on, regardless of the configuration file setting. </para> </note> <para> If the <link linkend="ini.register-globals">register_globals</link> directive is set, then these variables will also be made available in the global scope of the script; i.e., separate from the <varname>$HTTP_*_VARS</varname> arrays. This feature should be used with care, and turned off if possible; while the <varname>$HTTP_*_VARS</varname> variables are safe, the bare global equivalents can be overwritten by user input, with possibly malicious intent. If you cannot turn off <link linkend="ini.register-globals">register_globals</link>, you must take whatever steps are necessary to ensure that the data you are using is safe. </para> <para> <variablelist> <varlistentry> <term>argv</term> <listitem> <simpara> Array of arguments passed to the script. When the script is run on the command line, this gives C-style access to the command line parameters. When called via the GET method, this will contain the query string. </simpara> </listitem> </varlistentry> <varlistentry> <term>argc</term> <listitem> <simpara> Contains the number of command line parameters passed to the script (if run on the command line). </simpara> </listitem> </varlistentry> <varlistentry> <term>PHP_SELF</term> <listitem> <simpara> The filename of the currently executing script, relative to the document root. If PHP is running as a command-line processor, this variable is not available. </simpara> </listitem> </varlistentry> <varlistentry> <term>HTTP_COOKIE_VARS</term> <listitem> <simpara> An associative array of variables passed to the current script via HTTP cookies. </simpara> </listitem> </varlistentry> <varlistentry> <term>HTTP_GET_VARS</term> <listitem> <simpara> An associative array of variables passed to the current script via the HTTP GET method. </simpara> </listitem> </varlistentry> <varlistentry> <term>HTTP_POST_VARS</term> <listitem> <simpara> An associative array of variables passed to the current script via the HTTP POST method. </simpara> </listitem> </varlistentry> <varlistentry> <term>HTTP_POST_FILES</term> <listitem> <simpara> An associative array of variables containing information about files uploaded via the HTTP POST method. See <link linkend="features.file-upload.post-method">POST method uploads</link> for information on the contents of <varname>$HTTP_POST_FILES</varname>. </simpara> <para> <varname>$HTTP_POST_FILES</varname> is available only in PHP 4.0.0 and later. </para> </listitem> </varlistentry> <varlistentry> <term>HTTP_ENV_VARS</term> <listitem> <simpara> An associative array of variables passed to the current script via the parent environment. </simpara> </listitem> </varlistentry> <varlistentry> <term>HTTP_SERVER_VARS</term> <listitem> <simpara> An associative array of variables passed to the current script from the HTTP server. These variables are analogous to the Apache variables described above. </simpara> </listitem> </varlistentry> </variablelist> </para> </sect2> </sect1> <sect1 id="language.variables.scope"> <title>Variable scope</title> <simpara> The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope. This single scope spans included and required files as well. For example: </simpara> <informalexample> <programlisting role="php"> $a = 1; include "b.inc"; </programlisting> </informalexample> <simpara> Here the <varname>$a</varname> variable will be available within the included <filename>b.inc</filename> script. However, within user-defined functions a local function scope is introduced. Any variable used inside a function is by default limited to the local function scope. For example: </simpara> <informalexample> <programlisting role="php"> $a = 1; /* global scope */ Function Test () { echo $a; /* reference to local scope variable */ } Test (); </programlisting> </informalexample> <simpara> This script will not produce any output because the echo statement refers to a local version of the <varname>$a</varname> variable, and it has not been assigned a value within this scope. You may notice that this is a little bit different from the C language in that global variables in C are automatically available to functions unless specifically overridden by a local definition. This can cause some problems in that people may inadvertently change a global variable. In PHP global variables must be declared global inside a function if they are going to be used in that function. An example: </simpara> <informalexample> <programlisting role="php"> $a = 1; $b = 2; Function Sum () { global $a, $b; $b = $a + $b; } Sum (); echo $b; </programlisting> </informalexample> <simpara> The above script will output &quot;3&quot;. By declaring <varname>$a</varname> and <varname>$b</varname> global within the function, all references to either variable will refer to the global version. There is no limit to the number of global variables that can be manipulated by a function. </simpara> <simpara> A second way to access variables from the global scope is to use the special PHP-defined <varname>$GLOBALS</varname> array. The previous example can be rewritten as: </simpara> <informalexample> <programlisting role="php"> $a = 1; $b = 2; Function Sum () { $GLOBALS["b"] = $GLOBALS["a"] + $GLOBALS["b"]; } Sum (); echo $b; </programlisting> </informalexample> <simpara> The <varname>$GLOBALS</varname> array is an associative array with the name of the global variable being the key and the contents of that variable being the value of the array element. </simpara> <simpara> Another important feature of variable scoping is the <emphasis>static</emphasis> variable. A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope. Consider the following example: </simpara> <informalexample> <programlisting role="php"> Function Test () { $a = 0; echo $a; $a++; } </programlisting> </informalexample> <simpara> This function is quite useless since every time it is called it sets <varname>$a</varname> to <literal>0</literal> and prints &quot;0&quot;. The <varname>$a</varname>++ which increments the variable serves no purpose since as soon as the function exits the <varname>$a</varname> variable disappears. To make a useful counting function which will not lose track of the current count, the <varname>$a</varname> variable is declared static: </simpara> <informalexample> <programlisting role="php"> Function Test () { static $a = 0; echo $a; $a++; } </programlisting> </informalexample> <simpara> Now, every time the Test() function is called it will print the value of <varname>$a</varname> and increment it. </simpara> <simpara> Static variables also provide one way to deal with recursive functions. A recursive function is one which calls itself. Care must be taken when writing a recursive function because it is possible to make it recurse indefinitely. You must make sure you have an adequate way of terminating the recursion. The following simple function recursively counts to 10, using the static variable <varname>$count</varname> to know when to stop: </simpara> <informalexample> <programlisting role="php"> Function Test () { static $count = 0; $count++; echo $count; if ($count &lt; 10) { Test (); } $count--; } </programlisting> </informalexample> </sect1> <sect1 id="language.variables.variable"> <title>Variable variables</title> <simpara> Sometimes it is convenient to be able to have variable variable names. That is, a variable name which can be set and used dynamically. A normal variable is set with a statement such as: </simpara> <informalexample> <programlisting role="php"> $a = "hello"; </programlisting> </informalexample> <simpara> A variable variable takes the value of a variable and treats that as the name of a variable. In the above example, <emphasis>hello</emphasis>, can be used as the name of a variable by using two dollar signs. i.e. </simpara> <informalexample> <programlisting role="php"> $$a = "world"; </programlisting> </informalexample> <simpara> At this point two variables have been defined and stored in the PHP symbol tree: <varname>$a</varname> with contents "hello" and <varname>$hello</varname> with contents "world". Therefore, this statement: </simpara> <informalexample> <programlisting role="php"> echo "$a ${$a}"; </programlisting> </informalexample> <simpara> produces the exact same output as: </simpara> <informalexample> <programlisting> echo "$a $hello"; </programlisting> </informalexample> <simpara> i.e. they both produce: <computeroutput>hello world</computeroutput>. </simpara> <simpara> In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write <varname>$$a[1]</varname> then the parser needs to know if you meant to use <varname>$a[1]</varname> as a variable, or if you wanted <varname>$$a</varname> as the variable and then the [1] index from that variable. The syntax for resolving this ambiguity is: <varname>${$a[1]}</varname> for the first case and <varname>${$a}[1]</varname> for the second. </simpara> </sect1> <sect1 id="language.variables.external"> <title>Variables from outside PHP</title> <sect2 id="language.variables.external.form"> <title>HTML Forms (GET and POST)</title> <simpara> When a form is submitted to a PHP script, any variables from that form will be automatically made available to the script by PHP. If the <link linkend="ini.track-vars">track_vars</link> configuration option is turned on, then these variables will be located in the associative arrays <varname>$HTTP_POST_VARS</varname>, <varname>$HTTP_GET_VARS</varname>, and/or <varname>$HTTP_POST_FILES</varname>, according to the source of the variable in question. </simpara> <para> For more information on these variables, please read <link linkend="language.variables.predefined">Predefined variables</link>. </para> <para> <example> <title>Simple form variable</title> <programlisting role="php"> &lt;form action="foo.php" method="post"> Name: &lt;input type="text" name="username">&lt;br> &lt;input type="submit"> &lt;/form> </programlisting> </example> </para> <para> When the above form is submitted, the value from the text input will be available in <varname>$HTTP_POST_VARS['username']</varname>. If the <link linkend="ini.register-globals">register_globals</link> configuration directive is turned on, then the variable will also be available as <varname>$username</varname> in the global scope. </para> <simpara> PHP also understands arrays in the context of form variables. You may, for example, group related variables together, or use this feature to retrieve values from a multiple select input: </simpara> <para> <example> <title>More complex form variables</title> <programlisting role="php"> &lt;form action="array.php" method="post"> Name: &lt;input type="text" name="personal[name]">&lt;br> Email: &lt;input type="text" name="personal[email]">&lt;br> Beer: &lt;br> &lt;select multiple name="beer[]"> &lt;option value="warthog">Warthog &lt;option value="guinness">Guinness &lt;option value="stuttgarter">Stuttgarter Schwabenbr&auml;u &lt;/select> &lt;input type="submit"> &lt;/form> </programlisting> </example> </para> <para> In PHP 3, the array form variable usage is limited to single-dimensional arrays. In PHP 4, no such restriction applies. </para> <sect3 id="language.variables.external.form.submit"> <title>IMAGE SUBMIT variable names</title> <simpara> When submitting a form, it is possible to use an image instead of the standard submit button with a tag like:</simpara> <informalexample> <programlisting role="php"> &lt;input type=image src="image.gif" name="sub"> </programlisting> </informalexample> <simpara> When the user clicks somewhere on the image, the accompanying form will be transmitted to the server with two additional variables, sub_x and sub_y. These contain the coordinates of the user click within the image. The experienced may note that the actual variable names sent by the browser contains a period rather than an underscore, but PHP converts the period to an underscore automatically. </simpara> </sect3> </sect2> <sect2 id="language.variables.external.cookies"> <title>HTTP Cookies</title> <simpara> PHP transparently supports HTTP cookies as defined by <ulink url="&spec.cookies;">Netscape's Spec</ulink>. Cookies are a mechanism for storing data in the remote browser and thus tracking or identifying return users. You can set cookies using the <function>SetCookie</function> function. Cookies are part of the HTTP header, so the SetCookie function must be called before any output is sent to the browser. This is the same restriction as for the <function>Header</function> function. Any cookies sent to you from the client will automatically be turned into a PHP variable just like GET and POST method data.</simpara> <simpara> If you wish to assign multiple values to a single cookie, just add <emphasis>[]</emphasis> to the cookie name. For example: </simpara> <informalexample> <programlisting role="php"> SetCookie ("MyCookie[]", "Testing", time()+3600); </programlisting> </informalexample> <simpara> Note that a cookie will replace a previous cookie by the same name in your browser unless the path or domain is different. So, for a shopping cart application you may want to keep a counter and pass this along. i.e. </simpara> <example> <title>SetCookie Example</title> <programlisting role="php"> $Count++; SetCookie ("Count", $Count, time()+3600); SetCookie ("Cart[$Count]", $item, time()+3600); </programlisting> </example> </sect2> <sect2 id="language.variables.external.environment"> <title>Environment variables</title> <para> PHP automatically makes environment variables available as normal PHP variables. <informalexample> <programlisting role="php"> echo $HOME; /* Shows the HOME environment variable, if set. */ </programlisting> </informalexample> </para> <para> Since information coming in via GET, POST and Cookie mechanisms also automatically create PHP variables, it is sometimes best to explicitly read a variable from the environment in order to make sure that you are getting the right version. The <function>getenv</function> function can be used for this. You can also set an environment variable with the <function>putenv</function> function. </para> </sect2> <sect2 id="language.variables.external.dot-in-names"> <title>Dots in incoming variable names</title> <para> Typically, PHP does not alter the names of variables when they are passed into a script. However, it should be noted that the dot (period, full stop) is not a valid character in a PHP variable name. For the reason, look at it: <programlisting role="php"> $varname.ext; /* invalid variable name */ </programlisting> Now, what the parser sees is a variable named <varname>$varname</varname>, followed by the string concatenation operator, followed by the barestring (i.e. unquoted string which doesn't match any known key or reserved words) 'ext'. Obviously, this doesn't have the intended result. </para> <para> For this reason, it is important to note that PHP will automatically replace any dots in incoming variable names with underscores. </para> </sect2> <sect2 id="language.variables.determining-type-of"> <title>Determining variable types</title> <para> Because PHP determines the types of variables and converts them (generally) as needed, it is not always obvious what type a given variable is at any one time. PHP includes several functions which find out what type a variable is. They are <function>gettype</function>, <function>is_long</function>, <function>is_double</function>, <function>is_string</function>, <function>is_array</function>, and <function>is_object</function>. </para> </sect2> </sect1> </chapter> <!-- 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 sgml-parent-document:nil sgml-default-dtd-file:"../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: --> Index: phpdoc/pt_BR/functions/array.xml +++ phpdoc/pt_BR/functions/array.xml <reference id="ref.array"> <title>Array Functions</title> <titleabbrev>Arrays</titleabbrev> <partintro> <simpara> These functions allow you to interact with and manipulate arrays in various ways. Arrays are essential for storing, managing, and operating on sets of variables. </simpara> <simpara> Simple and multi-dimensional arrays are supported, and may be either user created or created by another function. There are specific database handling functions for populating arrays from database queries, and several functions return arrays. </simpara> <para> See also <function>is_array</function>, <function>explode</function>, <function>implode</function>, <function>split</function> and <function>join</function>. </para> </partintro> <refentry id="function.array"> <refnamediv> <refname>array</refname> <refpurpose> Create an array </refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>array <function>array</function></funcdef> <paramdef>mixed <parameter><optional>...</optional></parameter> </paramdef> </funcprototype> </funcsynopsis> <para> Returns an array of the parameters. The parameters can be given an index with the <literal>=&gt;</literal> operator. </para> <para> <note> <para> <function>Array</function> is a language construct used to represent literal arrays, and not a regular function. </para> </note> </para> <para> The following example demonstrates how to create a two-dimensional array, how to specify keys for associative arrays, and how to skip-and-continue numeric indices in normal arrays. <example> <title><function>Array</function> example</title> <programlisting role="php"> $fruits = array ( "fruits" =&gt; array ("a"=&gt;"orange", "b"=&gt;"banana", "c"=&gt;"apple"), "numbers" =&gt; array (1, 2, 3, 4, 5, 6), "holes" =&gt; array ("first", 5 =&gt; "second", "third") ); </programlisting> </example> </para> <para> See also: <function>list</function>. </para> </refsect1> </refentry> <refentry id="function.array-count-values"> <refnamediv> <refname>array_count_values</refname> <refpurpose>Counts all the values of an array</refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>array <function>array_count_values</function></funcdef> <paramdef>array <parameter>input</parameter></paramdef> </funcprototype> </funcsynopsis> <para> <function>Array_count_values</function> returns an array using the values of the <parameter>input</parameter> array as keys and their frequency in <parameter>input</parameter> as values. </para> <para> <example> <title><function>Array_count_values</function> example</title> <programlisting role="php"> $array = array (1, "hello", 1, "world", "hello"); array_count_values ($array); // returns array (1=&gt;2, "hello"=&gt;2, "world"=&gt;1) </programlisting> </example> </para> </refsect1> </refentry> <refentry id="function.array-diff"> <refnamediv> <refname>array_diff</refname> <refpurpose>Computes the difference of arrays</refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>array <function>array_diff</function></funcdef> <paramdef>array <parameter>array1</parameter></paramdef> <paramdef>array <parameter>array2</parameter></paramdef> <paramdef>array <parameter><optional> ...</optional></parameter> </paramdef> </funcprototype> </funcsynopsis> <para> <function>Array_diff</function> returns an array containing all the values of <parameter>array1</parameter> that are not present in any of the other arguments. Note that keys are preserved. </para> <para> <example> <title><function>Array_diff</function> example</title> <programlisting role="php"> $array1 = array ("a" =&gt; "green", "red", "blue"); $array2 = array ("b" =&gt; "green", "yellow", "red"); $result = array_diff ($array1, $array2); </programlisting> </example> </para> <para> This makes <varname>$result</varname> have <literal>array ("blue");</literal> </para> <para> See also <function>array_intersect</function>. </para> </refsect1> </refentry> <refentry id="function.array-flip"> <refnamediv> <refname>array_flip</refname> <refpurpose>Flip all the values of an array</refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>array <function>array_flip</function></funcdef> <paramdef>array <parameter>trans</parameter></paramdef> </funcprototype> </funcsynopsis> <para> <function>Array_flip</function> returns an array in flip order. </para> <para> <example> <title><function>Array_flip</function> example</title> <programlisting role="php"> $trans = array_flip ($trans); $original = strtr ($str, $trans); </programlisting> </example> </para> </refsect1> </refentry> <refentry id="function.array-intersect"> <refnamediv> <refname>array_intersect</refname> <refpurpose>Computes the intersection of arrays</refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>array <function>array_intersect</function></funcdef> <paramdef>array <parameter>array1</parameter></paramdef> <paramdef>array <parameter>array2</parameter></paramdef> <paramdef>array <parameter><optional> ...</optional></parameter> </paramdef> </funcprototype> </funcsynopsis> <para> <function>Array_intersect</function> returns an array containing all the values of <parameter>array1</parameter> that are present in all the arguments. Note that keys are preserved. </para> <para> <example> <title><function>Array_intersect</function> example</title> <programlisting role="php"> $array1 = array ("a" =&gt; "green", "red", "blue"); $array2 = array ("b" =&gt; "green", "yellow", "red"); $result = array_intersect ($array1, $array2); </programlisting> </example> </para> <para> This makes <varname>$result</varname> have <literal>array ("a" =&gt; "green", "red");</literal> </para> <para> See also <function>array_diff</function>. </para> </refsect1> </refentry> <refentry id="function.array-keys"> <refnamediv> <refname>array_keys</refname> <refpurpose>Return all the keys of an array</refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>array <function>array_keys</function></funcdef> <paramdef>array <parameter>input</parameter></paramdef> <paramdef>mixed <parameter> <optional>search_value</optional> </parameter> </paramdef> </funcprototype> </funcsynopsis> <para> <function>Array_keys</function> returns the keys, numeric and string, from the <parameter>input</parameter> array. </para> <para> If the optional <parameter>search_value</parameter> is specified, then only the keys for that value are returned. Otherwise, all the keys from the <parameter>input</parameter> are returned. </para> <para> <example> <title><function>Array_keys</function> example</title> <programlisting role="php"> $array = array (0 =&gt; 100, "color" =&gt; "red"); array_keys ($array); // returns array (0, "color") $array = array ("blue", "red", "green", "blue", "blue"); array_keys ($array, "blue"); // returns array (0, 3, 4) </programlisting> </example> </para> <note> <para> This function was added to PHP 4, below is an implementation for those still using PHP 3. <example> <title> Implementation of <function>array_keys</function> for PHP 3 users </title> <programlisting role="php"> function array_keys ($arr, $term="") { $t = array(); while (list($k,$v) = each($arr)) { if ($term &amp;&amp; $v != $term) continue; $t[] = $k; } return $t; } </programlisting> </example> </para> </note> <para> See also <function>array_values</function>. </para> </refsect1> </refentry> <refentry id="function.array-merge"> <refnamediv> <refname>array_merge</refname> <refpurpose>Merge two or more arrays</refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>array <function>array_merge</function></funcdef> <paramdef>array <parameter>array1</parameter></paramdef> <paramdef>array <parameter>array2</parameter></paramdef> <paramdef>array <parameter><optional>...</optional></parameter> </paramdef> </funcprototype> </funcsynopsis> <para> <function>Array_merge</function> merges the elements of two or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array. </para> <para> If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended. </para> <para> <example> <title><function>array_merge</function> example</title> <programlisting role="php"> $array1 = array ("color" =&gt; "red", 2, 4); $array2 = array ("a", "b", "color" =&gt; "green", "shape" =&gt; "trapezoid", 4); array_merge ($array1, $array2); </programlisting> </example> </para> <para> Resulting array will be <literal>array("color" =&gt; "green", 2, 4, "a", "b", "shape" =&gt; "trapezoid", 4)</literal>. </para> <para> See also <function>array_merge_recursive</function>. </para> </refsect1> </refentry> <refentry id="function.array-merge-recursive"> <refnamediv> <refname>array_merge_recursive</refname> <refpurpose>Merge two or more arrays recursively</refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>array <function>array_merge_recursive</function></funcdef> <paramdef>array <parameter>array1</parameter></paramdef> <paramdef>array <parameter>array2</parameter></paramdef> <paramdef>array <parameter><optional>...</optional></parameter> </paramdef> </funcprototype> </funcsynopsis> <para> <function>Array_merge_recursive</function> merges the elements of two or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array. </para> <para> If the input arrays have the same string keys, then the values for these keys are merged together into an array, and this is done recursively, so that if one of the values is an array itself, the function will merge it with a corresponding entry in another array too. If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended. </para> <para> <example> <title><function>Array_merge_recursive</function> example</title> <programlisting role="php"> $ar1 = array ("color" =&gt; array ("favorite" =&gt; "red"), 5); $ar2 = array (10, "color" =&gt; array ("favorite" =&gt; "green", "blue")); $result = array_merge_recursive ($ar1, $ar2); </programlisting> </example> </para> <para> Resulting array will be <literal>array ("color" =&gt; array ("favorite" =&gt; array ("red", "green"), "blue"), 5, 10)</literal>. </para> <para> See also <function>array_merge</function>. </para> </refsect1> </refentry> <refentry id="function.array-multisort"> <refnamediv> <refname>array_multisort</refname> <refpurpose>Sort multiple or multi-dimensional arrays</refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>bool <function>array_multisort</function></funcdef> <paramdef>array <parameter>ar1</parameter></paramdef> <paramdef>mixed <parameter><optional>arg</optional></parameter> </paramdef> <paramdef>mixed <parameter><optional>...</optional></parameter> </paramdef> <paramdef>array <parameter><optional>...</optional></parameter> </paramdef> </funcprototype> </funcsynopsis> <para> <function>Array_multisort</function> can be used to sort several arrays at once or a multi-dimensional array according by one of more dimensions. It maintains key association when sorting. </para> <para> The input arrays are treated as columns of a table to be sorted by rows - this resembles the functionality of SQL ORDER BY clause. The first array is the primary one to sort by. The rows (values) in that array that compare the same are sorted by the next input array, and so on. </para> <para> The argument structure of this function is a bit unusual, but flexible. The very first argument has to be an array. Subsequently, each argument can be either an array or a sorting flag from the following lists. </para> <para> Sorting order flags: <itemizedlist> <listitem> <simpara>SORT_ASC - sort in ascending order</simpara> </listitem> <listitem> <simpara>SORT_DESC - sort in descending order</simpara> </listitem> </itemizedlist> </para> <para> Sorting type flags: <itemizedlist> <listitem> <simpara>SORT_REGULAR - compare items normally</simpara> </listitem> <listitem> <simpara>SORT_NUMERIC - compare items numerically</simpara> </listitem> <listitem> <simpara>SORT_STRING - compare items as strings</simpara> </listitem> </itemizedlist> </para> <para> No two sorting flags of the same type can be specified after each array. The sortings flags specified after an array argument apply only to that array - they are reset to default SORT_ASC and SORT_REGULAR after before each new array argument. </para> <para> Returns true on success, false on failure. </para> <para> <example> <title>Sorting multiple arrays</title> <programlisting role="php"> $ar1 = array ("10", 100, 100, "a"); $ar2 = array (1, 3, "2", 1); array_multisort ($ar1, $ar2); </programlisting> </example> </para> <para> In this example, after sorting, the first array will contain 10, "a", 100, 100. The second array will contain 1, 1, 2, "3". The entries in the second array corresponding to the identical entries in the first array (100 and 100) were sorted as well. </para> <para> <example> <title>Sorting multi-dimensional array</title> <programlisting role="php"> $ar = array (array ("10", 100, 100, "a"), array (1, 3, "2", 1)); array_multisort ($ar[0], SORT_ASC, SORT_STRING, $ar[1], SORT_NUMERIC, SORT_DESC); </programlisting> </example> </para> <para> In this example, after sorting, the first array will contain 10, 100, 100, "a" (it was sorted as strings in ascending order), and the second one will contain 1, 3, "2", 1 (sorted as numbers, in descending order). </para> </refsect1> </refentry> <refentry id="function.array-pad"> <refnamediv> <refname>array_pad</refname> <refpurpose> Pad array to the specified length with a value </refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>array <function>array_pad</function></funcdef> <paramdef>array <parameter>input</parameter></paramdef> <paramdef>int <parameter>pad_size</parameter></paramdef> <paramdef>mixed <parameter>pad_value</parameter></paramdef> </funcprototype> </funcsynopsis> <para> <function>Array_pad</function> returns a copy of the <parameter>input</parameter> padded to size specified by <parameter>pad_size</parameter> with value <parameter>pad_value</parameter>. If <parameter>pad_size</parameter> is positive then the array is padded on the right, if it's negative then on the left. If the absolute value of <parameter>pad_size</parameter> is less than or equal to the length of the <parameter>input</parameter> then no padding takes place. </para> <para> <example> <title><function>Array_pad</function> example</title> <programlisting role="php"> $input = array (12, 10, 9); $result = array_pad ($input, 5, 0); // result is array (12, 10, 9, 0, 0) $result = array_pad ($input, -7, -1); // result is array (-1, -1, -1, -1, 12, 10, 9) $result = array_pad ($input, 2, "noop"); // not padded </programlisting> </example> </para> </refsect1> </refentry> <refentry id="function.array-pop"> <refnamediv> <refname>array_pop</refname> <refpurpose>Pop the element off the end of array</refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>mixed <function>array_pop</function></funcdef> <paramdef>array <parameter>array</parameter></paramdef> </funcprototype> </funcsynopsis> <para> <function>Array_pop</function> pops and returns the last value of the <parameter>array</parameter>, shortening the <parameter>array</parameter> by one element. </para> <para> <example> <title><function>Array_pop</function> example</title> <programlisting role="php"> $stack = array ("orange", "apple", "raspberry"); $fruit = array_pop ($stack); </programlisting> </example> </para> <para> After this, <varname>$stack</varname> has only 2 elements: "orange" and "apple", and <varname>$fruit</varname> has "raspberry". </para> <para> See also <function>array_push</function>, <function>array_shift</function>, and <function>array_unshift</function>. </para> </refsect1> </refentry> <refentry id="function.array-push"> <refnamediv> <refname>array_push</refname> <refpurpose> Push one or more elements onto the end of array </refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>int <function>array_push</function></funcdef> <paramdef>array <parameter>array</parameter></paramdef> <paramdef>mixed <parameter>var</parameter></paramdef> <paramdef>mixed <parameter><optional>...</optional></parameter> </paramdef> </funcprototype> </funcsynopsis> <para> <function>Array_push</function> treats <parameter>array</parameter> as a stack, and pushes the passed variables onto the end of <parameter>array</parameter>. The length of <parameter>array</parameter> increases by the number of variables pushed. Has the same effect as: <programlisting role="php"> $array[] = $var; </programlisting> repeated for each <parameter>var</parameter>. </para> <para> Returns the new number of elements in the array. </para> <para> <example> <title><function>Array_push</function> example</title> <programlisting role="php"> $stack = array (1, 2); array_push ($stack, "+", 3); </programlisting> </example> </para> <para> This example would result in <varname>$stack</varname> having 4 elements: 1, 2, "+", and 3. </para> <para> See also: <function>array_pop</function>, <function>array_shift</function>, and <function>array_unshift</function>. </para> </refsect1> </refentry> <refentry id="function.array-rand"> <refnamediv> <refname>array_rand</refname> <refpurpose> Pick one or more random entries out of an array </refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>mixed <function>array_rand</function></funcdef> <paramdef>array <parameter>input</parameter></paramdef> <paramdef>int <parameter><optional>num_req</optional></parameter> </paramdef> </funcprototype> </funcsynopsis> <para> <function>Array_rand</function> is rather useful when you want to pick one or more random entries out of an array. It takes an <parameter>input</parameter> array and an optional argument <parameter>num_req</parameter> which specifies how many entries you want to pick - if not specified, it defaults to 1. </para> <para> If you are picking only one entry, <function>array_rand</function> returns the key for a random entry. Otherwise, it returns an array of keys for the random entries. This is done so that you can pick random keys as well as values out of the array. </para> <para> Don't forget to call <function>srand</function> to seed the random number generator. </para> <para> <example> <title><function>Array_rand</function> example</title> <programlisting role="php"> srand ((double) microtime() * 10000000); $input = array ("Neo", "Morpheus", "Trinity", "Cypher", "Tank"); $rand_keys = array_rand ($input, 2); print $input[$rand_keys[0]]."\n"; print $input[$rand_keys[1]]."\n"; </programlisting> </example> </para> </refsect1> </refentry> <refentry id="function.array-reverse"> <refnamediv> <refname>array_reverse</refname> <refpurpose> Return an array with elements in reverse order </refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>array <function>array_reverse</function></funcdef> <paramdef>array <parameter>array</parameter></paramdef> </funcprototype> </funcsynopsis> <para> <function>Array_reverse</function> takes input <parameter>array</parameter> and returns a new array with the order of the elements reversed. </para> <para> <example> <title><function>Array_reverse</function> example</title> <programlisting role="php"> $input = array ("php", 4.0, array ("green", "red")); $result = array_reverse ($input); </programlisting> </example> </para> <para> This makes <varname>$result</varname> have <literal>array (array ("green", "red"), 4.0, "php")</literal>. </para> </refsect1> </refentry> <refentry id="function.array-shift"> <refnamediv> <refname>array_shift</refname> <refpurpose> Pop an element off the beginning of array </refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>mixed <function>array_shift</function></funcdef> <paramdef>array <parameter>array</parameter></paramdef> </funcprototype> </funcsynopsis> <para> <function>Array_shift</function> shifts the first value of the <parameter>array</parameter> off and returns it, shortening the <parameter>array</parameter> by one element and moving everything down. </para> <para> <example> <title><function>Array_shift</function> example</title> <programlisting role="php"> $args = array ("-v", "-f"); $opt = array_shift ($args); </programlisting> </example> </para> <para> This would result in <varname>$args</varname> having one element "-f" left, and <varname>$opt</varname> being "-v". </para> <para> See also <function>array_unshift</function>, <function>array_push</function>, and <function>array_pop</function>. </para> </refsect1> </refentry> <refentry id="function.array-slice"> <refnamediv> <refname>array_slice</refname> <refpurpose>Extract a slice of the array</refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>array <function>array_slice</function></funcdef> <paramdef>array <parameter>array</parameter></paramdef> <paramdef>int <parameter>offset</parameter></paramdef> <paramdef>int <parameter> <optional>length</optional> </parameter> </paramdef> </funcprototype> </funcsynopsis> <para> <function>Array_slice</function> returns a sequence of elements from the <parameter>array</parameter> specified by the <parameter>offset</parameter> and <parameter>length</parameter> parameters. </para> <para> If <parameter>offset</parameter> is positive, the sequence will start at that offset in the <parameter>array</parameter>. If <parameter>offset</parameter> is negative, the sequence will start that far from the end of the <parameter>array</parameter>. </para> <para> If <parameter>length</parameter> is given and is positive, then the sequence will have that many elements in it. If <parameter>length</parameter> is given and is negative then the sequence will stop that many elements from the end of the array. If it is omitted, then the sequence will have everything from <parameter>offset</parameter> up until the end of the <parameter>array</parameter>. </para> <para> <example> <title><function>Array_slice</function> examples</title> <programlisting role="php"> $input = array ("a", "b", "c", "d", "e"); $output = array_slice ($input, 2); // returns "c", "d", and "e" $output = array_slice ($input, 2, -1); // returns "c", "d" $output = array_slice ($input, -2, 1); // returns "d" $output = array_slice ($input, 0, 3); // returns "a", "b", and "c" </programlisting> </example> </para> <para> See also <function>array_splice</function>. </para> </refsect1> </refentry> <refentry id="function.array-splice"> <refnamediv> <refname>array_splice</refname> <refpurpose> Remove a portion of the array and replace it with something else </refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>array <function>array_splice</function></funcdef> <paramdef>array <parameter>input</parameter></paramdef> <paramdef>int <parameter>offset</parameter></paramdef> <paramdef>int <parameter><optional>length</optional></parameter> </paramdef> <paramdef>array <parameter> <optional>replacement</optional> </parameter> </paramdef> </funcprototype> </funcsynopsis> <para> <function>Array_splice</function> removes the elements designated by <parameter>offset</parameter> and <parameter>length</parameter> from the <parameter>input</parameter> array, and replaces them with the elements of the <parameter>replacement</parameter> array, if supplied. </para> <para> If <parameter>offset</parameter> is positive then the start of removed portion is at that offset from the beginning of the <parameter>input</parameter> array. If <parameter>offset</parameter> is negative then it starts that far from the end of the <parameter>input</parameter> array. </para> <para> If <parameter>length</parameter> is omitted, removes everything from <parameter>offset</parameter> to the end of the array. If <parameter>length</parameter> is specified and is positive, then that many elements will be removed. If <parameter>length</parameter> is specified and is negative then the end of the removed portion will be that many elements from the end of the array. Tip: to remove everything from <parameter>offset</parameter> to the end of the array when <parameter>replacement</parameter> is also specified, use <literal>count($input)</literal> for <parameter>length</parameter>. </para> <para> If <parameter>replacement</parameter> array is specified, then the removed elements are replaced with elements from this array. If <parameter>offset</parameter> and <parameter>length</parameter> are such that nothing is removed, then the elements from the <parameter>replacement</parameter> array are inserted in the place specified by the <parameter>offset</parameter>. Tip: if the replacement is just one element it is not necessary to put <literal>array()</literal> around it, unless the element is an array itself. </para> <para> The following equivalences hold: <programlisting> array_push ($input, $x, $y) array_splice ($input, count ($input), 0, array ($x, $y)) array_pop ($input) array_splice ($input, -1) array_shift ($input) array_splice ($input, 0, 1) array_unshift ($input, $x, $y) array_splice ($input, 0, 0, array ($x, $y)) $a[$x] = $y array_splice ($input, $x, 1, $y) </programlisting> </para> <para> Returns the array consisting of removed elements. </para> <para> <example> <title><function>Array_splice</function> examples</title> <programlisting role="php"> $input = array ("red", "green", "blue", "yellow"); array_splice ($input, 2); // $input is now array ("red", "green") array_splice ($input, 1, -1); // $input is now array ("red", "yellow") array_splice ($input, 1, count($input), "orange"); // $input is now array ("red", "orange") array_splice ($input, -1, 1, array("black", "maroon")); // $input is now array ("red", "green", // "blue", "black", "maroon") </programlisting> </example> </para> <para> See also <function>array_slice</function>. </para> </refsect1> </refentry> <refentry id="function.array-unique"> <refnamediv> <refname>array_unique</refname> <refpurpose>Removes duplicate values from an array</refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>array <function>array_unique</function></funcdef> <paramdef>array <parameter>array</parameter></paramdef> </funcprototype> </funcsynopsis> <para> <function>Array_unique</function> takes input <parameter>array</parameter> and returns a new array without duplicate values. Note that keys are preserved. </para> <para> <example> <title><function>Array_unique</function> example</title> <programlisting role="php"> $input = array ("a" =&gt; "green", "red", "b" =&gt; "green", "blue", "red"); $result = array_unique ($input); </programlisting> </example> </para> <para> This makes <varname>$result</varname> have <literal>array ("a" =&gt; "green", "red", "blue");</literal>. </para> </refsect1> </refentry> <refentry id="function.array-unshift"> <refnamediv> <refname>array_unshift</refname> <refpurpose> Push one or more elements onto the beginning of array </refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>int <function>array_unshift</function></funcdef> <paramdef>array <parameter>array</parameter></paramdef> <paramdef>mixed <parameter>var</parameter></paramdef> <paramdef>mixed <parameter> <optional>...</optional> </parameter> </paramdef> </funcprototype> </funcsynopsis> <para> <function>Array_unshift</function> prepends passed elements to the front of the <parameter>array</parameter>. Note that the list of elements is prepended as a whole, so that the prepended elements stay in the same order. </para> <para> Returns the new number of elements in the <parameter>array</parameter>. </para> <para> <example> <title><function>Array_unshift</function> example</title> <programlisting role="php"> $queue = array ("p1", "p3"); array_unshift ($queue, "p4", "p5", "p6"); </programlisting> </example> </para> <para> This would result in <varname>$queue</varname> having 5 elements: "p4", "p5", "p6", "p1", and "p3". </para> <para> See also <function>array_shift</function>, <function>array_push</function>, and <function>array_pop</function>. </para> </refsect1> </refentry> <refentry id="function.array-values"> <refnamediv> <refname>array_values</refname> <refpurpose>Return all the values of an array</refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>array <function>array_values</function></funcdef> <paramdef>array <parameter>input</parameter></paramdef> </funcprototype> </funcsynopsis> <para> <function>Array_values</function> returns all the values from the <parameter>input</parameter> array. </para> <para> <example> <title><function>Array_values</function> example</title> <programlisting role="php"> $array = array ("size" =&gt; "XL", "color" =&gt; "gold"); array_values ($array); // returns array ("XL", "gold") </programlisting> </example> </para> <note> <para> This function was added to PHP 4, below is an implementation for those still using PHP 3. <example> <title> Implementation of <function>array_values</function> for PHP 3 users </title> <programlisting role="php"> function array_values ($arr) { $t = array(); while (list($k, $v) = each ($arr)) { $t[] = $v; return $t; } } </programlisting> </example> </para> </note> </refsect1> </refentry> <refentry id="function.array-walk"> <refnamediv> <refname>array_walk</refname> <refpurpose> Apply a user function to every member of an array </refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>int <function>array_walk</function></funcdef> <paramdef>array <parameter>arr</parameter></paramdef> <paramdef>string <parameter>func</parameter></paramdef> <paramdef>mixed <parameter>userdata</parameter></paramdef> </funcprototype> </funcsynopsis> <simpara> Applies the function named by <parameter>func</parameter> to each element of <parameter>arr</parameter>. <parameter>func</parameter> will be passed array value as the first parameter and array key as the second parameter. If <parameter>userdata</parameter> is supplied, it will be passed as the third parameter to the user function. </simpara> <simpara> If <parameter>func</parameter> requires more than two or three arguments, depending on <parameter>userdata</parameter>, a warning will be generated each time <function>array_walk</function> calls <parameter>func</parameter>. These warnings may be suppressed by prepending the '@' sign to the <function>array_walk</function> call, or by using <function>error_reporting</function>. </simpara> <note> <para> If <parameter>func</parameter> needs to be working with the actual values of the array, specify that the first parameter of <parameter>func</parameter> should be passed by reference. Then any changes made to those elements will be made in the array itself. </para> </note> <note> <para> Passing the key and userdata to <parameter>func</parameter> was added in 4.0. </para> <para> In PHP 4 <function>reset</function> needs to be called as necessary since <function>array_walk</function> does not reset the array by default. </para> </note> <para> <example> <title><function>Array_walk</function> example</title> <programlisting role="php"> $fruits = array ("d"=&gt;"lemon", "a"=&gt;"orange", "b"=&gt;"banana", "c"=&gt;"apple"); function test_alter (&amp;$item1, $key, $prefix) { $item1 = "$prefix: $item1"; } function test_print ($item2, $key) { echo "$key. $item2&lt;br&gt;\n"; } array_walk ($fruits, 'test_print'); reset ($fruits); array_walk ($fruits, 'test_alter', 'fruit'); reset ($fruits); array_walk ($fruits, 'test_print'); </programlisting> </example> </para> <simpara> See also <function>each</function> and <function>list</function>. </simpara> </refsect1> </refentry> <refentry id="function.arsort"> <refnamediv> <refname>arsort</refname> <refpurpose> Sort an array in reverse order and maintain index association </refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>void <function>arsort</function></funcdef> <paramdef>array <parameter>array</parameter></paramdef> <paramdef>int <parameter><optional>sort_flags</optional></parameter> </paramdef> </funcprototype> </funcsynopsis> <para> This function sorts an array such that array indices maintain their correlation with the array elements they are associated with. This is used mainly when sorting associative arrays where the actual element order is significant. <example> <title><function>Arsort</function> example</title> <programlisting role="php"> $fruits = array ("d"=&gt;"lemon", "a"=&gt;"orange", "b"=&gt;"banana", "c"=&gt;"apple"); arsort ($fruits); reset ($fruits); while (list ($key, $val) = each ($fruits)) { echo "$key = $val\n"; } </programlisting> </example> </para> <para> This example would display: </para> <para> <informalexample> <programlisting> fruits[a] = orange fruits[d] = lemon fruits[b] = banana fruits[c] = apple </programlisting> </informalexample> </para> <para> The fruits have been sorted in reverse alphabetical order, and the index associated with each element has been maintained. </para> <para> You may modify the behavior of the sort using the optional parameter <parameter>sort_flags</parameter>, for details see <function>sort</function>. </para> <para> See also: <function>asort</function>, <function>rsort</function>, <function>ksort</function>, and <function>sort</function>. </para> </refsect1> </refentry> <refentry id="function.asort"> <refnamediv> <refname>asort</refname> <refpurpose>Sort an array and maintain index association</refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>void <function>asort</function></funcdef> <paramdef>array <parameter>array</parameter></paramdef> <paramdef>int <parameter><optional>sort_flags</optional></parameter></paramdef> </funcprototype> </funcsynopsis> <para> This function sorts an array such that array indices maintain their correlation with the array elements they are associated with. This is used mainly when sorting associative arrays where the actual element order is significant. <example> <title><function>Asort</function> example</title> <programlisting role="php"> $fruits = array ("d"=&gt;"lemon", "a"=&gt;"orange", "b"=&gt;"banana", "c"=&gt;"apple"); asort ($fruits); reset ($fruits); while (list ($key, $val) = each ($fruits)) { echo "$key = $val\n"; } </programlisting> </example> </para> <para> This example would display: </para> <para> <informalexample> <programlisting> fruits[c] = apple fruits[b] = banana fruits[d] = lemon fruits[a] = orange </programlisting> </informalexample> </para> <para> The fruits have been sorted in alphabetical order, and the index associated with each element has been maintained. </para> <para> You may modify the behavior of the sort using the optional parameter <parameter>sort_flags</parameter>, for details see <function>sort</function>. </para> <para> See also <function>arsort</function>, <function>rsort</function>, <function>ksort</function>, and <function>sort</function>. </para> </refsect1> </refentry> <refentry id="function.compact"> <refnamediv> <refname>compact</refname> <refpurpose> Create array containing variables and their values </refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>array <function>compact</function></funcdef> <paramdef>mixed <parameter>varname</parameter></paramdef> <paramdef>mixed <parameter><optional>...</optional></parameter> </paramdef> </funcprototype> </funcsynopsis> <para> <function>Compact</function> takes a variable number of parameters. Each parameter can be either a string containing the name of the variable, or an array of variable names. The array can contain other arrays of variable names inside it; <function>compact</function> handles it recursively. </para> <para> For each of these, <function>compact</function> looks for a variable with that name in the current symbol table and adds it to the output array such that the variable name becomes the key and the contents of the variable become the value for that key. In short, it does the opposite of <function>extract</function>. It returns the output array with all the variables added to it. </para> <para> <example> <title><function>Compact</function> example</title> <programlisting role="php"> $city = "San Francisco"; $state = "CA"; $event = "SIGGRAPH"; $location_vars = array ("city", "state"); $result = compact ("event", $location_vars); </programlisting> <para> After this, <varname>$result</varname> will be <literal>array ("event" =&gt; "SIGGRAPH", "city" =&gt; "San Francisco", "state" =&gt; "CA")</literal>. </para> </example> </para> <para> See also <function>extract</function>. </para> </refsect1> </refentry> <refentry id="function.count"> <refnamediv> <refname>count</refname> <refpurpose>Count elements in a variable</refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>int <function>count</function></funcdef> <paramdef>mixed <parameter>var</parameter></paramdef> </funcprototype> </funcsynopsis> <para> Returns the number of elements in <parameter>var</parameter>, which is typically an array (since anything else will have one element). </para> <para> Returns 1 if the variable is not an array. </para> <para> Returns 0 if the variable is not set. <warning> <para> <function>Count</function> may return 0 for a variable that isn't set, but it may also return 0 for a variable that has been initialized with an empty array. Use <function>isset</function> to test if a variable is set. </para> </warning> </para> <para> See also: <function>sizeof</function>, <function>isset</function>, and <function>is_array</function>. </para> </refsect1> </refentry> <refentry id="function.current"> <refnamediv> <refname>current</refname> <refpurpose>Return the current element in an array</refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>mixed <function>current</function></funcdef> <paramdef>array <parameter>array</parameter></paramdef> </funcprototype> </funcsynopsis> <para> Every array has an internal pointer to its "current" element, which is initialized to the first element inserted into the array. </para> <para> The <function>current</function> function simply returns the array element that's currently being pointed by the internal pointer. It does not move the pointer in any way. If the internal pointer points beyond the end of the elements list, <function>current</function> returns false. <warning> <para> If the array contains empty elements (0 or "", the empty string) then this function will return false for these elements as well. This makes it impossible to determine if you are really at the end of the list in such an array using <function>current</function>. To properly traverse an array that may contain empty elements, use the <function>each</function> function. </para> </warning> </para> <para> See also: <function>end</function>, <function>next</function>, <function>prev</function>, and <function>reset</function>. </para> </refsect1> </refentry> <refentry id="function.each"> <refnamediv> <refname>each</refname> <refpurpose> Return the next key and value pair from an array </refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>array <function>each</function></funcdef> <paramdef>array <parameter>array</parameter></paramdef> </funcprototype> </funcsynopsis> <para> Returns the current key and value pair from the array <parameter>array</parameter> and advances the array cursor. This pair is returned in a four-element array, with the keys <emphasis>0</emphasis>, <emphasis>1</emphasis>, <emphasis>key</emphasis>, and <emphasis>value</emphasis>. Elements <emphasis>0</emphasis> and <emphasis>key</emphasis> contain the key name of the array element, and <emphasis>1</emphasis> and <emphasis>value</emphasis> contain the data. </para> <para> If the internal pointer for the array points past the end of the array contents, <function>each</function> returns false. </para> <para> <example> <title><function>Each</function> examples</title> <programlisting role="php"> $foo = array ("bob", "fred", "jussi", "jouni", "egon", "marliese"); $bar = each ($foo); </programlisting> <para> <varname>$bar</varname> now contains the following key/value pairs: <itemizedlist spacing="compact"> <listitem><simpara>0 =&gt; 0</simpara></listitem> <listitem><simpara>1 =&gt; 'bob'</simpara></listitem> <listitem><simpara>key =&gt; 0</simpara></listitem> <listitem><simpara>value =&gt; 'bob'</simpara></listitem> </itemizedlist> <programlisting role="php"> $foo = array ("Robert" =&gt; "Bob", "Seppo" =&gt; "Sepi"); $bar = each ($foo); </programlisting> </para> <para> <varname>$bar</varname> now contains the following key/value pairs: <itemizedlist spacing="compact"> <listitem><simpara>0 =&gt; 'Robert'</simpara></listitem> <listitem><simpara>1 =&gt; 'Bob'</simpara></listitem> <listitem><simpara>key =&gt; 'Robert'</simpara></listitem> <listitem><simpara>value =&gt; 'Bob'</simpara></listitem> </itemizedlist> </para> </example> </para> <para> <function>Each</function> is typically used in conjunction with <function>list</function> to traverse an array; for instance, <varname>$HTTP_POST_VARS</varname>: <example> <title> Traversing <varname>$HTTP_POST_VARS</varname> with <function>each</function> </title> <programlisting role="php"> echo "Values submitted via POST method:&lt;br&gt;"; reset ($HTTP_POST_VARS); while (list ($key, $val) = each ($HTTP_POST_VARS)) { echo "$key =&gt; $val&lt;br&gt;"; } </programlisting> </example> </para> <para> After <function>each</function> has executed, the array cursor will be left on the next element of the array, or on the last element if it hits the end of the array. </para> <para> See also <function>key</function>, <function>list</function>, <function>current</function>, <function>reset</function>, <function>next</function>, and <function>prev</function>. </para> </refsect1> </refentry> <refentry id="function.end"> <refnamediv> <refname>end</refname> <refpurpose> Set the internal pointer of an array to its last element </refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>mixed <function>end</function></funcdef> <paramdef>array <parameter>array</parameter></paramdef> </funcprototype> </funcsynopsis> <para> <function>End</function> advances <parameter>array</parameter>'s internal pointer to the last element, and returns that element. </para> <para> See also: <function>current</function>, <function>each</function>, <function>end</function>, <function>next</function>, and <function>reset</function>. </para> </refsect1> </refentry> <refentry id="function.extract"> <refnamediv> <refname>extract</refname> <refpurpose> Import variables into the symbol table from an array </refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>void <function>extract</function></funcdef> <paramdef>array <parameter>var_array</parameter></paramdef> <paramdef>int <parameter><optional>extract_type</optional></parameter> </paramdef> <paramdef>string <parameter><optional>prefix</optional></parameter> </paramdef> </funcprototype> </funcsynopsis> <para> This function is used to import variables from an array into the current symbol table. It takes associative array <parameter>var_array</parameter> and treats keys as variable names and values as variable values. For each key/value pair it will create a variable in the current symbol table, subject to <parameter>extract_type</parameter> and <parameter>prefix</parameter> parameters. </para> <para> <function>Extract</function> checks for colissions with existing variables. The way collisions are treated is determined by <parameter>extract_type</parameter>. It can be one of the following values: <variablelist> <varlistentry> <term>EXTR_OVERWRITE</term> <listitem> <simpara> If there is a collision, overwrite the existing variable. </simpara> </listitem> </varlistentry> <varlistentry> <term>EXTR_SKIP</term> <listitem> <simpara> If there is a collision, don't overwrite the existing variable. </simpara> </listitem> </varlistentry> <varlistentry> <term>EXTR_PREFIX_SAME</term> <listitem> <simpara>If there is a collision, prefix the new variable with <parameter>prefix</parameter>. </simpara> </listitem> </varlistentry> <varlistentry> <term>EXTR_PREFIX_ALL</term> <listitem> <simpara> Prefix all variables with <parameter>prefix</parameter>. </simpara> </listitem> </varlistentry> </variablelist> </para> <para> If <parameter>extract_type</parameter> is not specified, it is assumed to be EXTR_OVERWRITE. </para> <para> Note that <parameter>prefix</parameter> is only required if <parameter>extract_type</parameter> is EXTR_PREFIX_SAME or EXTR_PREFIX_ALL. </para> <para> <function>Extract</function> checks each key to see if it constitues a valid variable name, and if it does only then does it proceed to import it. </para> <para> A possible use for extract is to import into symbol table variables contained in an associative array returned by <function>wddx_deserialize</function>. </para> <para> <example> <title><function>Extract</function> example</title> <programlisting role="php"> &lt;php? /* Suppose that $var_array is an array returned from wddx_deserialize */ $size = "large"; $var_array = array ("color" =&gt; "blue", "size" =&gt; "medium", "shape" =&gt; "sphere"); extract ($var_array, EXTR_PREFIX_SAME, "wddx"); print "$color, $size, $shape, $wddx_size\n"; ?> </programlisting> </example> </para> <para> The above example will produce: <programlisting> blue, large, sphere, medium </programlisting> </para> <para> The <varname>$size</varname> wasn't overwritten, becaus we specified EXTR_PREFIX_SAME, which resulted in <varname>$wddx_size</varname> being created. If EXTR_SKIP was specified, then $wddx_size wouldn't even have been created. EXTR_OVERWRITE would have cause <varname>$size</varname> to have value "medium", and EXTR_PREFIX_ALL would result in new variables being named <varname>$wddx_color</varname>, <varname>$wddx_size</varname>, and <varname>$wddx_shape</varname>. </para> </refsect1> </refentry> <refentry id="function.in-array"> <refnamediv> <refname>in_array</refname> <refpurpose>Return true if a value exists in an array</refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>bool in_array</funcdef> <paramdef>mixed <parameter>needle</parameter></paramdef> <paramdef>array <parameter>haystack</parameter></paramdef> </funcprototype> </funcsynopsis> <para> Searches <parameter>haystack</parameter> for <parameter>needle</parameter> and returns true if it is found in the array, false otherwise. </para> <para> <example> <title><function>In_array</function> example</title> <programlisting role="php"> $os = array ("Mac", "NT", "Irix", "Linux"); if (in_array ("Irix", $os)) print "Got Irix"; </programlisting> </example> </para> </refsect1> </refentry> <refentry id="function.key"> <refnamediv> <refname>key</refname> <refpurpose>Fetch a key from an associative array</refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>mixed <function>key</function></funcdef> <paramdef>array <parameter>array</parameter></paramdef> </funcprototype> </funcsynopsis> <para> <function>Key</function> returns the index element of the current array position. </para> <para> See also <function>current</function> and <function>next</function>. </para> </refsect1> </refentry> <refentry id="function.krsort"> <refnamediv> <refname>krsort</refname> <refpurpose>Sort an array by key in reverse order</refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>int <function>krsort</function></funcdef> <paramdef>array <parameter>array</parameter></paramdef> <paramdef>int <parameter><optional>sort_flags</optional></parameter> </paramdef> </funcprototype> </funcsynopsis> <para> Sorts an array by key in reverse order, maintaining key to data correlations. This is useful mainly for associative arrays. <example> <title><function>Krsort</function> example</title> <programlisting role="php"> $fruits = array ("d"=&gt;"lemon", "a"=&gt;"orange", "b"=&gt;"banana", "c"=&gt;"apple"); krsort ($fruits); reset ($fruits); while (list ($key, $val) = each ($fruits)) { echo "$key -&gt; $val\n"; } </programlisting> </example> </para> <para> This example would display: </para> <para> <informalexample> <programlisting> fruits[d] = lemon fruits[c] = apple fruits[b] = banana fruits[a] = orange </programlisting> </informalexample> </para> <para> You may modify the behavior of the sort using the optional parameter <parameter>sort_flags</parameter>, for details see <function>sort</function>. </para> <simpara> See also <function>asort</function>, <function>arsort</function>, <function>ksort</function> <function>sort</function>, <function>natsort</function>and <function>rsort</function>. </simpara> </refsect1> </refentry> <refentry id="function.ksort"> <refnamediv> <refname>ksort</refname> <refpurpose>Sort an array by key</refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>int <function>ksort</function></funcdef> <paramdef>array <parameter>array</parameter></paramdef> <paramdef>int <parameter><optional>sort_flags</optional></parameter> </paramdef> </funcprototype> </funcsynopsis> <para> Sorts an array by key, maintaining key to data correlations. This is useful mainly for associative arrays. <example> <title><function>Ksort</function> example</title> <programlisting role="php"> $fruits = array ("d"=&gt;"lemon", "a"=&gt;"orange", "b"=&gt;"banana", "c"=&gt;"apple"); ksort ($fruits); reset ($fruits); while (list ($key, $val) = each ($fruits)) { echo "$key -&gt; $val\n"; } </programlisting> </example> </para> <para> This example would display: </para> <para> <informalexample> <programlisting> fruits[a] = orange fruits[b] = banana fruits[c] = apple fruits[d] = lemon </programlisting> </informalexample> </para> <para> You may modify the behavior of the sort using the optional parameter <parameter>sort_flags</parameter>, for details see <function>sort</function>. </para> <simpara> See also <function>asort</function>, <function>arsort</function>, <function>sort</function>, <function>natsort</function>, and <function>rsort</function>. </simpara> </refsect1> </refentry> <refentry id="function.list"> <refnamediv> <refname>list</refname> <refpurpose> Assign variables as if they were an array </refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>void <function>list</function></funcdef> <varargs/> </funcprototype> </funcsynopsis> <para> Like <function>array</function>, this is not really a function, but a language construct. <function>list</function> is used to assign a list of variables in one operation. <example> <title><function>List</function> example</title> <programlisting role="php"> &lt;table> &lt;tr> &lt;th>Employee name&lt;/th> &lt;th>Salary&lt;/th> &lt;/tr> &lt;?php $result = mysql ($conn, "SELECT id, name, salary FROM employees"); while (list ($id, $name, $salary) = mysql_fetch_row ($result)) { print (" &lt;tr>\n". " &lt;td>&lt;a href=\"info.php3?id=$id\">$name&lt;/a>&lt;/td>\n". " &lt;td>$salary&lt;/td>\n". " &lt;/tr>\n"); } ?> &lt;/table> </programlisting> </example> </para> <para> See also <function>each</function> and <function>array</function>. </para> </refsect1> </refentry> <refentry id="function.natsort"> <refnamediv> <refname>natsort</refname> <refpurpose> Sort an array using a "natural order" algorithm </refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>void <function>natsort</function></funcdef> <paramdef>array <parameter>array</parameter></paramdef> </funcprototype> </funcsynopsis> <para> This function implements a sort algorithm that orders alphanumeric strings in the way a human being would. This is described as a "natural ordering". An example of the difference between this algorithm and the regular computer string sorting algorithms (used in <function>sort</function>) can be seen below: </para> <para> <example> <title><function>natsort</function> example</title> <programlisting role="php"> $array1 = $array2 = array ("img12.png","img10.png","img2.png","img1.png"); sort($array1); echo "Standard sorting\n"; print_r($array1); natsort($array2); echo "\nNatural order sorting\n"; print_r($array2); </programlisting> </example> </para> <para> The code above will generate the following output: </para> <para> <informalexample> <programlisting> Standard sorting Array ( [0] =&gt; img1.png [1] =&gt; img10.png [2] =&gt; img12.png [3] =&gt; img2.png ) Natural order sorting Array ( [3] =&gt; img1.png [2] =&gt; img2.png [1] =&gt; img10.png [0] =&gt; img12.png ) </programlisting> </informalexample> For more infomation see: Martin Pool's <ulink url="&url.strnatcmp;">Natural Order String Comparison</ulink> page. </para> <para> See also <function>natcasesort</function>, <function>strnatcmp</function> and <function>strnatcasecmp</function>. </para> </refsect1> </refentry> <refentry id="function.natcasesort"> <refnamediv> <refname>natcasesort</refname> <refpurpose> Sort an array using a case insensitive "natural order" algorithm </refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>void <function>natcasesort</function></funcdef> <paramdef>array <parameter>array</parameter></paramdef> </funcprototype> </funcsynopsis> <para> This function implements a sort algorithm that orders alphanumeric strings in the way a human being would. This is described as a "natural ordering". </para> <para> <function>natcasesort</function> is a case insensitive version of <function>natsort</function>. See <function>natsort</function> for an example of the difference between this algorithm and the regular computer string sorting algorithms. </para> <para> For more infomation see: Martin Pool's <ulink url="&url.strnatcmp;">Natural Order String Comparison</ulink> page. </para> <para> See also <function>sort</function>, <function>natsort</function>, <function>strnatcmp</function> and <function>strnatcasecmp</function>. </para> </refsect1> </refentry> <refentry id="function.next"> <refnamediv> <refname>next</refname> <refpurpose> Advance the internal array pointer of an array </refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>mixed <function>next</function></funcdef> <paramdef>array <parameter>array</parameter></paramdef> </funcprototype> </funcsynopsis> <para> Returns the array element in the next place that's pointed by the internal array pointer, or false if there are no more elements. </para> <para> <function>Next</function> behaves like <function>current</function>, with one difference. It advances the internal array pointer one place forward before returning the element. That means it returns the next array element and advances the internal array pointer by one. If advancing the internal array pointer results in going beyond the end of the element list, <function>next</function> returns false. <warning> <para> If the array contains empty elements, or elements that have a key value of 0 then this function will return false for these elements as well. To properly traverse an array which may contain empty elements or elements with key values of 0 see the <function>each</function> function. </para> </warning> </para> <para> See also: <function>current</function>, <function>end</function>, <function>prev</function>, and <function>reset</function>. </para> </refsect1> </refentry> <refentry id="function.pos"> <refnamediv> <refname>pos</refname> <refpurpose>Get the current element from an array</refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>mixed <function>pos</function></funcdef> <paramdef>array <parameter>array</parameter></paramdef> </funcprototype> </funcsynopsis> <simpara> This is an alias for <function>current</function>. </simpara> <para> See also: <function>end</function>, <function>next</function>, <function>prev</function> and <function>reset</function>. </para> </refsect1> </refentry> <refentry id="function.prev"> <refnamediv> <refname>prev</refname> <refpurpose>Rewind the internal array pointer</refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>mixed <function>prev</function></funcdef> <paramdef>array <parameter>array</parameter></paramdef> </funcprototype> </funcsynopsis> <para> Returns the array element in the previous place that's pointed by the internal array pointer, or false if there are no more elements. <warning> <para> If the array contains empty elements then this function will return false for these elements as well. To properly traverse an array which may contain empty elements see the <function>each</function> function. </para> </warning> </para> <para> <function>Prev</function> behaves just like <function>next</function>, except it rewinds the internal array pointer one place instead of advancing it. </para> <para> See also: <function>current</function>, <function>end</function>, <function>next</function>, and <function>reset</function>. </para> </refsect1> </refentry> <refentry id="function.range"> <refnamediv> <refname>range</refname> <refpurpose> Create an array containing a range of integers </refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>array <function>range</function></funcdef> <paramdef>int <parameter>low</parameter></paramdef> <paramdef>int <parameter>high</parameter></paramdef> </funcprototype> </funcsynopsis> <para> <function>Range</function> returns an array of integers from <parameter>low</parameter> to <parameter>high</parameter>, inclusive. </para> <para> See <function>shuffle</function> for an example of its use. </para> </refsect1> </refentry> <refentry id="function.reset"> <refnamediv> <refname>reset</refname> <refpurpose> Set the internal pointer of an array to its first element </refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>mixed <function>reset</function></funcdef> <paramdef>array <parameter>array</parameter></paramdef> </funcprototype> </funcsynopsis> <para> <function>Reset</function> rewinds <parameter>array</parameter>'s internal pointer to the first element. </para> <para> <function>Reset</function> returns the value of the first array element. </para> <para> See also: <function>current</function>, <function>each</function>, <function>next</function>, and <function>prev</function>. </para> </refsect1> </refentry> <refentry id="function.rsort"> <refnamediv> <refname>rsort</refname> <refpurpose>Sort an array in reverse order</refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>void <function>rsort</function></funcdef> <paramdef>array <parameter>array</parameter></paramdef> <paramdef>int <parameter><optional>sort_flags</optional></parameter> </paramdef> </funcprototype> </funcsynopsis> <para> This function sorts an array in reverse order (highest to lowest). <example> <title><function>Rsort</function> example</title> <programlisting role="php"> $fruits = array ("lemon", "orange", "banana", "apple"); rsort ($fruits); reset ($fruits); while (list ($key, $val) = each ($fruits)) { echo "$key -&gt; $val\n"; } </programlisting> </example> </para> <para> This example would display: </para> <para> <informalexample> <programlisting> fruits[0] = orange fruits[1] = lemon fruits[2] = banana fruits[3] = apple </programlisting> </informalexample> </para> <para> The fruits have been sorted in reverse alphabetical order. </para> <para> You may modify the behavior of the sort using the optional parameter <parameter>sort_flags</parameter>, for details see <function>sort</function>. </para> <para> See also: <function>arsort</function>, <function>asort</function>, <function>ksort</function>, <function>sort</function>, and <function>usort</function>. </para> </refsect1> </refentry> <refentry id="function.shuffle"> <refnamediv> <refname>shuffle</refname> <refpurpose>Shuffle an array</refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>void <function>shuffle</function></funcdef> <paramdef>array <parameter>array</parameter></paramdef> </funcprototype> </funcsynopsis> <para> This function shuffles (randomizes the order of the elements in) an array. <example> <title><function>Shuffle</function> example</title> <programlisting role="php"> $numbers = range (1,20); srand (time()); shuffle ($numbers); while (list (, $number) = each ($numbers)) { echo "$number "; } </programlisting> </example> </para> <para> See also <function>arsort</function>, <function>asort</function>, <function>ksort</function>, <function>rsort</function>, <function>sort</function> and <function>usort</function>. </para> </refsect1> </refentry> <refentry id="function.sizeof"> <refnamediv> <refname>sizeof</refname> <refpurpose>Get the number of elements in an array</refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>int <function>sizeof</function></funcdef> <paramdef>array <parameter>array</parameter></paramdef> </funcprototype> </funcsynopsis> <para> Returns the number of elements in the array. </para> <para> See also <function>count</function>. </para> </refsect1> </refentry> <refentry id="function.sort"> <refnamediv> <refname>sort</refname> <refpurpose>Sort an array</refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>void <function>sort</function></funcdef> <paramdef>array <parameter>array</parameter></paramdef> <paramdef>int <parameter><optional>sort_flags</optional></parameter></paramdef> </funcprototype> </funcsynopsis> <para> This function sorts an array. Elements will be arranged from lowest to highest when this function has completed. <example> <title><function>Sort</function> example</title> <programlisting role="php"> &lt;?php $fruits = array ("lemon", "orange", "banana", "apple"); sort ($fruits); reset ($fruits); while (list ($key, $val) = each ($fruits)) { echo "fruits[".$key."] = ".$val; } ?&gt; </programlisting> </example> </para> <para> This example would display: </para> <para> <informalexample> <programlisting> fruits[0] = apple fruits[1] = banana fruits[2] = lemon fruits[3] = orange </programlisting> </informalexample> </para> <para> The fruits have been sorted in alphabetical order. </para> <para> The optional second parameter <parameter>sort_flags</parameter> may be used to modify the sorting behavior using theese valies: </para> <para> Sorting type flags: <itemizedlist> <listitem> <simpara>SORT_REGULAR - compare items normally</simpara> </listitem> <listitem> <simpara>SORT_NUMERIC - compare items numerically</simpara> </listitem> <listitem> <simpara>SORT_STRING - compare items as strings</simpara> </listitem> </itemizedlist> </para> <para> See also: <function>arsort</function>, <function>asort</function>, <function>ksort</function>, <function>natsort</function>, <function>natcasesort</function>, <function>rsort</function>, <function>usort</function> and <function>uksort</function>. </para> </refsect1> </refentry> <refentry id="function.uasort"> <refnamediv> <refname>uasort</refname> <refpurpose> Sort an array with a user-defined comparison function and maintain index association </refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>void <function>uasort</function></funcdef> <paramdef>array <parameter>array</parameter></paramdef> <paramdef>function <parameter>cmp_function</parameter></paramdef> </funcprototype> </funcsynopsis> <para> This function sorts an array such that array indices maintain their correlation with the array elements they are associated with. This is used mainly when sorting associative arrays where the actual element order is significant. The comparison function is user-defined. </para> <note> <para> Please see <function>usort</function> and <function>uksort</function> for examples of user-defined comparison functions. </para> </note> <para> See also: <function>usort</function>, <function>uksort</function>, <function>sort</function>, <function>asort</function>, <function>arsort</function>, <function>ksort</function> and <function>rsort</function>. </para> </refsect1> </refentry> <refentry id="function.uksort"> <refnamediv> <refname>uksort</refname> <refpurpose> Sort an array by keys using a user-defined comparison function </refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>void <function>uksort</function></funcdef> <paramdef>array <parameter>array</parameter></paramdef> <paramdef>function <parameter>cmp_function</parameter></paramdef> </funcprototype> </funcsynopsis> <para> This function will sort the keys of an array using a user-supplied comparison function. If the array you wish to sort needs to be sorted by some non-trivial criteria, you should use this function. </para> <para> <example> <title><function>Uksort</function> example</title> <programlisting role="php"> function cmp ($a, $b) { if ($a == $b) return 0; return ($a > $b) ? -1 : 1; } $a = array (4 =&gt; "four", 3 =&gt; "three", 20 =&gt; "twenty", 10 =&gt; "ten"); uksort ($a, "cmp"); while (list ($key, $value) = each ($a)) { echo "$key: $value\n"; } </programlisting> </example> </para> <para> This example would display: </para> <para> <informalexample> <programlisting> 20: twenty 10: ten 4: four 3: three </programlisting> </informalexample> </para> <para> See also: <function>usort</function>, <function>uasort</function>, <function>sort</function>, <function>asort</function>, <function>arsort</function>, <function>ksort</function>, <function>natsort</function> and <function>rsort</function>. </para> </refsect1> </refentry> <refentry id="function.usort"> <refnamediv> <refname>usort</refname> <refpurpose> Sort an array by values using a user-defined comparison function </refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>void <function>usort</function></funcdef> <paramdef>array <parameter>array</parameter></paramdef> <paramdef>string <parameter>cmp_function</parameter></paramdef> </funcprototype> </funcsynopsis> <para> This function will sort an array by its values using a user-supplied comparison function. If the array you wish to sort needs to be sorted by some non-trivial criteria, you should use this function. </para> <para> The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. If two members compare as equal, their order in the sorted array is undefined. </para> <para> <example> <title><function>Usort</function> example</title> <programlisting role="php"> function cmp ($a, $b) { if ($a == $b) return 0; return ($a > $b) ? -1 : 1; } $a = array (3, 2, 5, 6, 1); usort ($a, "cmp"); while (list ($key, $value) = each ($a)) { echo "$key: $value\n"; } </programlisting> </example> </para> <para> This example would display: </para> <para> <informalexample> <programlisting> 0: 6 1: 5 2: 3 3: 2 4: 1 </programlisting> </informalexample> </para> <note> <para> Obviously in this trivial case the <function>rsort</function> function would be more appropriate. </para> </note> <para> <example> <title> <function>Usort</function> example using multi-dimensional array </title> <programlisting role="php"> function cmp ($a, $b) { return strcmp($a["fruit"],$b["fruit"]); } $fruits[0]["fruit"] = "lemons"; $fruits[1]["fruit"] = "apples"; $fruits[2]["fruit"] = "grapes"; usort($fruits, "cmp"); while (list ($key, $value) = each ($fruits)) { echo "\$fruits[$key]: " . $value["fruit"] . "\n"; } </programlisting> </example> </para> <para> When sorting a multi-dimensional array, $a and $b contain references to the first index of the array. </para> <para> This example would display: </para> <para> <informalexample> <programlisting> $fruits[0]: apples $fruits[1]: grapes $fruits[2]: lemons </programlisting> </informalexample> </para> <para> <warning> <para> The underlying quicksort function in some C libraries (such as on Solaris systems) may cause PHP to crash if the comparison function does not return consistent values. </para> </warning> </para> <para> See also: <function>uasort</function>, <function>uksort</function>, <function>sort</function>, <function>asort</function>, <function>arsort</function>,<function>ksort</function>, <function>natsort</function>, and <function>rsort</function>. </para> </refsect1> </refentry> </reference> <!-- 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 sgml-parent-document:nil sgml-default-dtd-file:"../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: --> Index: phpdoc/pt_BR/functions/bc.xml +++ phpdoc/pt_BR/functions/bc.xml <reference id="ref.bc"> <title>BCMath Arbitrary Precision Mathematics Functions</title> <titleabbrev>BC math</titleabbrev> <partintro> <para> These functions are only available if PHP was configured with <option role="configure">--enable-bcmath</option>. </para> <note> <para> Due to changes in the licensing, the BCMATH library is distributed separate from the standard PHP source distribution. You can download the tar-gzipped archive at the url: <ulink url="&url.bcmath;">&url.bcmath;</ulink>. Read the file <filename>README.BCMATH</filename> in the PHP distribution for more information. </para> </note> </partintro> <refentry id="function.bcadd"> <refnamediv> <refname>bcadd</refname> <refpurpose>Add two arbitrary precision numbers</refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>string <function>bcadd</function></funcdef> <paramdef>string <parameter>left operand</parameter></paramdef> <paramdef>string <parameter>right operand</parameter></paramdef> <paramdef>int <parameter><optional>scale</optional></parameter> </paramdef> </funcprototype> </funcsynopsis> <para> Adds the <parameter>left operand</parameter> to the <parameter>right operand</parameter> and returns the sum in a string. The optional <parameter>scale</parameter> parameter is used to set the number of digits after the decimal place in the result. </para> <para> See also <function>bcsub</function>. </para> </refsect1> </refentry> <refentry id="function.bccomp"> <refnamediv> <refname>bccomp</refname> <refpurpose>Compare two arbitrary precision numbers</refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>int <function>bccomp</function></funcdef> <paramdef>string <parameter>left operand</parameter></paramdef> <paramdef>string <parameter>right operand</parameter></paramdef> <paramdef>int <parameter><optional>scale</optional></parameter> </paramdef> </funcprototype> </funcsynopsis> <para> Compares the <parameter>left operand</parameter> to the <parameter>right operand</parameter> and returns the result as an integer. The optional <parameter>scale</parameter> parameter is used to set the number of digits after the decimal place which will be used in the comparion. The return value is 0 if the two operands are equal. If the <parameter>left operand</parameter> is larger than the <parameter>right operand</parameter> the return value is +1 and if the <parameter>left operand</parameter> is less than the <parameter>right operand</parameter> the return value is -1. </para> </refsect1> </refentry> <refentry id="function.bcdiv"> <refnamediv> <refname>bcdiv</refname> <refpurpose>Divide two arbitrary precision numbers</refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>string <function>bcdiv</function></funcdef> <paramdef>string <parameter>left operand</parameter></paramdef> <paramdef>string <parameter>right operand</parameter></paramdef> <paramdef>int <parameter><optional>scale</optional></parameter> </paramdef> </funcprototype> </funcsynopsis> <para> Divides the <parameter>left operand</parameter> by the <parameter>right operand</parameter> and returns the result. The optional <parameter>scale</parameter> sets the number of digits after the decimal place in the result. </para> <para> See also <function>bcmul</function>. </para> </refsect1> </refentry> <refentry id="function.bcmod"> <refnamediv> <refname>bcmod</refname> <refpurpose> Get modulus of an arbitrary precision number </refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>string <function>bcmod</function></funcdef> <paramdef>string <parameter>left operand</parameter></paramdef> <paramdef>string <parameter>modulus</parameter></paramdef> </funcprototype> </funcsynopsis> <para> Get the modulus of the <parameter>left operand</parameter> using <parameter>modulus</parameter>. </para> <para> See also <function>bcdiv</function>. </para> </refsect1> </refentry> <refentry id="function.bcmul"> <refnamediv> <refname>bcmul</refname> <refpurpose>Multiply two arbitrary precision number</refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>string <function>bcmul</function></funcdef> <paramdef>string <parameter>left operand</parameter></paramdef> <paramdef>string <parameter>right operand</parameter></paramdef> <paramdef>int <parameter><optional>scale</optional></parameter> </paramdef> </funcprototype> </funcsynopsis> <para> Multiply the <parameter>left operand</parameter> by the <parameter>right operand</parameter> and returns the result. The optional <parameter>scale</parameter> sets the number of digits after the decimal place in the result. </para> <para> See also <function>bcdiv</function>. </para> </refsect1> </refentry> <refentry id="function.bcpow"> <refnamediv> <refname>bcpow</refname> <refpurpose> Raise an arbitrary precision number to another </refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>string <function>bcpow</function></funcdef> <paramdef>string <parameter>x</parameter></paramdef> <paramdef>string <parameter>y</parameter></paramdef> <paramdef>int <parameter><optional>scale</optional></parameter> </paramdef> </funcprototype> </funcsynopsis> <para> Raise <parameter>x</parameter> to the power <parameter>y</parameter>. The optional <parameter>scale</parameter> can be used to set the number of digits after the decimal place in the result. </para> <para> See also <function>bcsqrt</function>. </para> </refsect1> </refentry> <refentry id="function.bcscale"> <refnamediv> <refname>bcscale</refname> <refpurpose> Set default scale parameter for all bc math functions </refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>string <function>bcscale</function></funcdef> <paramdef>int <parameter>scale</parameter></paramdef> </funcprototype> </funcsynopsis> <para> This function sets the default scale parameter for all subsequent bc math functions that do not explicitly specify a scale parameter. </para> </refsect1> </refentry> <refentry id="function.bcsqrt"> <refnamediv> <refname>bcsqrt</refname> <refpurpose> Get the square root of an arbitray precision number </refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>string <function>bcsqrt</function></funcdef> <paramdef>string <parameter>operand</parameter></paramdef> <paramdef>int <parameter>scale</parameter></paramdef> </funcprototype> </funcsynopsis> <para> Return the square root of the <parameter>operand</parameter>. The optional <parameter>scale</parameter> parameter sets the number of digits after the decimal place in the result. </para> <para> See also <function>bcpow</function>. </para> </refsect1> </refentry> <refentry id="function.bcsub"> <refnamediv> <refname>bcsub</refname> <refpurpose> Subtract one arbitrary precision number from another </refpurpose> </refnamediv> <refsect1> <title>Description</title> <funcsynopsis> <funcprototype> <funcdef>string <function>bcsub</function></funcdef> <paramdef>string <parameter>left operand</parameter></paramdef> <paramdef>string <parameter>right operand</parameter></paramdef> <paramdef>int <parameter><optional>scale</optional></parameter> </paramdef> </funcprototype> </funcsynopsis> <para> Subtracts the <parameter>right operand</parameter> from the <parameter>left operand</parameter> and returns the result in a string. The optional <parameter>scale</parameter> parameter is used to set the number of digits after the decimal place in the result. </para> <para> See also <function>bcadd</function>. </para> </refsect1> </refentry> </reference> <!-- 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 sgml-parent-document:nil sgml-default-dtd-file:"../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: -->

« previous php.doc (#969332501) next »