Tải bản đầy đủ (.pdf) (5 trang)

PHP Developer''''s Dictionary- P24 doc

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (363.38 KB, 5 trang )

PHP Developer’s Dictionary
IT-SC book
115

$array1 = array(1,2,"two"=>3);
$array2 = array("one"=>1,"two"=>2);
$array3 = array_merge ($array1,$array2);
//$array3 =(1,2,"two"=>2,"one"=>1)


array_merge_recursive()
Syntax

array array_merge_recursive(array array1, array array2, [ ])


Description
The
array_merge_recursive()
function , which was added in PHP 4.0, appends
multiple arrays together to form one single array. If one of the array parameters
contains further arrays, it is also merged.

$array1 = array ("type" => array ("values" => "long"), 1);
$array2 = array (2, "type" => array ("values" => "int","blob");
$array3 = array_merge_recursive ($array1, $array2);
//$array3 = ("type" => array("values"=>array("int","blob"),"long"),1,2)


array_multisort()
Syntax



array array_multisort(array ar1 [, mixed arg [, mixed [, array
]]])


Description
The
array_multisort()
function, which was added in PHP 4.0b4, is used to sort
multiple arrays as well as multidimensional arrays. The first parameter must be an
array, but subsequent parameters can be either an array or a sorting flag. Sort order
flags are either
SORT_ASC
or
SORT_DESC
, indicating a sort in ascending or descending
order, respectively. Possible sort order flag types include
SORT_REGULAR
,
SORT_STRING
, and
SORT_NUMERIC
. The defaults are
SORT_ASC
and
SORT_REGULAR
, and
the flags apply to only the previously specified array—not every array in the
parameter list.
array_pad()

PHP Developer’s Dictionary
IT-SC book
116
Syntax

array array_pad(array input, int pad_size, mixed pad_value)


Description
The
array_pad()
function , which was added in PHP 4.0b4, expands the input
array
with pad_value to reach the pad_size . If the pad_size is positive, padding occurs
on the right, and padding occurs on the left if the pad_size
is negative. If the input
array size is greater than the pad_size
, no padding takes place.
array_pop()
Syntax

mixed array_pop(array array)


Description
The
array_pop()
function, which was added in PHP 4.0, removes and returns the
last element from array . This allows an array to act as a stack data structure with
the stack top at the end of the array.


$array1 = array (1,2,3);
$top = array_pop($array1);//$top = 3, $array1=(1,2)


array_push()
Syntax

int array_push(array array, mixed var, [ ])


Description
The
array_push()
function, which was added in PHP 4.0, appends the passed-in
variables to the end of the array
. This allows the array to act as a stack or queue
data structure. Items can be either pushed onto the stack or enqueued at the end of
the queue.

$array1 = array (1,2);
PHP Developer’s Dictionary
IT-SC book
117
array_push ($array1,3);//$array1 = (1,2,3)


array_rand()
Syntax


int array_rand(array
input
[, int
num_req
])


Description
The array_rand() function, which was added in PHP 4.0, randomly picks a key or
keys from the input array. If num_req is
1
or not specified, one key will be returned;
if num_req
is greater than
1
, an array with the keys will be returned. The
srand()

function should be called to generate a new random seed before using this function.
array_reverse()
Syntax

array array_reverse(array array)


Description
The
array_reverse()
function, which was added in PHP 4.0b4, returns an array that
is the passed-in

array
with its element order reversed.
array_shift()
Syntax

mixed array_shift(array array)


Description
The
array_shift()
function, which was added in PHP 4.0, removes the first element
of the array and returns it. This could be used to implement a queue data structure
in order to dequeue items from the queue.

$array1 = array (1,2,3);
$front = array_shift($array1);//$front = 1, $array1=(2,3)
PHP Developer’s Dictionary
IT-SC book
118


array_slice()
Syntax

array array_slice(array array, int offset, int [length] )


Description
The

array_slice()
function, which was added in PHP 4.0, returns a subset of the
parameter array beginning at the offset and extending for the length . If offset
is positive, the subset is based on the start of the array. If offset is negative, the
subset is based on the end of the array. The length parameter is optional, and when
positive, results in the subset containing the length number of elements. When
length
is negative, it indicates the subset should stop length
away from the end of
the array. If length is omitted, the subset will contain everything from the offset
until an array boundary has been reached.

$array1 = array (1,2,3,4,5);
$slice = array_slice($array1,1,3);//$slice = (2,3,4)


array_splice()
Syntax

array array_splice(array inpt, int offset, int [length] , array
[replacement])


Description
The
array_splice()
function, which was added in PHP 4.0, removes a subset of
input
bounded by the offset
and length

parameters. Optionally, the elements can
be replaced with replacement
. If offset
is positive, the offset
is measured from
the beginning of input . If offset is negative, it is measured from the end of input
.
If the optional length
parameter is omitted, removal occurs from offset
to a
boundary of
input
. If length
is positive, the corresponding number of elements will
be removed. If length
is negative, the last element of the removed portion of the
input
array will be length
number of elements from the end of the array. To
remove everything from the offset
to the end of the array, specify
count($input)

as the length
parameter.
PHP Developer’s Dictionary
IT-SC book
119
The optional replacement
array will be put in place of any deleted elements. If

offset and length don't account for the removal of any elements, replacement is
inserted at the offset location. When replacement is just one element and not an
array itself, it is not necessary to place an
array()
around it.

$array1 = array (1,2,3,4,5);
$array2 = array (4,3,2);
$array3 = array_splice($array1,1,3,$array2);//$array3 = (2,3,4),$array1
= (1,4,3,2,5)


array_unique()
Syntax

int array_unique(array array)


Description
The
array_unique()
function, which was added in PHP 4.0.1, returns an array that
is the array
parameter with any duplicates removed.
array_unshift()
Syntax

int array_unshift(array array, mixed var,[, mixed ])



Description
The
array_unshift()
function, which was added in PHP 4.0, adds to the array
any
of the parameters passed in to the front of the array. The return value is the number
of elements prepended to array .
array_values()
Syntax

array array_values(array input)


Description

×