loop through an array in steps

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
yanisdon
Forum Newbie
Posts: 18
Joined: Thu Jul 06, 2006 10:44 pm

loop through an array in steps

Post by yanisdon »

Hi,
I want to create a loop which iterates through an array in steps of 10

Let's assume we have an array.

Code: Select all

Array
(
    [0] => Array
        (
            [title] => Title one
            [link] => http://www.mylink.com
            [description] => This is a test description
            [dot] => Array
                (
                    [source] => test source
                    [relevance] => 1.0
                    [metadata_score] => 0.3505949
                    [metadata_collection] => Test collection
                    [metadata_mimetype] => text/html
                    [metadata_size] => 0
                    [metadata_url] => http://www.test.com
                )

            [summary] => This is just a test summary
        )

    [1] => Array
        (
            [title] => Title one
            [link] => http://www.mylink.com
            [description] => This is a test description
            [dot] => Array
                (
                    [source] => test source
                    [relevance] => 1.0
                    [metadata_score] => 0.3505949
                    [metadata_collection] => Test collection
                    [metadata_mimetype] => text/html
                    [metadata_size] => 0
                    [metadata_url] => http://www.test.com
                )

            [summary] => This is just a test summary
        )
etc, etc..
So far so good.

Now let's assume the array contains aprox. 5000 items and this value would be represented by a variable called $totaValue

What I really like to be doing here is to loop through the array in steps of 10 and by doing that to create some sort of paging mechanism in order to avoid any impacts triggered by server timeout's etc.

Say I've got these values in $arr_param[]:

Code: Select all

[totalresults] => 200
    [startindex] => 1
    [itemsperpage] => 10 //$arr_param['itemsperpage']
So I'd need to create a loop or a function which loops through the entire array and iterates 10 times ($arr_param['itemsperpage']).

After that go back and procede with the next 10 iterations and so on.

In order to be able to achieve this I'd need to set the $arr_param[startindex] to 11, next time to 21 and next to 31 and so on.. :

How can this be done?

Any ideas?

Cheers
Post Reply