1999-06-06 18:51:02 +00:00
|
|
|
<reference id="ref.array">
|
1999-06-20 03:04:56 +00:00
|
|
|
<title>Array functions</title>
|
1999-06-06 18:51:02 +00:00
|
|
|
<titleabbrev>Arrays</titleabbrev>
|
|
|
|
|
|
|
|
<refentry id="function.array">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>array</refname>
|
|
|
|
<refpurpose>
|
|
|
|
Create an array
|
|
|
|
</refpurpose>
|
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<funcsynopsis>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>array <function>array</function></funcdef>
|
|
|
|
<varargs/>
|
|
|
|
</funcprototype>
|
1999-06-06 18:51:02 +00:00
|
|
|
</funcsynopsis>
|
|
|
|
<para>
|
|
|
|
Returns an array of the parameters. The parameters can be given
|
|
|
|
an index with the <literal>=></literal> operator.
|
2000-01-03 22:26:44 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
<function>array</function> is a language construct used to
|
|
|
|
represent literal arrays, and not a regular function.
|
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
<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>
|
2000-01-03 22:26:44 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
$fruits = array (
|
|
|
|
"fruits" => array("a"=>"orange", "b"=>"banana", "c"=>"apple"),
|
1999-06-06 18:51:02 +00:00
|
|
|
"numbers" => array(1, 2, 3, 4, 5, 6),
|
|
|
|
"holes" => array("first", 5 => "second", "third")
|
|
|
|
);
|
2000-01-03 22:26:44 +00:00
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
<para>
|
2000-01-03 22:26:44 +00:00
|
|
|
See also: <function>list</function>.
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
1999-09-20 10:25:50 +00:00
|
|
|
<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>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>array <function>array_count_values</function></funcdef>
|
|
|
|
<paramdef>array <parameter>input</parameter></paramdef>
|
|
|
|
</funcprototype>
|
1999-09-20 10:25:50 +00:00
|
|
|
</funcsynopsis>
|
|
|
|
<para>
|
2000-01-03 22:26:44 +00:00
|
|
|
<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>
|
1999-09-20 10:25:50 +00:00
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title><function>array_count_values</function> example</title>
|
2000-01-03 22:26:44 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
$array = array(1, "hello", 1, "world", "hello");
|
|
|
|
array_count_values($array); // returns array(1=>2, "hello"=>2, "world"=>1)
|
1999-09-20 10:25:50 +00:00
|
|
|
</programlisting>
|
2000-01-03 22:26:44 +00:00
|
|
|
</example>
|
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
This function was added in PHP 4.0.
|
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
</para>
|
1999-09-20 10:25:50 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
1999-11-21 21:05:39 +00:00
|
|
|
<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>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>array <function>array_flip</function></funcdef>
|
|
|
|
<paramdef>array <parameter>trans</parameter></paramdef>
|
|
|
|
</funcprototype>
|
1999-11-21 21:05:39 +00:00
|
|
|
</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>
|
|
|
|
<note>
|
|
|
|
<para>
|
1999-11-22 07:42:45 +00:00
|
|
|
This function was added in PHP 4.0.
|
1999-11-21 21:05:39 +00:00
|
|
|
</para>
|
2000-01-03 22:26:44 +00:00
|
|
|
</note>
|
|
|
|
</para>
|
1999-11-21 21:05:39 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
1999-09-06 03:22:00 +00:00
|
|
|
<refentry id="function.array-keys">
|
1999-07-16 17:05:14 +00:00
|
|
|
<refnamediv>
|
1999-09-06 03:22:00 +00:00
|
|
|
<refname>array_keys</refname>
|
|
|
|
<refpurpose>Return all the keys of an array</refpurpose>
|
1999-07-16 17:05:14 +00:00
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<funcsynopsis>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>array <function>array_keys</function></funcdef>
|
|
|
|
<paramdef>array <parameter>input</parameter></paramdef>
|
|
|
|
<paramdef>mixed
|
|
|
|
<parameter>
|
|
|
|
<optional>search_value</optional>
|
|
|
|
</parameter>
|
|
|
|
</paramdef>
|
|
|
|
</funcprototype>
|
1999-07-16 17:05:14 +00:00
|
|
|
</funcsynopsis>
|
|
|
|
<para>
|
1999-09-07 20:25:39 +00:00
|
|
|
<function>array_keys</function> returns the keys, numeric and
|
2000-01-03 22:26:44 +00:00
|
|
|
string, from the <parameter>input</parameter> array.
|
|
|
|
</para>
|
1999-09-07 19:00:28 +00:00
|
|
|
<para>
|
1999-09-07 20:25:39 +00:00
|
|
|
If the optional <parameter>search_value</parameter> is specified,
|
|
|
|
then only the keys for that value are returned. Otherwise, all
|
2000-01-03 22:26:44 +00:00
|
|
|
the keys from the <parameter>input</parameter> are returned.
|
|
|
|
</para>
|
1999-07-16 17:05:14 +00:00
|
|
|
<para>
|
|
|
|
<example>
|
1999-09-06 03:22:00 +00:00
|
|
|
<title><function>array_keys</function> example</title>
|
2000-01-03 22:26:44 +00:00
|
|
|
<programlisting role="php">
|
1999-09-06 03:22:00 +00:00
|
|
|
$array = array(0 => 100, "color" => "red");
|
2000-01-03 22:26:44 +00:00
|
|
|
array_keys ($array); // returns array (0, "color")
|
1999-09-07 19:00:28 +00:00
|
|
|
|
|
|
|
$array = array(1, 100, 2, 100);
|
2000-01-03 22:26:44 +00:00
|
|
|
array_keys ($array, 100); // returns array (0, 2)
|
1999-07-16 17:05:14 +00:00
|
|
|
</programlisting>
|
2000-01-03 22:26:44 +00:00
|
|
|
</example>
|
|
|
|
</para>
|
1999-07-16 17:05:14 +00:00
|
|
|
<para>
|
1999-09-06 03:22:00 +00:00
|
|
|
See also <function>array_values</function>.
|
2000-01-03 22:26:44 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
This function was added in PHP 4.0.
|
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
</para>
|
1999-07-16 17:05:14 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
1999-09-06 03:22:00 +00:00
|
|
|
<refentry id="function.array-merge">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>array_merge</refname>
|
|
|
|
<refpurpose>Merge two or more arrays</refpurpose>
|
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<funcsynopsis>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>array <function>array_merge</function></funcdef>
|
|
|
|
<paramdef>array <parameter>array1</parameter></paramdef>
|
|
|
|
<paramdef>array <parameter>array2</parameter></paramdef>
|
|
|
|
<paramdef>
|
|
|
|
<parameter><optional> ...</optional></parameter>
|
|
|
|
</paramdef>
|
|
|
|
</funcprototype>
|
1999-09-06 03:22:00 +00:00
|
|
|
</funcsynopsis>
|
|
|
|
<para>
|
|
|
|
<function>array_merge</function> merges the elements of two or
|
|
|
|
more arrays together so that the values of one are appended to
|
2000-01-03 22:26:44 +00:00
|
|
|
the end of the previous one. It returns the resulting array.
|
|
|
|
</para>
|
1999-09-06 03:22:00 +00:00
|
|
|
<para>
|
1999-09-07 20:25:39 +00:00
|
|
|
If the input arrays had the same string keys, then the later
|
|
|
|
value for that key will overwrite previous one. If, however, the
|
|
|
|
arrays have the same numeric key, this does not happen since the
|
2000-01-03 22:26:44 +00:00
|
|
|
values are appended.
|
|
|
|
</para>
|
1999-09-06 03:22:00 +00:00
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title><function>array_merge</function> example</title>
|
2000-01-03 22:26:44 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
$array1 = array ("color" => "red", 2, 4);
|
|
|
|
$array2 = array ("a", "b", "color" => "green", "shape" => "trapezoid");
|
|
|
|
array_merge ($array1, $array2);
|
1999-09-06 03:22:00 +00:00
|
|
|
</programlisting>
|
|
|
|
<para>
|
1999-09-07 20:25:39 +00:00
|
|
|
Resulting array will be array("color" => "green", 2, 4, "a",
|
2000-01-03 22:26:44 +00:00
|
|
|
"b", "shape" => "trapezoid").
|
|
|
|
</para>
|
1999-09-06 03:22:00 +00:00
|
|
|
</example>
|
2000-01-03 22:26:44 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
This function was added in PHP 4.0.
|
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
</para>
|
1999-09-06 03:22:00 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
1999-11-02 14:58:47 +00:00
|
|
|
<refentry id="function.array-pad">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>array_pad</refname>
|
2000-01-03 22:26:44 +00:00
|
|
|
<refpurpose>
|
|
|
|
Pad array to the specified length with a value
|
|
|
|
</refpurpose>
|
1999-11-02 14:58:47 +00:00
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<funcsynopsis>
|
2000-06-24 07:38:45 +00:00
|
|
|
<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>
|
1999-11-02 14:58:47 +00:00
|
|
|
</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>
|
2000-01-03 22:26:44 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
$input = array (12, 10, 9);
|
1999-11-02 14:58:47 +00:00
|
|
|
|
2000-01-03 22:26:44 +00:00
|
|
|
$result = array_pad ($input, 5, 0);
|
|
|
|
// result is array (12, 10, 9, 0, 0)
|
1999-11-02 14:58:47 +00:00
|
|
|
|
2000-01-03 22:26:44 +00:00
|
|
|
$result = array_pad ($input, -7, -1);
|
|
|
|
// result is array (-1, -1, -1, -1, 12, 10, 9)
|
1999-11-02 14:58:47 +00:00
|
|
|
|
2000-01-03 22:26:44 +00:00
|
|
|
$result = array_pad ($input, 2, "noop");
|
1999-11-02 14:58:47 +00:00
|
|
|
// not padded
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
1999-07-16 17:05:14 +00:00
|
|
|
<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>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>mixed <function>array_pop</function></funcdef>
|
|
|
|
<paramdef>array <parameter>array</parameter></paramdef>
|
|
|
|
</funcprototype>
|
1999-07-16 17:05:14 +00:00
|
|
|
</funcsynopsis>
|
|
|
|
<para>
|
|
|
|
<function>array_pop</function> pops and returns the last value of
|
|
|
|
the <parameter>array</parameter>, shortening the
|
2000-01-03 22:26:44 +00:00
|
|
|
<parameter>array</parameter> by one element.
|
|
|
|
</para>
|
1999-07-16 17:05:14 +00:00
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title><function>array_pop</function> example</title>
|
2000-01-03 22:26:44 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
$stack = array ("orange", "apple", "raspberry");
|
|
|
|
$fruit = array_pop ($stack);
|
1999-07-16 17:05:14 +00:00
|
|
|
</programlisting>
|
1999-07-16 19:09:45 +00:00
|
|
|
<para>
|
|
|
|
After this, $stack has only 2 elements: "orange" and "apple",
|
2000-01-03 22:26:44 +00:00
|
|
|
and $fruit has "raspberry".
|
|
|
|
</para>
|
|
|
|
</example>
|
|
|
|
</para>
|
1999-07-16 17:05:14 +00:00
|
|
|
<para>
|
|
|
|
See also <function>array_push</function>,
|
|
|
|
<function>array_shift</function>, and
|
|
|
|
<function>array_unshift</function>.
|
2000-01-03 22:26:44 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
This function was added in PHP 4.0.
|
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
</para>
|
1999-07-16 17:05:14 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
1999-09-06 03:22:00 +00:00
|
|
|
<refentry id="function.array-push">
|
1999-07-16 17:05:14 +00:00
|
|
|
<refnamediv>
|
1999-09-06 03:22:00 +00:00
|
|
|
<refname>array_push</refname>
|
1999-09-07 20:25:39 +00:00
|
|
|
<refpurpose>
|
|
|
|
Push one or more elements onto the end of array
|
|
|
|
</refpurpose>
|
1999-07-16 17:05:14 +00:00
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<funcsynopsis>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>int <function>array_push</function></funcdef>
|
|
|
|
<paramdef>array <parameter>array</parameter></paramdef>
|
|
|
|
<paramdef>mixed <parameter>var</parameter></paramdef>
|
|
|
|
<paramdef>
|
|
|
|
<parameter><optional>...</optional></parameter>
|
|
|
|
</paramdef>
|
|
|
|
</funcprototype>
|
1999-07-16 17:05:14 +00:00
|
|
|
</funcsynopsis>
|
|
|
|
|
|
|
|
<para>
|
1999-09-06 03:22:00 +00:00
|
|
|
<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:
|
2000-01-03 22:26:44 +00:00
|
|
|
<programlisting role="php">
|
1999-09-06 03:22:00 +00:00
|
|
|
$array[] = $var;
|
|
|
|
</programlisting>
|
2000-01-03 22:26:44 +00:00
|
|
|
repeated for each <parameter>var</parameter>.
|
|
|
|
</para>
|
1999-07-16 17:05:14 +00:00
|
|
|
<para>
|
2000-01-03 22:26:44 +00:00
|
|
|
Returns the new number of elements in the array.
|
|
|
|
</para>
|
1999-07-16 17:05:14 +00:00
|
|
|
<para>
|
|
|
|
<example>
|
1999-09-06 03:22:00 +00:00
|
|
|
<title><function>array_push</function> example</title>
|
2000-01-03 22:26:44 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
$stack = array (1, 2);
|
1999-09-06 03:22:00 +00:00
|
|
|
array_push($stack, "+", 3);
|
1999-07-16 17:05:14 +00:00
|
|
|
</programlisting>
|
|
|
|
</example>
|
1999-09-07 20:25:39 +00:00
|
|
|
This example would result in $stack having 4 elements: 1, 2, "+",
|
2000-01-03 22:26:44 +00:00
|
|
|
and 3.
|
|
|
|
</para>
|
1999-07-16 17:05:14 +00:00
|
|
|
<para>
|
2000-01-03 22:26:44 +00:00
|
|
|
See also: <function>array_pop</function>,
|
1999-09-06 03:22:00 +00:00
|
|
|
<function>array_shift</function>, and
|
|
|
|
<function>array_unshift</function>.
|
2000-01-03 22:26:44 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
This function was added in PHP 4.0.
|
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
</para>
|
1999-07-16 17:05:14 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
1999-10-01 19:04:08 +00:00
|
|
|
<refentry id="function.array-reverse">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>array_reverse</refname>
|
2000-01-03 22:26:44 +00:00
|
|
|
<refpurpose>
|
|
|
|
Return an array with elements in reverse order
|
|
|
|
</refpurpose>
|
1999-10-01 19:04:08 +00:00
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<funcsynopsis>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>array <function>array_reverse</function></funcdef>
|
|
|
|
<paramdef>array <parameter>array</parameter></paramdef>
|
|
|
|
</funcprototype>
|
1999-10-01 19:04:08 +00:00
|
|
|
</funcsynopsis>
|
|
|
|
<para>
|
2000-01-03 22:26:44 +00:00
|
|
|
<function>array_reverse</function> takes input
|
|
|
|
<parameter>array</parameter> and returns a new array with the
|
|
|
|
order of the elements reversed.
|
|
|
|
</para>
|
1999-10-01 19:04:08 +00:00
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title><function>array_reverse</function> example</title>
|
2000-01-03 22:26:44 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
$input = array ("php", 4.0, array ("green", "red"));
|
|
|
|
$result = array_reverse ($input);
|
1999-10-01 19:04:08 +00:00
|
|
|
</programlisting>
|
|
|
|
</example>
|
2000-01-03 22:26:44 +00:00
|
|
|
This makes $result have array (array ("green", "red"), 4.0, "php").
|
1999-10-01 19:04:08 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
2000-01-03 22:26:44 +00:00
|
|
|
This function was added in PHP 4.0 Beta 3.
|
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
</para>
|
1999-10-01 19:04:08 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
1999-07-16 17:05:14 +00:00
|
|
|
<refentry id="function.array-shift">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>array_shift</refname>
|
2000-01-03 22:26:44 +00:00
|
|
|
<refpurpose>
|
|
|
|
Pop an element off the beginning of array
|
|
|
|
</refpurpose>
|
1999-07-16 17:05:14 +00:00
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<funcsynopsis>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>mixed <function>array_shift</function></funcdef>
|
|
|
|
<paramdef>array <parameter>array</parameter></paramdef>
|
|
|
|
</funcprototype>
|
1999-07-16 17:05:14 +00:00
|
|
|
</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
|
2000-01-03 22:26:44 +00:00
|
|
|
down.
|
|
|
|
</para>
|
1999-07-16 17:05:14 +00:00
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title><function>array_shift</function> example</title>
|
2000-01-03 22:26:44 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
$args = array ("-v", "-f");
|
|
|
|
$opt = array_shift ($args);
|
1999-07-16 17:05:14 +00:00
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
This would result in $args having one element "-f" left, and $opt
|
2000-01-03 22:26:44 +00:00
|
|
|
being "-v".
|
|
|
|
</para>
|
1999-07-16 19:06:21 +00:00
|
|
|
<para>
|
|
|
|
See also <function>array_unshift</function>,
|
|
|
|
<function>array_push</function>, and
|
|
|
|
<function>array_pop</function>.
|
2000-01-03 22:26:44 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
This function was added in PHP 4.0.
|
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
</para>
|
1999-07-16 19:06:21 +00:00
|
|
|
</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>
|
2000-06-24 07:38:45 +00:00
|
|
|
<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>
|
1999-07-16 19:06:21 +00:00
|
|
|
</funcsynopsis>
|
|
|
|
<para>
|
1999-07-16 20:51:08 +00:00
|
|
|
<function>array_slice</function> returns a sequence of elements
|
|
|
|
from the <parameter>array</parameter> specified by the
|
|
|
|
<parameter>offset</parameter> and <parameter>length</parameter>
|
2000-01-03 22:26:44 +00:00
|
|
|
parameters.
|
|
|
|
</para>
|
1999-07-16 19:06:21 +00:00
|
|
|
<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
|
2000-01-03 22:26:44 +00:00
|
|
|
start that far from the end of the <parameter>array</parameter>.
|
|
|
|
</para>
|
1999-07-16 19:06:21 +00:00
|
|
|
<para>
|
|
|
|
If <parameter>length</parameter> is given and is positive, then
|
|
|
|
the sequence will have that many elements in it. If
|
1999-07-16 21:12:20 +00:00
|
|
|
<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
|
2000-01-03 22:26:44 +00:00
|
|
|
<parameter>array</parameter>.
|
|
|
|
</para>
|
1999-07-16 19:06:21 +00:00
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title><function>array_slice</function> examples</title>
|
2000-01-03 22:26:44 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
$input = array ("a", "b", "c", "d", "e");
|
1999-07-16 19:06:21 +00:00
|
|
|
|
2000-01-03 22:26:44 +00:00
|
|
|
$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"
|
1999-07-16 19:06:21 +00:00
|
|
|
</programlisting>
|
2000-01-03 22:26:44 +00:00
|
|
|
</example>
|
|
|
|
</para>
|
1999-07-16 19:06:21 +00:00
|
|
|
<para>
|
|
|
|
See also <function>array_splice</function>.
|
2000-01-03 22:26:44 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
This function was added in PHP 4.0.
|
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
</para>
|
1999-07-16 17:05:14 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
1999-06-06 18:51:02 +00:00
|
|
|
|
1999-07-16 21:12:20 +00:00
|
|
|
<refentry id="function.array-splice">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>array_splice</refname>
|
1999-09-07 20:25:39 +00:00
|
|
|
<refpurpose>
|
|
|
|
Remove a portion of the array and replace it with something
|
|
|
|
else
|
|
|
|
</refpurpose>
|
1999-07-16 21:12:20 +00:00
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<funcsynopsis>
|
2000-06-24 07:38:45 +00:00
|
|
|
<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>
|
1999-07-16 21:12:20 +00:00
|
|
|
</funcsynopsis>
|
|
|
|
<para>
|
2000-04-28 17:16:32 +00:00
|
|
|
<function>array_splice</function> removes the elements designated
|
1999-07-16 21:12:20 +00:00
|
|
|
by <parameter>offset</parameter> and
|
|
|
|
<parameter>length</parameter> from the
|
|
|
|
<parameter>input</parameter> array, and replaces them with the
|
1999-09-07 20:25:39 +00:00
|
|
|
elements of the <parameter>replacement</parameter> array, if
|
2000-01-03 22:26:44 +00:00
|
|
|
supplied.
|
|
|
|
</para>
|
1999-07-16 21:12:20 +00:00
|
|
|
<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
|
2000-01-03 22:26:44 +00:00
|
|
|
from the end of the <parameter>input</parameter> array.
|
|
|
|
</para>
|
1999-07-16 21:12:20 +00:00
|
|
|
<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
|
2000-01-03 22:26:44 +00:00
|
|
|
<parameter>length</parameter>.
|
|
|
|
</para>
|
1999-07-16 21:12:20 +00:00
|
|
|
<para>
|
|
|
|
If <parameter>replacement</parameter> array is specified, then
|
1999-09-07 20:25:39 +00:00
|
|
|
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>
|
2000-01-03 22:26:44 +00:00
|
|
|
around it, unless the element is an array itself.
|
|
|
|
</para>
|
1999-07-16 21:12:20 +00:00
|
|
|
<para>
|
|
|
|
The following equivalences hold:
|
2000-01-03 22:26:44 +00:00
|
|
|
<programlisting role="php">
|
1999-07-16 21:12:20 +00:00
|
|
|
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)
|
2000-01-03 22:26:44 +00:00
|
|
|
</programlisting>
|
|
|
|
</para>
|
1999-07-16 21:12:20 +00:00
|
|
|
<para>
|
2000-01-03 22:26:44 +00:00
|
|
|
Returns the array consisting of removed elements.
|
|
|
|
</para>
|
1999-07-16 21:12:20 +00:00
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title><function>array_splice</function> examples</title>
|
2000-01-03 22:26:44 +00:00
|
|
|
<programlisting role="php">
|
1999-07-16 21:12:20 +00:00
|
|
|
$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")
|
1999-09-07 20:25:39 +00:00
|
|
|
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")
|
1999-07-16 21:12:20 +00:00
|
|
|
</programlisting>
|
2000-01-03 22:26:44 +00:00
|
|
|
</example>
|
|
|
|
</para>
|
1999-07-16 21:12:20 +00:00
|
|
|
<para>
|
|
|
|
See also <function>array_slice</function>.
|
2000-01-03 22:26:44 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
This function was added in PHP 4.0.
|
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
</para>
|
1999-07-16 21:12:20 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
1999-09-06 03:22:00 +00:00
|
|
|
<refentry id="function.array-unshift">
|
1999-07-16 22:28:56 +00:00
|
|
|
<refnamediv>
|
1999-09-06 03:22:00 +00:00
|
|
|
<refname>array_unshift</refname>
|
|
|
|
<refpurpose>Push one or more elements
|
|
|
|
onto the beginning of array</refpurpose>
|
1999-07-16 22:28:56 +00:00
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<funcsynopsis>
|
1999-09-06 03:22:00 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>int <function>array_unshift</function></funcdef>
|
|
|
|
<paramdef>array <parameter>array</parameter></paramdef>
|
|
|
|
<paramdef>mixed <parameter>var</parameter></paramdef>
|
2000-01-03 22:26:44 +00:00
|
|
|
<paramdef>
|
|
|
|
<parameter>
|
2000-06-24 07:38:45 +00:00
|
|
|
<optional>...</optional>
|
2000-01-03 22:26:44 +00:00
|
|
|
</parameter>
|
|
|
|
</paramdef>
|
1999-09-06 03:22:00 +00:00
|
|
|
</funcprototype>
|
1999-07-16 22:28:56 +00:00
|
|
|
</funcsynopsis>
|
|
|
|
<para>
|
1999-09-06 03:22:00 +00:00
|
|
|
<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
|
2000-01-03 22:26:44 +00:00
|
|
|
elements stay in the same order.
|
|
|
|
</para>
|
1999-07-16 21:43:06 +00:00
|
|
|
<para>
|
1999-09-06 03:22:00 +00:00
|
|
|
Returns the new number of elements in the
|
2000-01-03 22:26:44 +00:00
|
|
|
<parameter>array</parameter>.
|
|
|
|
</para>
|
1999-07-16 21:43:06 +00:00
|
|
|
<para>
|
|
|
|
<example>
|
1999-09-06 03:22:00 +00:00
|
|
|
<title><function>array_unshift</function> example</title>
|
2000-01-03 22:26:44 +00:00
|
|
|
<programlisting role="php">
|
1999-09-06 03:22:00 +00:00
|
|
|
$queue = array("p1", "p3");
|
|
|
|
array_unshift($queue, "p4", "p5", "p6");
|
1999-07-16 21:43:06 +00:00
|
|
|
</programlisting>
|
|
|
|
</example>
|
1999-09-06 03:22:00 +00:00
|
|
|
This would result in $queue having 5 elements: "p4", "p5", "p6",
|
2000-01-03 22:26:44 +00:00
|
|
|
"p1", and "p3".
|
|
|
|
</para>
|
1999-07-16 21:43:06 +00:00
|
|
|
<para>
|
1999-09-06 03:22:00 +00:00
|
|
|
See also <function>array_shift</function>,
|
|
|
|
<function>array_push</function>, and
|
|
|
|
<function>array_pop</function>.
|
2000-01-03 22:26:44 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
This function was added in PHP 4.0.
|
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
</para>
|
1999-07-16 21:43:06 +00:00
|
|
|
</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>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>array <function>array_values</function></funcdef>
|
|
|
|
<paramdef>array <parameter>input</parameter></paramdef>
|
|
|
|
</funcprototype>
|
1999-07-16 21:43:06 +00:00
|
|
|
</funcsynopsis>
|
|
|
|
<para>
|
1999-09-07 20:25:39 +00:00
|
|
|
<function>array_values</function> returns all the values from the
|
2000-01-03 22:26:44 +00:00
|
|
|
<parameter>input</parameter> array.
|
|
|
|
</para>
|
1999-07-16 21:43:06 +00:00
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title><function>array_values</function> example</title>
|
2000-01-03 22:26:44 +00:00
|
|
|
<programlisting role="php">
|
1999-07-16 21:43:06 +00:00
|
|
|
$array = array("size" => "XL", "color" => "gold");
|
|
|
|
array_values($array); // returns array("XL", "gold")
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
1999-07-16 21:45:26 +00:00
|
|
|
<note>
|
1999-07-16 22:49:11 +00:00
|
|
|
<para>
|
2000-01-03 22:26:44 +00:00
|
|
|
This function was added in PHP 4.0.
|
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
</para>
|
1999-07-16 21:43:06 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
1999-07-16 21:12:20 +00:00
|
|
|
|
1999-06-06 18:51:02 +00:00
|
|
|
<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>
|
2000-06-24 07:38:45 +00:00
|
|
|
<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>
|
1999-06-06 18:51:02 +00:00
|
|
|
</funcsynopsis>
|
|
|
|
<simpara>
|
|
|
|
Applies the function named by <parameter>func</parameter> to each
|
2000-01-03 22:26:44 +00:00
|
|
|
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>
|
1999-08-14 16:47:11 +00:00
|
|
|
If <parameter>func</parameter> requires more than two or three
|
|
|
|
arguments, depending on <parameter>userdata</parameter>, a
|
1999-06-06 18:51:02 +00:00
|
|
|
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>
|
2000-01-03 22:26:44 +00:00
|
|
|
call, or by using <function>error_reporting</function>.
|
|
|
|
</simpara>
|
1999-06-28 02:07:12 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
1999-08-14 16:47:11 +00:00
|
|
|
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.
|
1999-06-28 02:07:12 +00:00
|
|
|
</para>
|
|
|
|
</note>
|
1999-09-14 13:29:55 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
1999-12-15 18:45:44 +00:00
|
|
|
Passing the key and userdata to <parameter>func</parameter> was
|
2000-01-03 22:26:44 +00:00
|
|
|
added in 4.0.
|
|
|
|
</para>
|
1999-12-15 18:45:44 +00:00
|
|
|
<para>
|
|
|
|
In PHP 4 <function>reset</function> needs to be called as
|
|
|
|
necessary since <function>array_walk</function> does not reset
|
2000-01-03 22:26:44 +00:00
|
|
|
the array by default.
|
|
|
|
</para>
|
1999-09-14 13:29:55 +00:00
|
|
|
</note>
|
1999-06-06 18:51:02 +00:00
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title><function>array_walk</function> example</title>
|
2000-01-03 22:26:44 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
$fruits = array ("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
|
1999-06-06 18:51:02 +00:00
|
|
|
|
2000-01-03 22:26:44 +00:00
|
|
|
function test_alter (&$item1, $key, $prefix) {
|
1999-08-14 16:47:11 +00:00
|
|
|
$item1 = "$prefix: $item1";
|
1999-06-06 18:51:02 +00:00
|
|
|
}
|
|
|
|
|
2000-01-03 22:26:44 +00:00
|
|
|
function test_print ($item2, $key) {
|
1999-08-14 16:47:11 +00:00
|
|
|
echo "$key. $item2<br>\n";
|
1999-06-06 18:51:02 +00:00
|
|
|
}
|
|
|
|
|
2000-01-03 22:26:44 +00:00
|
|
|
array_walk ($fruits, 'test_print');
|
|
|
|
reset ($fruits);
|
|
|
|
array_walk ($fruits, 'test_alter', 'fruit');
|
|
|
|
reset ($fruits);
|
|
|
|
array_walk ($fruits, 'test_print');
|
1999-06-06 18:51:02 +00:00
|
|
|
</programlisting>
|
2000-01-03 22:26:44 +00:00
|
|
|
</example>
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
<simpara>
|
2000-01-03 22:26:44 +00:00
|
|
|
See also <function>each</function> and <function>list</function>.
|
|
|
|
</simpara>
|
1999-06-06 18:51:02 +00:00
|
|
|
</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>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>void <function>arsort</function></funcdef>
|
|
|
|
<paramdef>array <parameter>array</parameter></paramdef>
|
|
|
|
</funcprototype>
|
1999-06-06 18:51:02 +00:00
|
|
|
</funcsynopsis>
|
|
|
|
<para>
|
1999-09-07 20:25:39 +00:00
|
|
|
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.
|
1999-06-06 18:51:02 +00:00
|
|
|
<example>
|
|
|
|
<title><function>arsort</function> example</title>
|
2000-01-03 22:26:44 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
$fruits = array ("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
|
|
|
|
arsort ($fruits);
|
2000-06-15 08:50:25 +00:00
|
|
|
reset($fruits);
|
|
|
|
while(list($key,$val)=each($fruits)) {
|
|
|
|
echo "$key = $val\n";
|
1999-06-06 18:51:02 +00:00
|
|
|
}
|
2000-01-03 22:26:44 +00:00
|
|
|
</programlisting>
|
|
|
|
</example>
|
1999-06-06 18:51:02 +00:00
|
|
|
This example would display:
|
|
|
|
<computeroutput>
|
|
|
|
fruits[a] = orange
|
|
|
|
fruits[d] = lemon
|
|
|
|
fruits[b] = banana
|
|
|
|
fruits[c] = apple
|
2000-01-03 22:26:44 +00:00
|
|
|
</computeroutput>
|
1999-06-06 18:51:02 +00:00
|
|
|
The fruits have been sorted in reverse alphabetical order, and
|
2000-01-03 22:26:44 +00:00
|
|
|
the index associated with each element has been maintained.
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
<para>
|
|
|
|
See also: <function>asort</function>, <function>rsort</function>,
|
2000-01-03 22:26:44 +00:00
|
|
|
<function>ksort</function>, and <function>sort</function>.
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
</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>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>void <function>asort</function></funcdef>
|
|
|
|
<paramdef>array <parameter>array</parameter></paramdef>
|
|
|
|
</funcprototype>
|
1999-06-06 18:51:02 +00:00
|
|
|
</funcsynopsis>
|
|
|
|
<para>
|
1999-09-07 20:25:39 +00:00
|
|
|
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.
|
1999-06-06 18:51:02 +00:00
|
|
|
<example>
|
|
|
|
<title><function>asort</function> example</title>
|
2000-01-03 22:26:44 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
$fruits = array ("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
|
|
|
|
asort ($fruits);
|
2000-06-15 08:50:25 +00:00
|
|
|
reset($fruits);
|
|
|
|
while(list($key,$val)=each($fruits)) {
|
|
|
|
echo "$key = $val\n";
|
1999-06-06 18:51:02 +00:00
|
|
|
}
|
2000-01-03 22:26:44 +00:00
|
|
|
</programlisting>
|
|
|
|
</example>
|
1999-06-06 18:51:02 +00:00
|
|
|
This example would display:
|
|
|
|
<computeroutput>
|
|
|
|
fruits[c] = apple
|
|
|
|
fruits[b] = banana
|
|
|
|
fruits[d] = lemon
|
|
|
|
fruits[a] = orange
|
2000-01-03 22:26:44 +00:00
|
|
|
</computeroutput>
|
1999-06-06 18:51:02 +00:00
|
|
|
The fruits have been sorted in alphabetical order, and the index
|
2000-01-03 22:26:44 +00:00
|
|
|
associated with each element has been maintained.
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
<para>
|
|
|
|
See also <function>arsort</function>, <function>rsort</function>,
|
2000-01-03 22:26:44 +00:00
|
|
|
<function>ksort</function>, and <function>sort</function>.
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
1999-07-17 15:41:41 +00:00
|
|
|
<refentry id="function.compact">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>compact</refname>
|
1999-09-07 20:25:39 +00:00
|
|
|
<refpurpose>
|
|
|
|
Create array containing variables and their values
|
|
|
|
</refpurpose>
|
1999-07-17 15:41:41 +00:00
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<funcsynopsis>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>array <function>compact</function></funcdef>
|
|
|
|
<paramdef><parameter>string varname | array
|
|
|
|
varnames</parameter></paramdef>
|
|
|
|
<paramdef>
|
|
|
|
<parameter><optional>...</optional></parameter>
|
|
|
|
</paramdef>
|
|
|
|
</funcprototype>
|
1999-07-17 15:41:41 +00:00
|
|
|
</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;
|
2000-01-03 22:26:44 +00:00
|
|
|
<function>compact</function> handles it recursively.
|
|
|
|
</para>
|
1999-07-17 15:41:41 +00:00
|
|
|
<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>.
|
2000-01-03 22:26:44 +00:00
|
|
|
It returns the output array with all the variables added to it.
|
|
|
|
</para>
|
1999-07-17 15:41:41 +00:00
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title><function>compact</function> example</title>
|
2000-01-03 22:26:44 +00:00
|
|
|
<programlisting role="php">
|
1999-07-17 15:41:41 +00:00
|
|
|
$city = "San Francisco";
|
|
|
|
$state = "CA";
|
|
|
|
$event = "SIGGRAPH";
|
|
|
|
|
2000-01-03 22:26:44 +00:00
|
|
|
$location_vars = array ("city", "state");
|
1999-07-17 15:41:41 +00:00
|
|
|
|
2000-01-03 22:26:44 +00:00
|
|
|
$result = compact ("event", $location_vars);
|
1999-07-17 15:41:41 +00:00
|
|
|
</programlisting>
|
|
|
|
<para>
|
2000-01-03 22:26:44 +00:00
|
|
|
After this, $result will be array ("event" => "SIGGRAPH",
|
|
|
|
"city" => "San Francisco", "state" => "CA").
|
|
|
|
</para>
|
|
|
|
</example>
|
|
|
|
</para>
|
1999-07-17 15:41:41 +00:00
|
|
|
<para>
|
|
|
|
See also <function>extract</function>.
|
2000-01-03 22:26:44 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
This function was added in PHP 4.0.
|
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
</para>
|
1999-07-17 15:41:41 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
1999-06-06 18:51:02 +00:00
|
|
|
<refentry id="function.count">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>count</refname>
|
|
|
|
<refpurpose>count elements in a variable</refpurpose>
|
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<funcsynopsis>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>int <function>count</function></funcdef>
|
|
|
|
<paramdef>mixed <parameter>var</parameter></paramdef>
|
|
|
|
</funcprototype>
|
1999-06-06 18:51:02 +00:00
|
|
|
</funcsynopsis>
|
|
|
|
<para>
|
1999-09-07 20:25:39 +00:00
|
|
|
Returns the number of elements in <parameter>var</parameter>,
|
|
|
|
which is typically an array (since anything else will have one
|
2000-01-03 22:26:44 +00:00
|
|
|
element).
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
<para>
|
2000-01-03 22:26:44 +00:00
|
|
|
Returns 1 if the variable is not an array.
|
|
|
|
</para>
|
1999-06-28 02:07:12 +00:00
|
|
|
<para>
|
|
|
|
Returns 0 if the variable is not set.
|
2000-01-03 22:26:44 +00:00
|
|
|
<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>
|
1999-06-06 18:51:02 +00:00
|
|
|
<para>
|
2000-01-03 22:26:44 +00:00
|
|
|
See also: <function>sizeof</function>,
|
|
|
|
<function>isset</function>, and
|
|
|
|
<function>is_array</function>.
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
|
|
|
<refentry id="function.current">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>current</refname>
|
1999-06-28 02:07:12 +00:00
|
|
|
<refpurpose>Return the current element in an array</refpurpose>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<funcsynopsis>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>mixed <function>current</function></funcdef>
|
|
|
|
<paramdef>array <parameter>array</parameter></paramdef>
|
|
|
|
</funcprototype>
|
1999-06-06 18:51:02 +00:00
|
|
|
</funcsynopsis>
|
|
|
|
<para>
|
1999-06-28 02:07:12 +00:00
|
|
|
Every array has an internal pointer to its "current" element,
|
|
|
|
which is initialized to the first element inserted into the
|
2000-01-03 22:26:44 +00:00
|
|
|
array.
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
<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.
|
|
|
|
|
2000-01-03 22:26:44 +00:00
|
|
|
<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>
|
1999-06-06 18:51:02 +00:00
|
|
|
<para>
|
2000-01-03 22:26:44 +00:00
|
|
|
See also: <function>end</function>, <function>next</function>,
|
|
|
|
<function>prev</function> and <function>reset</function>.
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
|
|
|
<refentry id="function.each">
|
|
|
|
<refnamediv>
|
1999-09-07 20:25:39 +00:00
|
|
|
<refname>each</refname>
|
|
|
|
<refpurpose>
|
|
|
|
Return the next key and value pair from an array
|
|
|
|
</refpurpose>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<funcsynopsis>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>array <function>each</function></funcdef>
|
|
|
|
<paramdef>array <parameter>array</parameter></paramdef>
|
|
|
|
</funcprototype>
|
1999-06-06 18:51:02 +00:00
|
|
|
</funcsynopsis>
|
|
|
|
<para>
|
1999-06-28 02:07:12 +00:00
|
|
|
Returns the current key and value pair from the array
|
1999-09-07 20:25:39 +00:00
|
|
|
<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
|
2000-01-03 22:26:44 +00:00
|
|
|
<emphasis>value</emphasis> contain the data.
|
|
|
|
</para>
|
1999-06-28 02:07:12 +00:00
|
|
|
<para>
|
|
|
|
If the internal pointer for the array points past the end of the
|
2000-01-03 22:26:44 +00:00
|
|
|
array contents, <function>each</function> returns false.
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
<para>
|
|
|
|
<example>
|
1999-06-28 02:07:12 +00:00
|
|
|
<title><function>each</function> examples</title>
|
2000-01-03 22:26:44 +00:00
|
|
|
<programlisting role="php">
|
1999-09-07 20:25:39 +00:00
|
|
|
$foo = array ("bob", "fred", "jussi", "jouni", "egon", "marliese");
|
2000-01-03 22:26:44 +00:00
|
|
|
$bar = each ($foo);
|
1999-06-06 18:51:02 +00:00
|
|
|
</programlisting>
|
|
|
|
<para>
|
|
|
|
<literal>$bar</literal> now contains the following key/value
|
|
|
|
pairs:
|
|
|
|
<itemizedlist spacing="compact">
|
1999-10-20 22:41:42 +00:00
|
|
|
<listitem><simpara>0 => 0</simpara></listitem>
|
|
|
|
<listitem><simpara>1 => 'bob'</simpara></listitem>
|
|
|
|
<listitem><simpara>key => 0</simpara></listitem>
|
|
|
|
<listitem><simpara>value => 'bob'</simpara></listitem>
|
1999-06-06 18:51:02 +00:00
|
|
|
</itemizedlist>
|
|
|
|
|
2000-01-03 22:26:44 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
$foo = array ("Robert" => "Bob", "Seppo" => "Sepi");
|
|
|
|
$bar = each ($foo);
|
|
|
|
</programlisting>
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
<para>
|
|
|
|
<literal>$bar</literal> now contains the following key/value
|
|
|
|
pairs:
|
|
|
|
<itemizedlist spacing="compact">
|
1999-10-20 22:41:42 +00:00
|
|
|
<listitem><simpara>0 => 'Robert'</simpara></listitem>
|
|
|
|
<listitem><simpara>1 => 'Bob'</simpara></listitem>
|
|
|
|
<listitem><simpara>key => 'Robert'</simpara></listitem>
|
|
|
|
<listitem><simpara>value => 'Bob'</simpara></listitem>
|
2000-01-03 22:26:44 +00:00
|
|
|
</itemizedlist>
|
|
|
|
</para>
|
|
|
|
</example>
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
|
|
|
|
<para>
|
|
|
|
<function>each</function> is typically used in conjunction with
|
|
|
|
<function>list</function> to traverse an array; for instance,
|
|
|
|
$HTTP_POST_VARS:
|
1999-09-07 20:25:39 +00:00
|
|
|
<example>
|
|
|
|
<title>
|
|
|
|
Traversing $HTTP_POST_VARS with <function>each</function>
|
|
|
|
</title>
|
2000-01-03 22:26:44 +00:00
|
|
|
<programlisting role="php">
|
1999-06-06 18:51:02 +00:00
|
|
|
echo "Values submitted via POST method:<br>";
|
2000-01-03 22:26:44 +00:00
|
|
|
reset ($HTTP_POST_VARS);
|
|
|
|
while (list ($key, $val) = each ($HTTP_POST_VARS)) {
|
|
|
|
echo "$key => $val<br>";
|
1999-06-06 18:51:02 +00:00
|
|
|
}
|
|
|
|
</programlisting>
|
2000-01-03 22:26:44 +00:00
|
|
|
</example>
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
|
2000-01-03 22:26:44 +00:00
|
|
|
<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>
|
1999-06-06 18:51:02 +00:00
|
|
|
<para>
|
|
|
|
See also <function>key</function>, <function>list</function>,
|
2000-01-03 22:26:44 +00:00
|
|
|
<function>current</function>, <function>reset</function>,
|
|
|
|
<function>next</function>, and <function>prev</function>.
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
|
|
|
<refentry id="function.end">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>end</refname>
|
1999-09-07 20:25:39 +00:00
|
|
|
<refpurpose>
|
|
|
|
Set the internal pointer of an array to its last element
|
|
|
|
</refpurpose>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<funcsynopsis>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef><function>end</function></funcdef>
|
|
|
|
<paramdef>array <parameter>array</parameter></paramdef>
|
|
|
|
</funcprototype>
|
1999-06-06 18:51:02 +00:00
|
|
|
</funcsynopsis>
|
|
|
|
<para>
|
|
|
|
<function>end</function> advances <parameter>array</parameter>'s
|
2000-01-03 22:26:44 +00:00
|
|
|
internal pointer to the last element.
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
<para>
|
2000-01-03 22:26:44 +00:00
|
|
|
See also: <function>current</function>,
|
|
|
|
<function>each</function>, <function>end</function>,
|
|
|
|
<function>next</function>, and <function>reset</function>.
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
1999-07-16 21:48:10 +00:00
|
|
|
<refentry id="function.extract">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>extract</refname>
|
1999-09-07 20:25:39 +00:00
|
|
|
<refpurpose>
|
|
|
|
Import variables into the symbol table from an array
|
|
|
|
</refpurpose>
|
1999-07-16 21:48:10 +00:00
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<funcsynopsis>
|
2000-06-24 07:38:45 +00:00
|
|
|
<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>
|
1999-07-16 21:48:10 +00:00
|
|
|
</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
|
2000-01-03 22:26:44 +00:00
|
|
|
<parameter>prefix</parameter> parameters.
|
|
|
|
</para>
|
1999-09-07 20:25:39 +00:00
|
|
|
<para>
|
|
|
|
<function>extract</function> checks for colissions with existing
|
|
|
|
variables. The way collisions are treated is determined by
|
1999-07-16 21:48:10 +00:00
|
|
|
<parameter>extract_type</parameter>. It can be one of the
|
|
|
|
following values:
|
1999-09-07 20:25:39 +00:00
|
|
|
<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>
|
2000-01-03 22:26:44 +00:00
|
|
|
If there is a collision, don't overwrite the existing
|
|
|
|
variable.
|
1999-09-07 20:25:39 +00:00
|
|
|
</simpara>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
<varlistentry>
|
|
|
|
<term>EXTR_PREFIX_SAME</term>
|
|
|
|
<listitem>
|
|
|
|
<simpara>If there is a collision, prefix the new variable with
|
2000-01-03 22:26:44 +00:00
|
|
|
<parameter>prefix</parameter>.
|
1999-09-07 20:25:39 +00:00
|
|
|
</simpara>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
<varlistentry>
|
|
|
|
<term>EXTR_PREFIX_ALL</term>
|
|
|
|
<listitem>
|
|
|
|
<simpara>
|
|
|
|
Prefix all variables with <parameter>prefix</parameter>.
|
|
|
|
</simpara>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2000-01-03 22:26:44 +00:00
|
|
|
</variablelist>
|
|
|
|
</para>
|
1999-09-07 20:25:39 +00:00
|
|
|
<para>
|
|
|
|
If <parameter>extract_type</parameter> is not specified, it is
|
2000-01-03 22:26:44 +00:00
|
|
|
assumed to be EXTR_OVERWRITE.
|
|
|
|
</para>
|
1999-09-07 20:25:39 +00:00
|
|
|
<para>
|
1999-07-16 21:48:10 +00:00
|
|
|
Note that <parameter>prefix</parameter> is only required if
|
|
|
|
<parameter>extract_type</parameter> is EXTR_PREFIX_SAME or
|
2000-01-03 22:26:44 +00:00
|
|
|
EXTR_PREFIX_ALL.
|
|
|
|
</para>
|
1999-09-07 20:25:39 +00:00
|
|
|
<para>
|
1999-07-16 21:48:10 +00:00
|
|
|
<function>extract</function> checks each key to see if it
|
|
|
|
constitues a valid variable name, and if it does only then does
|
2000-01-03 22:26:44 +00:00
|
|
|
it proceed to import it.
|
|
|
|
</para>
|
1999-09-07 20:25:39 +00:00
|
|
|
<para>
|
1999-07-16 21:48:10 +00:00
|
|
|
A possible use for extract is to import into symbol table
|
|
|
|
variables contained in an associative array returned by
|
2000-01-03 22:26:44 +00:00
|
|
|
<function>wddx_deserialize</function>.
|
|
|
|
</para>
|
1999-07-16 21:48:10 +00:00
|
|
|
<para>
|
|
|
|
<example>
|
2000-01-03 22:26:44 +00:00
|
|
|
<title><function>Extract</function> example</title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<php?
|
1999-07-16 21:48:10 +00:00
|
|
|
|
|
|
|
/* Suppose that $var_array is an array returned from
|
|
|
|
wddx_deserialize */
|
2000-01-03 22:26:44 +00:00
|
|
|
|
1999-07-16 21:48:10 +00:00
|
|
|
$size = "large";
|
2000-01-03 22:26:44 +00:00
|
|
|
$var_array = array ("color" => "blue",
|
|
|
|
"size" => "medium",
|
|
|
|
"shape" => "sphere");
|
|
|
|
extract ($var_array, EXTR_PREFIX_SAME, "wddx");
|
1999-07-16 21:48:10 +00:00
|
|
|
|
|
|
|
print "$color, $size, $shape, $wddx_size\n";
|
|
|
|
|
|
|
|
?>
|
|
|
|
</programlisting>
|
2000-01-03 22:26:44 +00:00
|
|
|
</example>
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
The above example will produce:
|
|
|
|
<programlisting>
|
1999-07-16 21:48:10 +00:00
|
|
|
blue, large, sphere, medium
|
2000-01-03 22:26:44 +00:00
|
|
|
</programlisting>
|
|
|
|
</para>
|
1999-09-07 20:25:39 +00:00
|
|
|
<para>
|
1999-07-16 21:48:10 +00:00
|
|
|
The $size wasn't overwritten, becaus we specified
|
|
|
|
EXTR_PREFIX_SAME, which resulted in $wddx_size being created.
|
|
|
|
If EXTR_SKIP was specified, then $wddx_size wouldn't even have
|
|
|
|
been created. EXTR_OVERWRITE would have cause $size to have
|
|
|
|
value "medium", and EXTR_PREFIX_ALL would result in new
|
2000-01-03 22:26:44 +00:00
|
|
|
variables being named $wddx_color, $wddx_size, and $wddx_shape.
|
|
|
|
</para>
|
1999-07-16 21:48:10 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
1999-07-16 21:43:06 +00:00
|
|
|
<refentry id="function.in-array">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>in_array</refname>
|
1999-07-16 22:28:56 +00:00
|
|
|
<refpurpose>Return true if a value exists in an array</refpurpose>
|
1999-07-16 21:43:06 +00:00
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<funcsynopsis>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>bool in_array</funcdef>
|
|
|
|
<paramdef>mixed <parameter>needle</parameter></paramdef>
|
|
|
|
<paramdef>array <parameter>haystack</parameter></paramdef>
|
|
|
|
</funcprototype>
|
1999-07-16 21:43:06 +00:00
|
|
|
</funcsynopsis>
|
|
|
|
<para>
|
|
|
|
Searches <parameter>haystack</parameter> for
|
|
|
|
<parameter>needle</parameter> and returns true if it is found in
|
2000-01-03 22:26:44 +00:00
|
|
|
the array, false otherwise.
|
|
|
|
</para>
|
1999-07-16 21:43:06 +00:00
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title><function>in_array</function> example</title>
|
2000-01-03 22:26:44 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
$os = array ("Mac", "NT", "Irix", "Linux");
|
|
|
|
if (in_array ("Irix", $os))
|
|
|
|
print "Got Irix";
|
1999-07-16 21:43:06 +00:00
|
|
|
</programlisting>
|
|
|
|
</example>
|
2000-01-03 22:26:44 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
This function was added in PHP 4.0.
|
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
</para>
|
1999-07-16 21:43:06 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
1999-06-06 18:51:02 +00:00
|
|
|
<refentry id="function.key">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>key</refname>
|
1999-06-28 02:07:12 +00:00
|
|
|
<refpurpose>Fetch a key from an associative array</refpurpose>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<funcsynopsis>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>mixed <function>key</function></funcdef>
|
|
|
|
<paramdef>array <parameter>array</parameter></paramdef>
|
|
|
|
</funcprototype>
|
1999-06-06 18:51:02 +00:00
|
|
|
</funcsynopsis>
|
|
|
|
<para>
|
|
|
|
<function>key</function> returns the index element of the
|
2000-01-03 22:26:44 +00:00
|
|
|
current array position.
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
<para>
|
2000-01-03 22:26:44 +00:00
|
|
|
See also: <function>current</function>, <function>next</function>
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
1999-09-21 14:41:01 +00:00
|
|
|
<refentry id="function.krsort">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>krsort</refname>
|
|
|
|
<refpurpose>Sort an array by key in reverse order</refpurpose>
|
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<funcsynopsis>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>int <function>krsort</function></funcdef>
|
|
|
|
<paramdef>array <parameter>array</parameter></paramdef>
|
|
|
|
</funcprototype>
|
1999-09-21 14:41:01 +00:00
|
|
|
</funcsynopsis>
|
|
|
|
<para>
|
2000-01-03 22:26:44 +00:00
|
|
|
Sorts an array by key in reverse order, maintaining key to data
|
|
|
|
correlations. This is useful mainly for associative arrays.
|
|
|
|
<example>
|
1999-09-21 14:41:01 +00:00
|
|
|
<title><function>krsort</function> example</title>
|
2000-01-03 22:26:44 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
$fruits = array ("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
|
|
|
|
krsort ($fruits);
|
2000-06-15 08:50:25 +00:00
|
|
|
reset($fruits);
|
|
|
|
while(list($key,$val)=each($fruits)) {
|
|
|
|
echo "$key -> $val\n";
|
1999-09-21 14:41:01 +00:00
|
|
|
}
|
2000-01-03 22:26:44 +00:00
|
|
|
</programlisting>
|
|
|
|
</example>
|
1999-09-21 14:41:01 +00:00
|
|
|
This example would display:
|
|
|
|
<computeroutput>
|
|
|
|
fruits[d] = lemon
|
|
|
|
fruits[c] = apple
|
|
|
|
fruits[b] = banana
|
|
|
|
fruits[a] = orange
|
2000-01-03 22:26:44 +00:00
|
|
|
</computeroutput>
|
|
|
|
</para>
|
1999-09-21 14:41:01 +00:00
|
|
|
<simpara>
|
2000-01-03 22:26:44 +00:00
|
|
|
See also <function>asort</function>, <function>arsort</function>,
|
|
|
|
<function>ksort</function> <function>sort</function>, and
|
|
|
|
<function>rsort</function>.
|
|
|
|
</simpara>
|
1999-09-21 14:41:01 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
1999-06-06 18:51:02 +00:00
|
|
|
<refentry id="function.ksort">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>ksort</refname>
|
1999-06-28 02:07:12 +00:00
|
|
|
<refpurpose>Sort an array by key</refpurpose>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<funcsynopsis>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>int <function>ksort</function></funcdef>
|
|
|
|
<paramdef>array <parameter>array</parameter></paramdef>
|
|
|
|
</funcprototype>
|
1999-06-06 18:51:02 +00:00
|
|
|
</funcsynopsis>
|
|
|
|
<para>
|
|
|
|
Sorts an array by key, maintaining key to data correlations. This
|
|
|
|
is useful mainly for associative arrays.
|
2000-01-03 22:26:44 +00:00
|
|
|
<example>
|
1999-06-06 18:51:02 +00:00
|
|
|
<title><function>ksort</function> example</title>
|
2000-01-03 22:26:44 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
$fruits = array ("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
|
|
|
|
ksort ($fruits);
|
2000-06-15 08:50:25 +00:00
|
|
|
reset($fruits);
|
|
|
|
while(list($key,$val)=each($fruits)) {
|
|
|
|
echo "$key -> $val\n";
|
1999-06-06 18:51:02 +00:00
|
|
|
}
|
2000-01-03 22:26:44 +00:00
|
|
|
</programlisting>
|
|
|
|
</example>
|
1999-06-06 18:51:02 +00:00
|
|
|
This example would display:
|
|
|
|
<computeroutput>
|
|
|
|
fruits[a] = orange
|
|
|
|
fruits[b] = banana
|
|
|
|
fruits[c] = apple
|
|
|
|
fruits[d] = lemon
|
2000-01-03 22:26:44 +00:00
|
|
|
</computeroutput>
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
<simpara>
|
|
|
|
See also <function>asort</function>, <function>arsort</function>,
|
2000-01-03 22:26:44 +00:00
|
|
|
<function>sort</function>, and <function>rsort</function>.
|
|
|
|
</simpara>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
|
|
|
<refentry id="function.list">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>list</refname>
|
|
|
|
<refpurpose>
|
1999-06-28 02:07:12 +00:00
|
|
|
Assign variables as if they were an array
|
1999-06-06 18:51:02 +00:00
|
|
|
</refpurpose>
|
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<funcsynopsis>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>void <function>list</function></funcdef>
|
|
|
|
<varargs/>
|
|
|
|
</funcprototype>
|
1999-06-06 18:51:02 +00:00
|
|
|
</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>
|
2000-01-03 22:26:44 +00:00
|
|
|
<programlisting role="php">
|
1999-06-06 18:51:02 +00:00
|
|
|
<table>
|
|
|
|
<tr>
|
|
|
|
<th>Employee name</th>
|
|
|
|
<th>Salary</th>
|
|
|
|
</tr>
|
2000-01-03 22:26:44 +00:00
|
|
|
|
1999-06-06 18:51:02 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
$result = mysql($conn, "SELECT id, name, salary FROM employees");
|
|
|
|
while (list($id, $name, $salary) = mysql_fetch_row($result)) {
|
|
|
|
print(" <tr>\n".
|
|
|
|
" <td><a href=\"info.php3?id=$id\">$name</a></td>\n".
|
|
|
|
" <td>$salary</td>\n".
|
|
|
|
" </tr>\n");
|
|
|
|
}
|
|
|
|
|
2000-01-03 22:26:44 +00:00
|
|
|
?>
|
1999-06-06 18:51:02 +00:00
|
|
|
|
2000-01-03 22:26:44 +00:00
|
|
|
</table>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
<para>
|
2000-01-03 22:26:44 +00:00
|
|
|
See also: <function>each</function>, <function>array</function>.
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
|
|
|
<refentry id="function.next">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>next</refname>
|
1999-09-07 20:25:39 +00:00
|
|
|
<refpurpose>
|
|
|
|
Advance the internal array pointer of an array
|
|
|
|
</refpurpose>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<funcsynopsis>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>mixed <function>next</function></funcdef>
|
|
|
|
<paramdef>array <parameter>array</parameter></paramdef>
|
|
|
|
</funcprototype>
|
1999-06-06 18:51:02 +00:00
|
|
|
</funcsynopsis>
|
|
|
|
<para>
|
|
|
|
Returns the array element in the next place that's pointed by the
|
2000-01-03 22:26:44 +00:00
|
|
|
internal array pointer, or false if there are no more elements.
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
<para>
|
1999-09-07 20:25:39 +00:00
|
|
|
<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 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>
|
2000-01-03 22:26:44 +00:00
|
|
|
</warning>
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
<para>
|
|
|
|
See also:
|
|
|
|
<function>current</function>, <function>end</function>
|
2000-01-03 22:26:44 +00:00
|
|
|
<function>prev</function> and <function>reset</function>
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
|
|
|
<refentry id="function.pos">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>pos</refname>
|
1999-06-28 02:07:12 +00:00
|
|
|
<refpurpose>Get the current element from an array</refpurpose>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<funcsynopsis>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>mixed <function>pos</function></funcdef>
|
|
|
|
<paramdef>array <parameter>array</parameter></paramdef>
|
|
|
|
</funcprototype>
|
1999-06-06 18:51:02 +00:00
|
|
|
</funcsynopsis>
|
|
|
|
<simpara>
|
2000-01-03 22:26:44 +00:00
|
|
|
This is an alias for <function>current</function>.
|
|
|
|
</simpara>
|
1999-06-06 18:51:02 +00:00
|
|
|
<para>
|
|
|
|
See also:
|
|
|
|
<function>end</function>, <function>next</function>,
|
2000-01-03 22:26:44 +00:00
|
|
|
<function>prev</function> and <function>reset</function>.
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
|
|
|
<refentry id="function.prev">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>prev</refname>
|
1999-06-28 02:07:12 +00:00
|
|
|
<refpurpose>Rewind the internal array pointer</refpurpose>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<funcsynopsis>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>mixed <function>prev</function></funcdef>
|
|
|
|
<paramdef>array <parameter>array</parameter></paramdef>
|
|
|
|
</funcprototype>
|
1999-06-06 18:51:02 +00:00
|
|
|
</funcsynopsis>
|
|
|
|
<para>
|
|
|
|
Returns the array element in the previous place that's pointed by
|
1999-09-07 20:25:39 +00:00
|
|
|
the internal array pointer, or false if there are no more
|
|
|
|
elements.
|
2000-01-03 22:26:44 +00:00
|
|
|
<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>
|
1999-06-06 18:51:02 +00:00
|
|
|
<para>
|
|
|
|
<function>prev</function> behaves just like
|
|
|
|
<function>next</function>, except it rewinds the internal array
|
2000-01-03 22:26:44 +00:00
|
|
|
pointer one place instead of advancing it.
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
<para>
|
2000-01-03 22:26:44 +00:00
|
|
|
See also: <function>current</function>, <function>end</function>
|
|
|
|
<function>next</function> and <function>reset</function>
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
|
|
|
<refentry id="function.range">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>range</refname>
|
1999-09-07 20:25:39 +00:00
|
|
|
<refpurpose>
|
|
|
|
Create an array containing a range of integers
|
|
|
|
</refpurpose>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<funcsynopsis>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>array <function>range</function></funcdef>
|
|
|
|
<paramdef>int <parameter>low</parameter></paramdef>
|
|
|
|
<paramdef>int <parameter>high</parameter></paramdef>
|
|
|
|
</funcprototype>
|
1999-06-06 18:51:02 +00:00
|
|
|
</funcsynopsis>
|
|
|
|
<para>
|
|
|
|
<function>range</function> returns an array of integers from
|
|
|
|
<parameter>low</parameter> to <parameter>high</parameter>,
|
2000-01-03 22:26:44 +00:00
|
|
|
inclusive.
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
<para>
|
2000-01-03 22:26:44 +00:00
|
|
|
See <function>shuffle</function> for an example of its use.
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
|
|
|
<refentry id="function.reset">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>reset</refname>
|
1999-06-28 02:07:12 +00:00
|
|
|
<refpurpose>
|
|
|
|
Set the internal pointer of an array to its first element
|
|
|
|
</refpurpose>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<funcsynopsis>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>mixed <function>reset</function></funcdef>
|
|
|
|
<paramdef>array <parameter>array</parameter></paramdef>
|
|
|
|
</funcprototype>
|
1999-06-06 18:51:02 +00:00
|
|
|
</funcsynopsis>
|
|
|
|
<para>
|
|
|
|
<function>reset</function> rewinds <parameter>array</parameter>'s
|
2000-01-03 22:26:44 +00:00
|
|
|
internal pointer to the first element.
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
<para>
|
|
|
|
<function>reset</function> returns the value of the first array
|
2000-01-03 22:26:44 +00:00
|
|
|
element.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
See also: <function>current</function>,
|
|
|
|
<function>each</function>, <function>next</function>,
|
|
|
|
<function>prev</function>, and <function>reset</function>.
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
|
|
|
<refentry id="function.rsort">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>rsort</refname>
|
|
|
|
<refpurpose>Sort an array in reverse order</refpurpose>
|
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<funcsynopsis>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>void <function>rsort</function></funcdef>
|
|
|
|
<paramdef>array <parameter>array</parameter></paramdef>
|
|
|
|
</funcprototype>
|
1999-06-06 18:51:02 +00:00
|
|
|
</funcsynopsis>
|
|
|
|
<para>
|
1999-06-28 02:07:12 +00:00
|
|
|
This function sorts an array in reverse order (highest to lowest).
|
|
|
|
<example>
|
1999-06-06 18:51:02 +00:00
|
|
|
<title><function>rsort</function> example</title>
|
2000-01-03 22:26:44 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
$fruits = array ("lemon", "orange", "banana", "apple");
|
|
|
|
rsort ($fruits);
|
2000-06-15 08:50:25 +00:00
|
|
|
reset($fruits);
|
|
|
|
while(list($key,$val)=each($fruits)) {
|
|
|
|
echo "$key -> $val\n";
|
1999-06-28 02:07:12 +00:00
|
|
|
}
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
This example would display:
|
|
|
|
<computeroutput>
|
|
|
|
fruits[0] = orange
|
|
|
|
fruits[1] = lemon
|
|
|
|
fruits[2] = banana
|
|
|
|
fruits[3] = apple
|
|
|
|
</computeroutput>
|
2000-01-03 22:26:44 +00:00
|
|
|
The fruits have been sorted in reverse alphabetical order.
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
<para>
|
2000-01-03 22:26:44 +00:00
|
|
|
See also: <function>arsort</function>,
|
|
|
|
<function>asort</function>, <function>ksort</function>,
|
|
|
|
<function>sort</function>, and <function>usort</function>.
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
|
|
|
<refentry id="function.shuffle">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>shuffle</refname>
|
|
|
|
<refpurpose>Shuffle an array</refpurpose>
|
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<funcsynopsis>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>void <function>shuffle</function></funcdef>
|
|
|
|
<paramdef>array <parameter>array</parameter></paramdef>
|
|
|
|
</funcprototype>
|
1999-06-06 18:51:02 +00:00
|
|
|
</funcsynopsis>
|
|
|
|
<para>
|
1999-09-07 20:25:39 +00:00
|
|
|
This function shuffles (randomizes the order of the elements in)
|
1999-06-06 18:51:02 +00:00
|
|
|
an array.
|
|
|
|
<example>
|
|
|
|
<title><function>shuffle</function> example</title>
|
2000-01-03 22:26:44 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
$numbers = range (1,20);
|
|
|
|
srand (time());
|
|
|
|
shuffle ($numbers);
|
|
|
|
while (list(, $number) = each ($numbers)) {
|
1999-06-28 02:07:12 +00:00
|
|
|
echo "$number ";
|
1999-06-06 18:51:02 +00:00
|
|
|
}
|
2000-01-03 22:26:44 +00:00
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
<para>
|
|
|
|
See also <function>arsort</function>, <function>asort</function>,
|
|
|
|
<function>ksort</function>, <function>rsort</function>,
|
2000-01-03 22:26:44 +00:00
|
|
|
<function>sort</function> and <function>usort</function>.
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
|
|
|
<refentry id="function.sizeof">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>sizeof</refname>
|
1999-06-28 02:07:12 +00:00
|
|
|
<refpurpose>Get the number of elements in an array</refpurpose>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<funcsynopsis>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>int <function>sizeof</function></funcdef>
|
|
|
|
<paramdef>array <parameter>array</parameter></paramdef>
|
|
|
|
</funcprototype>
|
1999-06-06 18:51:02 +00:00
|
|
|
</funcsynopsis>
|
|
|
|
<para>
|
2000-01-03 22:26:44 +00:00
|
|
|
Returns the number of elements in the array.
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
<para>
|
2000-01-03 22:26:44 +00:00
|
|
|
See also: <function>count</function>
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
|
|
|
<refentry id="function.sort">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>sort</refname>
|
|
|
|
<refpurpose>Sort an array</refpurpose>
|
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<funcsynopsis>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>void <function>sort</function></funcdef>
|
|
|
|
<paramdef>array <parameter>array</parameter></paramdef>
|
|
|
|
</funcprototype>
|
1999-06-06 18:51:02 +00:00
|
|
|
</funcsynopsis>
|
|
|
|
<para>
|
1999-09-07 20:25:39 +00:00
|
|
|
This function sorts an array. Elements will be arranged from
|
|
|
|
lowest to highest when this function has completed.
|
1999-06-06 18:51:02 +00:00
|
|
|
<example>
|
|
|
|
<title><function>sort</function> example</title>
|
2000-01-03 22:26:44 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
$fruits = array ("lemon", "orange", "banana", "apple");
|
|
|
|
sort ($fruits);
|
2000-06-15 08:50:25 +00:00
|
|
|
reset($fruits);
|
|
|
|
while(list($key,$val)=each($fruits)) {
|
|
|
|
echo "$key -> $val\n";
|
1999-06-06 18:51:02 +00:00
|
|
|
}
|
2000-01-03 22:26:44 +00:00
|
|
|
</programlisting>
|
|
|
|
</example>
|
1999-06-06 18:51:02 +00:00
|
|
|
This example would display:
|
|
|
|
<computeroutput>
|
|
|
|
fruits[0] = apple
|
|
|
|
fruits[1] = banana
|
|
|
|
fruits[2] = lemon
|
|
|
|
fruits[3] = orange
|
2000-01-03 22:26:44 +00:00
|
|
|
</computeroutput>
|
|
|
|
The fruits have been sorted in alphabetical order.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
See also: <function>arsort</function>,
|
|
|
|
<function>asort</function>, <function>ksort</function>,
|
|
|
|
<function>rsort</function>, and <function>usort</function>.
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
|
|
|
<refentry id="function.uasort">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>uasort</refname>
|
1999-09-07 20:25:39 +00:00
|
|
|
<refpurpose>
|
|
|
|
Sort an array with a user-defined comparison function and
|
|
|
|
maintain index association
|
|
|
|
</refpurpose>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<funcsynopsis>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>void <function>uasort</function></funcdef>
|
|
|
|
<paramdef>array <parameter>array</parameter></paramdef>
|
|
|
|
<paramdef>function <parameter>cmp_function</parameter></paramdef>
|
|
|
|
</funcprototype>
|
1999-06-06 18:51:02 +00:00
|
|
|
</funcsynopsis>
|
|
|
|
<para>
|
1999-09-07 20:25:39 +00:00
|
|
|
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
|
2000-01-03 22:26:44 +00:00
|
|
|
is user-defined.
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
|
|
|
<refentry id="function.uksort">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>uksort</refname>
|
1999-09-07 20:25:39 +00:00
|
|
|
<refpurpose>
|
|
|
|
Sort an array by keys using a user-defined comparison function
|
|
|
|
</refpurpose>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<funcsynopsis>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>void <function>uksort</function></funcdef>
|
|
|
|
<paramdef>array <parameter>array</parameter></paramdef>
|
|
|
|
<paramdef>function <parameter>cmp_function</parameter></paramdef>
|
|
|
|
</funcprototype>
|
1999-06-06 18:51:02 +00:00
|
|
|
</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.
|
1999-06-28 02:07:12 +00:00
|
|
|
|
1999-06-06 18:51:02 +00:00
|
|
|
<example>
|
|
|
|
<title><function>uksort</function> example</title>
|
2000-01-03 22:26:44 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
function mycompare ($a, $b) {
|
1999-06-06 18:51:02 +00:00
|
|
|
if ($a == $b) return 0;
|
|
|
|
return ($a > $b) ? -1 : 1;
|
|
|
|
}
|
2000-01-03 22:26:44 +00:00
|
|
|
$a = array (4 => "four", 3 => "three", 20 => "twenty", 10 => "ten");
|
|
|
|
uksort ($a, mycompare);
|
|
|
|
while (list ($key, $value) = each ($a)) {
|
1999-06-06 18:51:02 +00:00
|
|
|
echo "$key: $value\n";
|
|
|
|
}
|
2000-01-03 22:26:44 +00:00
|
|
|
</programlisting>
|
|
|
|
</example>
|
1999-06-06 18:51:02 +00:00
|
|
|
This example would display:
|
|
|
|
<computeroutput>
|
|
|
|
20: twenty
|
|
|
|
10: ten
|
|
|
|
4: four
|
|
|
|
3: three
|
2000-01-03 22:26:44 +00:00
|
|
|
</computeroutput>
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
<para>
|
2000-01-03 22:26:44 +00:00
|
|
|
See also: <function>arsort</function>,
|
|
|
|
<function>asort</function>, <function>uasort</function>,
|
|
|
|
<function>ksort</function>, <function>rsort</function>, and
|
|
|
|
<function>sort</function>.
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
|
|
|
<refentry id="function.usort">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>usort</refname>
|
1999-09-07 20:25:39 +00:00
|
|
|
<refpurpose>
|
|
|
|
Sort an array by values using a user-defined comparison function
|
|
|
|
</refpurpose>
|
1999-06-06 18:51:02 +00:00
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<funcsynopsis>
|
2000-06-24 07:38:45 +00:00
|
|
|
<funcprototype>
|
|
|
|
<funcdef>void <function>usort</function></funcdef>
|
|
|
|
<paramdef>array <parameter>array</parameter></paramdef>
|
|
|
|
<paramdef>function <parameter>cmp_function</parameter></paramdef>
|
|
|
|
</funcprototype>
|
1999-06-06 18:51:02 +00:00
|
|
|
</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
|
2000-01-03 22:26:44 +00:00
|
|
|
this function.
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
<para>
|
1999-09-07 20:25:39 +00:00
|
|
|
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.
|
1999-06-06 18:51:02 +00:00
|
|
|
<example>
|
|
|
|
<title><function>usort</function> example</title>
|
2000-01-03 22:26:44 +00:00
|
|
|
<programlisting>
|
|
|
|
function cmp ($a, $b) {
|
1999-06-06 18:51:02 +00:00
|
|
|
if ($a == $b) return 0;
|
|
|
|
return ($a > $b) ? -1 : 1;
|
|
|
|
}
|
2000-01-03 22:26:44 +00:00
|
|
|
$a = array (3, 2, 5, 6, 1);
|
|
|
|
usort ($a, cmp);
|
|
|
|
while (list ($key, $value) = each ($a)) {
|
1999-06-06 18:51:02 +00:00
|
|
|
echo "$key: $value\n";
|
|
|
|
}
|
2000-01-03 22:26:44 +00:00
|
|
|
</programlisting>
|
|
|
|
</example>
|
1999-06-06 18:51:02 +00:00
|
|
|
This example would display:
|
|
|
|
<computeroutput>
|
|
|
|
0: 6
|
|
|
|
1: 5
|
|
|
|
2: 3
|
|
|
|
3: 2
|
|
|
|
4: 1
|
2000-01-03 22:26:44 +00:00
|
|
|
</computeroutput>
|
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
Obviously in this trivial case the <function>rsort</function>
|
|
|
|
function would be more appropriate.
|
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
<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>
|
1999-06-06 18:51:02 +00:00
|
|
|
<para>
|
2000-01-03 22:26:44 +00:00
|
|
|
See also: <function>arsort</function>,
|
|
|
|
<function>asort</function>, <function>ksort</function>,
|
|
|
|
<function>rsort</function> and <function>sort</function>.
|
|
|
|
</para>
|
1999-06-06 18:51:02 +00:00
|
|
|
</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
|
1999-12-15 18:45:44 +00:00
|
|
|
sgml-default-dtd-file:"../../manual.ced"
|
1999-06-06 18:51:02 +00:00
|
|
|
sgml-exposed-tags:nil
|
|
|
|
sgml-local-catalogs:nil
|
|
|
|
sgml-local-ecat-files:nil
|
|
|
|
End:
|
|
|
|
-->
|