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
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

You could write a wrapper function that paginates your array.

Turns out it's pretty much array_slice() with default arguments, and an auto offset increment for added flare:

Code: Select all

// FUNCTION:

function paginate($array,&$offset,$perpage=10){
	$offset = $offset?$offset:0;
	if($offset >= count($array)) return FALSE;
	$page = array_slice($array,$offset,$perpage,TRUE);
	$offset+=$perpage;
	return $page;
}

// USAGE:

	// build a test array
	for($i=0;$i<100;$i++){
		$array[$i] = 'item '.$i;
	}
	
	// set your offset variable
	$offset = 25;

	// paginate it some to test it
	while( $a=paginate($array,$offset) ){
		print_r($a);
	}
yanisdon
Forum Newbie
Posts: 18
Joined: Thu Jul 06, 2006 10:44 pm

Post by yanisdon »

That looks promising.

Thanks for the response.
Well, maybe I need to add that I don't want to generate a real paging mechanism.

A little bit of background.

My little app parses a remote RSS file with Magpie. Basically Magpie parses the content of a given RSS file into a PHP object.
So far so good.

The RSS file in question is being generated by a remote Server which is prone to timeout. The RSS file contains dynamic parameter for the total number of items and the pagesize which can be used to manipulate the $_GET parameter in order to bypass the timeout issue.
So again:

1) the array contains 5000 items.

2) start to loop through the array and get the first 10 items (1 -10)

3) do something with those 10 items..

4) now go back and get the next 10 items (11-20)

5) do something with those 10 items..

4) now go back and get the next 10 items (21-30)

and so on..


I'll try your approach.



Cheers.
yanisdon
Forum Newbie
Posts: 18
Joined: Thu Jul 06, 2006 10:44 pm

Post by yanisdon »

That works. Thanks a lot!

Code: Select all

<?php


// FUNCTION:

function paginate($array,&$offset,$perpage=10){
        $offset = $offset?$offset:0;
        if($offset >= count($array)) return FALSE;
        $page = array_slice($array,$offset,$perpage,TRUE);
        $offset+=$perpage;
        return $page;
}

// USAGE:

        // build a test array
        for($i=0;$i<=5000;$i++){
                $array[$i] = 'item '.$i;
        }
       
        // set your offset variable
        $offset = 1;

        // paginate it some to test it
        while( $a=paginate($array,$offset) ){
                print_r($a);
        } 
?>
/**
Array
(
    [1] => item 1
    [2] => item 2
    [3] => item 3
    [4] => item 4
    [5] => item 5
    [6] => item 6
    [7] => item 7
    [8] => item 8
    [9] => item 9
    [10] => item 10
)
Array
(
    [11] => item 11
    [12] => item 12
    [13] => item 13
    [14] => item 14
    [15] => item 15
    [16] => item 16
    [17] => item 17
    [18] => item 18
    [19] => item 19
    [20] => item 20
)
Array
(
    [21] => item 21
    [22] => item 22
    [23] => item 23
    [24] => item 24
    [25] => item 25
    [26] => item 26
    [27] => item 27
    [28] => item 28
    [29] => item 29
    [30] => item 30
)
Array
(
    [31] => item 31

etc, etc,.........
*/
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Don't forget that most arrays start with an offset of zero.

Also, the offset passed to the paginate function MUST be a variable, not a number or a string.
Post Reply