分页和foreach循环亚马逊s3

时间:2011-10-31 13:25:26

标签: php arrays for-loop foreach

我正在尝试用亚马逊s3实现分页,这是他在foreach开始和完成时可以设置的方式吗?

示例i从亚马逊获得以下输出。这是我的print_r($ files);

的输出
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个元素中停止,仅显示其中的元素。

$i = 0;

$start = 4;

$stop = 6;

    foreach($files as $v){ 

      if (++$i == $start)

    echo $v['name'];

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

    }

有人可以帮我解决一下如何实现这个???

2 个答案:

答案 0 :(得分:0)

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

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'];
}

如果你是一个肮脏的程序员,你可以使用它!我认为你可以用迭代器做一些美好的事情。

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

$i = 0;
$start = 4;
$stop = 6;
foreach($files as $v){ 
  if (++$i >= $start) echo $v['name'];
  if ($i == $stop) break;
}

答案 1 :(得分:0)

你必须使限制器动态化。您还需要2个不同的分页计数器。 一个计数器从零开始......下一个计数器从PAGE *结果开始。这两个需要在每个循环中添加1个:

$c=0;
$p=$_GET[page] * 25;
$l=$p + 25;
foreach($array as $obj){
$c++; $p++;
if ($p==$l) break;
if ($c>$p) echo $obj;
}