你好我想用for循环对数组进行分页,但问题是我的数组键不像(1,2,3,4,5 ......等),
for ($x=0; $x<$members_per_page; $x++) {
$position = (int) ($currentPage-1) * $members_per_page + $x;
$member = get_userdata($members[$position]);
blackfyre_clan_members_links($member,$post_meta_arr, $post_id, $isleader);
if ( $position >= $last_key ) break;
}
我的数组内容是:
Array
(
[4] => Array
(
[boid] => 4
[cr_nickname] =>
)
[564] => Array
(
[boid] => 564
[cr_nickname] =>
)
) .... ETC
答案 0 :(得分:0)
您可以使用array_values
强制您的数组使用顺序键,如下所示:
for ($x=0; $x<$members_per_page; $x++) {
$position = (int) ($currentPage-1) * $members_per_page + $x;
$member = get_userdata(array_values($members)[$position]);
blackfyre_clan_members_links($member,$post_meta_arr, $post_id, $isleader);
if ( $position >= $last_key ) break;
}
然而,(可能)更好的分页方式是:
$offset = (int) ($currentPage-1) * $members_per_page;
$slice = array_slice ($members, $offset, $members_per_page,true);
array_walk($slice, function ($member) use ($post_meta, $post_id, $isleader) {
blackfyre_clan_members_links($member,$post_meta_arr, $post_id, $isleader);
});
答案 1 :(得分:0)
只需使用foreach($ member as $ member){}而不是