首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >分页和foreach循环亚马逊s3

分页和foreach循环亚马逊s3
EN

Stack Overflow用户
提问于 2011-10-31 21:25:26
回答 2查看 451关注 0票数 1

我正在尝试用亚马逊s3实现分页,这是我可以设置foreach何时开始和何时结束的方法吗?

例如,我有以下来自amazon的输出。这是我的print_r($files)的输出;

代码语言:javascript
复制
Array
(
    [Computer Fix Files/] => Array
        (
            [name] => Computer Fix Files/
            [time] => 1296249553
            [size] => 0
            [hash] => d41d8cd98f00b204e9800998ecf8427e
        )

    [Computer Fix Files/3DP_Net_v1101.exe] => Array
        (
            [name] => Computer Fix Files/3DP_Net_v1101.exe
            [time] => 1296254834
            [size] => 33672146
            [hash] => 068b68fdafe6a8e55020dbbc152b3a26
        )

    [Computer Fix Files/DriverEasy_Setup.exe] => Array
        (
            [name] => Computer Fix Files/DriverEasy_Setup.exe
            [time] => 1296255133
            [size] => 1838760
            [hash] => 4f7d0cfe38f62637b2a4c1d7226a2821
        )

    [Computer Fix Files/Dropbox 0.7.110.exe] => Array
        (
            [name] => Computer Fix Files/Dropbox 0.7.110.exe
            [time] => 1296255148
            [size] => 13525424
            [hash] => 843135bb60735f8a40810555ee960d5d
        )

    [Computer Fix Files/GoodSync-Setup.exe] => Array
        (
            [name] => Computer Fix Files/GoodSync-Setup.exe
            [time] => 1296255257
            [size] => 6124120
            [hash] => f9b197072675bb930d529080563671d0
        )

    [Computer Fix Files/ccsetup302.exe] => Array
        (
            [name] => Computer Fix Files/ccsetup302.exe
            [time] => 1296255107
            [size] => 2976440
            [hash] => 8142275100b3de92db382a0deb3a24d5
        )

    [Computer Fix Files/driver-ml-dlan-usb-windows-r2.exe] => Array
        (
            [name] => Computer Fix Files/driver-ml-dlan-usb-windows-r2.exe
            [time] => 1296255131
            [size] => 262144
            [hash] => 4e673fbe72579e3eaeccd2e3d8c22e46
        )

    [Computer Fix Files/installer_driver_hercules_dj_console_mk2_pc_2009_English.exe] => Array
        (
            [name] => Computer Fix Files/installer_driver_hercules_dj_console_mk2_pc_2009_English.exe
            [time] => 1296255306
            [size] => 2844911
            [hash] => c52f7500a8c3be68c62fd500ca0cb211
        )

    [Computer Fix Files/jre-6u21-windows-i586-iftw-rv.exe] => Array
        (
            [name] => Computer Fix Files/jre-6u21-windows-i586-iftw-rv.exe
            [time] => 1296255329
            [size] => 875296
            [hash] => dfccbb06ed411e0c006f05bcb1bdf7c2
        )

    [Computer Fix Files/m4a-to-mp3-converter.exe] => Array
        (
            [name] => Computer Fix Files/m4a-to-mp3-converter.exe
            [time] => 1296255336
            [size] => 5461664
            [hash] => 4bb68b384902f3f4fcc78e69d795e82d
        )

    [Computer Fix Files/software-dlan-windows-v20.exe] => Array
        (
            [name] => Computer Fix Files/software-dlan-windows-v20.exe
            [time] => 1296255380
            [size] => 2393336
            [hash] => 4034845466edd280e3241ff93c61bfde
        )

    [Music/] => Array
        (
            [name] => Music/
            [time] => 1296576896
            [size] => 0
            [hash] => d41d8cd98f00b204e9800998ecf8427e
        )

    [Music/TheBeautifulSouth/] => Array
        (
            [name] => Music/TheBeautifulSouth/
            [time] => 1296610421
            [size] => 0
            [hash] => d41d8cd98f00b204e9800998ecf8427e
        )

    [Music/TheBeautifulSouth/1-01 Song for Whoever (Single Versio.m4a] => Array
        (
            [name] => Music/TheBeautifulSouth/1-01 Song for Whoever (Single Versio.m4a
            [time] => 1296610421
            [size] => 8667378
            [hash] => c0a7ba2388267a542049369bf90e7dd1
        )

    [Music/TheBeautifulSouth/1-02 A Little Time.m4a] => Array
        (
            [name] => Music/TheBeautifulSouth/1-02 A Little Time.m4a
            [time] => 1296610491
            [size] => 6467370
            [hash] => c9ca14f142709a90196ab9b447ee83e1
        )
)

我想从foreach中的第4个元素开始,并在第6个元素停止,只显示它们之间的元素。

代码语言:javascript
复制
$i = 0;

$start = 4;

$stop = 6;

    foreach($files as $v){ 

      if (++$i == $start)

    echo $v['name'];

      if (++$i == $stop) break; 

    }

有没有人可以帮我实现这个功能?

EN

回答 2

Stack Overflow用户

发布于 2011-10-31 21:42:45

它非常难看,但它可以工作(如果您想显示第4项和第5项):

代码语言:javascript
复制
reset ($files);
for($i = 0; $i < 3; $i++) {
    next($files);
}
for ($i = 0, $current = current($files); $i < 2 && false !== $current; $i++, $current = next($files)) {
    echo $current['name'];
}

如果你是一个糟糕的程序员,你可以使用这个!我认为你可以用迭代器做一些漂亮的事情。

编辑:或者你也可以尝试这样做:(有点像你的代码)

代码语言:javascript
复制
$i = 0;
$start = 4;
$stop = 6;
foreach($files as $v){ 
  if (++$i >= $start) echo $v['name'];
  if ($i == $stop) break;
}
票数 0
EN

Stack Overflow用户

发布于 2011-10-31 22:00:18

你必须让你的限制器动态化。您还需要2个不同的计数器来进行分页。一个计数器从零开始...下一步从第*页开始。这两个循环需要在每个循环中加1:

代码语言:javascript
复制
$c=0;
$p=$_GET[page] * 25;
$l=$p + 25;
foreach($array as $obj){
$c++; $p++;
if ($p==$l) break;
if ($c>$p) echo $obj;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7954360

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档