I need to iterate through an array, starting with the first element, in chunks of, say, 5, then break off and do something else with the array, then go back to where I left off.

I'm confused about how the array counter, using next, or current etc. would work. How can I reset the current counter to be where it was before it was potentially messed about with some other operation?

I'm not sure if I've explained that well. So, say I want to start at an element of an array that isn't the beginning, (with a stored key) then iterate through the next five elements, then store where I got to for the next time. Does that make sense? The array in question doesn't use incremental numbers [1,2,3] as keys, by the way, it's more random than that.

Hope someone understands what I'm trying to do and can give me a few pointers. All the examples I've managed to find are for iterating complete arrays.

Thanks.

I think this is going to depend more on how your array is actually structured. Post up and example of the array your iterating and a more detailed example of the result.

Hi,
The array is loaded from an api call, and uses 6 (or 8) digits as keys. I'm not ordering the keys after the load, as I want the default ordering for other reasons, though every time I've looked the key numbers have been in order. I don't want to rely on it, though.

Basically, I want to iterate through the main array elements in chunks of five for one operation type, and 12 for another. I could just reset the counters, but that seemed a bit inefficient: it's probably the easiest way to do it, though.

I've just read somewhere about the array_keys and array_chunk functions. I think it may well work if I create two other arrays of the keys and then iterate those?

I need to give it more thought, but my knowledge of what's available in php functions is sadly lacking, I'm afraid.

This code fragment illustrates what I wanted it to, i.e. cycle through the same base array ($wholeArray), with different "chunk" sizes. This particular bit of test code carries on cycling through at a rate of 3 elements, and stops once the 2 element cycle is complete. It does work, though any criticisms or ideas for improvement would be welcomed.

$wholeArray = array('k1'=>'red','k2'=>'orange','k3'=>'yellow','k4'=>'green','k5'=>'blue','k6'=>'indigo','k7'=>'violet',
'k21'=>'red2','k22'=>'orange2','k23'=>'yellow2','k24'=>'green2','k25'=>'blue2','k26'=>'indigo2','k27'=>'violet2' );

$minichunks = array_chunk(array_keys($wholeArray), 2 , true);
$biggerchunks = array_chunk(array_keys($wholeArray), 3 , true);
  
while(key($minichunks) !== null){
	
	//print out current value
	foreach (current($minichunks) as $key => $value ){
		echo "minichunks value: " . $value. " references: ". $wholeArray[$value]. chr(13) . chr(10);
	
	}
	next($minichunks);
	
	if (key($biggerchunks) !== null) {
		foreach (current($biggerchunks) as $key => $value ){
			echo "biggerchunks value" . $value. " references: ". $wholeArray[$value].chr(13) . chr(10);
		}
		next($biggerchunks);
	}
	else {
		reset($biggerchunks);
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.