Instance
Starts with the second element of the array and returns all elements until the end of the array:
<?php$a=array ("Red", "green", "blue", "yellow", "brown");p Rint_r (Array_slice ($a, 2));? >
Definition and usage
The Array_slice () function returns the selected part of the array.
Note: If the array has a string key name, the returned array will retain the key name (see Example 4).
Grammar
Array_slice (Array,start,length,preserve)
Parameters |
Describe |
Array |
Necessary. Specifies the array. |
Start |
Necessary. Numerical. Specifies the starting position of the element to be removed. 0 = first element. If the value is set to a positive number, it is taken from the previous start. If the value is set to a negative number, the start absolute value is taken from the back forward. 2 means starting with the second-to-last element of the array. |
Length |
Optional. Numerical. Specifies the length of the returned array. If the value is set to an integer, the number of elements is returned. If the value is set to a negative number, the function terminates the fetch at such a distance from the end of the example array. If the value is not set, returns all elements starting at the position set by the start parameter until the end of the array. |
Preserve |
Optional. Specifies whether the function retains the key name or resets the key name. Possible values:
|
Technical details
return value: |
Returns the selected part of the array. |
PHP version: |
4 + |
Update log: |
The preserve parameter was added in PHP 5.0.2. |
More examples
Example 1
Starts with the first element of the array and returns two elements:
<?php$a=array ("Red", "green", "blue", "yellow", "brown");p Rint_r (Array_slice ($a,));? >
Example 2
Use the negative start parameter:
<?php$a=array ("Red", "green", "blue", "yellow", "brown");p Rint_r (Array_slice ($a, -2,1));? >
Example 3
With the preserve parameter set to true:
<?php$a=array ("Red", "green", "blue", "yellow", "brown");p Rint_r (Array_slice ($a, 1,2,true));? >
Example 4
With string and integer key names:
<?php$a=array ("A" = "red", "b" = "green", "c" = "Blue", "d" = "yellow", "e" = "Brown");p Rint_r (Array_ Slice ($a,)); $a =array ("0" = "Red", "1" = "green", "2" = "Blue", "3" = "Yellow", "4" = "Brown");p Rint_r (Array_slice ($a,)); >
The paging method implemented by the Array_slice function is very useful and is shared as follows:
<?php//assumes a two-dimensional array of result sets: $arr = Array (' name ' = ' name1 ', ' sex ' = ' sex1 ', ' job ' = ' job1 '), AR Ray (' name ' = ' name2 ', ' sex ' = ' sex2 ', ' job ' = ' job2 '), Array (' name ' = ' = ' name3 ', ' sex ' = ' sex3 ') , ' job ' = ' job3 '), Array (' name ' = ' name4 ', ' sex ' = ' sex4 ', ' job ' = ' job4 '), Array ( ' Name ' = ' name5 ', ' sex ' = ' sex5 ', ' job ' = ' job5 ', Array (' name ' = ' name6 ', ' sex ' = ' sex6 ', ' Jo B ' = ' job6 '), Array (' name ' = ' name7 ', ' sex ' = ' sex7 ', ' job ' = ' job7 '), Array (' Nam E ' = ' name8 ', ' sex ' = ' sex8 ', ' job ' = ' job8 '), Array (' name ' = ' = ' name9 ', ' sex ' = ' sex9 ', ' job ' = > ' job9 '), Array (' name ' = ' name10 ', ' sex ' = ' sex10 ', ' job ' = ' job10 '), Array (' name ' = ' name11 ', ' sex ' = ' sex11 ', ' job ' = ' job11 '), Array (' name ' = ' name12 ', ' sex ' = ' sex12 ', ' Jo B ' = ' job12 '),); Calculates the total number of record bars $num = count ($arr); Specify the number of bars per page $perpage = 3; Count pages $pages = ceil ($num/$perpage); Echo $num, $perpage, $pagecount; exit; if (Is_numeric ($_request[' page ')) {if ($_request[' page ']<1) {$page = 1; }elseif ($_request[' page ']> $pages) {$page = $pages; }else{$page = $_request[' page '); }}else{$page = 1; } $start = ($page-1) * $perpage; $newpage = Array_slice ($arr, $start, $perpage, true); Print_r ($newpage); exit;?> <table cellpadding= "0" cellspacing= "0" border= "1" > <tr> <td>name </td> <td>sex</td> <td>job</td> </tr> <?php foreach ($newpage as $k = $v) {?> <tr> <td><?php echo $v [' name '];?></td> <td><?php echo $v [' sex '];? >< ;/td> <td><?php echo $v [' job '];?></td> </tr> <?php}?> </table> <?php if ($ page>1) {echo "<a href= '? page=1 ' > Home </a>"; echo "<a href="? PagE= ". ($page-1). "' > Prev </a> "; }if ($page < $pages) {echo "<a href= '? page=". $page + 1). "' > Next </a> "; echo "<a href= ' page=". $pages. "' > End </a> "; }?>