本文へスキップ

[Function] array_slice()

MintState 2009年2月23日 14:38 閲覧 13537

[Function] array_slice()

array array_slice ( array $array , int $offset [, int $length [, bool $preserve_keys ]] )
offset 과 length 인수에 따라 array 로부터 일련의 배열 원소를 반환한다.
예를 들어 배열을 뽑아내고 정렬(usort(), sort())을 한후 정렬된 배열을 일부분 반환 할때 쓸 수 있겠네요.

예)

<?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 "d"
$output = array_slice ($input, 0, 3);   // returns "a", "b", and "c"

// 배열 키의 차이에 주의
print_r(array_slice($input, 2, -1));
print_r(array_slice($input, 2, -1, true));
?>

 

출력 :

Array
(
    [0] => c
    [1] => d
)
Array
(
    [2] => c
    [3] => d
)
共有 X Facebook LINE QR コード

コメント 0 件

最初のコメントを書いてみましょう。