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
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

Link to comment
Share on other sites

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 ) 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.