Jump to content

Unusual issue with arrays


papaface

Recommended Posts

Hey

 

I wonder if you could help me.

I have a script that scans a given webpage for the pagination numbers.

 

Firstly I scan the page and load all the page numbers into $info['page'] as an array.

 

I then setup a foreach loop, to go through each element (page number in the array).

 

So the result is like:

Array
(
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
)

 

Element 1 is page 2 etc.

 

In the foreach loop I get it to scan page 2 of the webpage and return the page numbers. I am careful to not add a page number to the array if it already exists, and I also have another array that holds all the page numbers that has been scanned. I have this working fine.

 

The problem I have, is that it is only looping up to page 10, despite the $info['page'] array having

( [9] => 10 [10] => 11 [11] => 12 [12] => 13 ) 

left in it.

Why is this?

 

Any help would be appreciated.

Link to comment
https://forums.phpfreaks.com/topic/219634-unusual-issue-with-arrays/
Share on other sites

foreach($info['Pages'] as $k => $v)
{

	$returned = $km->fetchCatPageInfo($km->selectedcaturl."?page=".$v,array("Page"=>array('<div class="pagination">','>></a></li> </ul></div>')             ));	
	?>
	Page <?php echo $v;?>:<textarea><?php print_r($returned['HTML']); ?></textarea>
	<?php
	$km->mappedpagenumbers[] = $v;

	foreach	($returned['Pages'] as $k2 => $v2)
		{
		if	(!in_array($v2,$km->mappedpagenumbers) && !in_array($v2,$info['Pages']))
			{
			$info['Pages'][] = $v2;
			}
		}
	unset($info['Pages'][$k]);


}

Please see above

Removed it and it stops in the same place. Except it just has more items in the info[pages] array

$info['Pages']:Array ( [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 10 [10] => 11 [11] => 12 [12] => 13 )
mappedpagenumbers:Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 ) 

also this bothers me: $info['Pages'][] = $v2; the code is looping over the array $info['Pages'] and modifying the array at the same time.

 

I wouldn't mess with an array that I am in the process of looping over. if you need to update the array you are looping over, i would create another array within the loop and replace the original array after the loop exits.

also this bothers me: $info['Pages'][] = $v2; the code is looping over the array $info['Pages'] and modifying the array at the same time.

Thats right.

It needs to do this so that it automatically goes through all the elements/pages.

Do you think it'd be better to do:

while (count($info['pages']) > 0)

?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.