tailieunhanh - PHP 5 Recipes A Problem-Solution Approach 2005 phần 3
CÁC TÍNH NĂNG NGÔN NGỮ - Mô hình hướng đối tượng mới Khi Zeev Suraski đưa cú pháp hướng đối tượng vào PHP từ cái thời của PHP3 thì những lập trình viên chuyên nghiệp có thể dễ dàng nhận ra chúng giống như một thứ gia vị để truy cập vào các collection. | 4-21 EXTRACTING A PORTION OF AN ARRAY 153 Beginning with PHP you can cause the array returned by this function to preserve the indexes of the original by passing an additional argument of TRUE slice2 array_slice languages 2 count languages - 2 TRUE printf pre Slice 2 n s pre n var_export slice2 TRUE How It Works You can see how this works by examining the output of the previous code and comparing slice1 and slice2 Original array array 0 French 1 German 2 Russian 3 Chinese 4 Hindi 5 Quechua 6 Spanish 7 Hausa Slice 1 array 0 Russian 1 Chinese 2 Hindi 3 Quechua 4 Spanish 5 Hausa Slice 2 array 2 Russian 3 Chinese 4 Hindi 5 Quechua 6 Spanish 7 Hausa 154 4-22 EXTRACTING VALUES FROM ARRAYS WITH EXTRACT You can also use negative values for Soffset and or length. If Soffset is negative then the slice returned will start abs Soffset elements from the end of the array if Slength is negative then the slice will end abs Slength elements from the end of the array. For instance given the following slice3 array_slice Slanguages -6 -2 TRUE then slice3 will be identical to slice2 from the previous example. Finally you can obtain a slice consisting of all elements of an array beginning with the element at index Soffset all the way through to the end of the array simply by omitting the length argument. Assuming that Slanguages is the same array as defined in the last two examples then the following last3 array_slice languages -3 printf p Last 3 s p n var_export last3 TRUE will produce this output Last 3 array 0 Quechua 1 Spanish 2 Hausa Note that if you want to preserve the original keys you must supply Slength as well as preserve_keys as using 0 or any value that converts to 0 such as an empty string or NULL for Slength will return an empty array. To get a slice consisting of the last three elements in Slanguages while preserving the keys you would need to use something like this Slast3 array_slice Slanguages -3 3 TRUE printf p Last 3 s p n var_export Slast3 TRUE This will .
đang nạp các trang xem trước