thinkphp中的offset是不是必須和length同時使用
2 回答

素胚勾勒不出你
TA貢獻1827條經(jīng)驗 獲得超9個贊
加了offset不加length的話系統(tǒng)會自動取數(shù)組中元素的個數(shù)來補全,但是不加offset卻加了length則會報錯。
下面大概說一下這里thinkphp是如何實現(xiàn)的:
thinkphp這里是用的數(shù)組截取,也就是array_slice函數(shù),
12 | array array_slice ( array $array , int $offset [, int $length [, bool $preserve_keys ]] ) |
array_slice() 默認將重置數(shù)組的鍵。自 PHP 5.0.2
起,可以通過將 preserve_keys 設(shè)為
TRUE 來改變此行為。
這里因為要保存原來的鍵值,所以需要第四個參數(shù)TRUE的。
模板和實現(xiàn)對應(yīng):
1、正常
1 | < volist name = "lists" id = "list" offset = "0" length = '15' > |
1 | $__LIST__ = array_slice ( $lists ,0,15,true); |
2、正常
1 | < volist name = "lists" id = "list" offset = "0" > |
1 | $__LIST__ = array_slice ( $lists ,0, count ( $__LIST__ ),true); |
3、報錯
1 | < volist name = "lists" id = "list" length = "15" > |
1 | $__LIST__ = array_slice ( $lists ,,15,true); |

瀟瀟雨雨
TA貢獻1833條經(jīng)驗 獲得超4個贊
不是必須的。
如果offset為5,length為10,則輸出5-15條數(shù)據(jù)。
若offset為5,length不寫,則輸出從第五條以后的所有數(shù)據(jù)。
- 2 回答
- 0 關(guān)注
- 791 瀏覽
添加回答
舉報
0/150
提交
取消