2

I have a really strange problem with range(); According to docs :

Create an array containing a range of elements

But when I do :

foreach (range(900,950,1) as $art_id){
    //ob_start();
    //do stuff
    //do a lot more stuff
    echo $art_id;
    //ob_get_clean(); }

or even

$arts_id = range (900, 920);
 foreach ($arts_id as $art_id){
        //ob_start();
        //do stuff
        //do a lot more stuff
        echo $art_id;
        //ob_get_clean(); }

The output is strangly repeating itself in a series like

"900,900,901,900,901,902,900,901,9002,903,900..."

meaning it is comming back to the first ID after each loop.

(1st iteration -> 900
2nd iteration -> 900,901
3rd iteration -> 900,901,902
...)

When I just put a manual array it works perfectly in order and no duplicates :

$arts_id = array(900,901,902,903,904,905,906,907,908,909,910...);

What Am I doing wrong (again ?? )

EDIT I

here is the whole script :

http://pastebin.com/ZHm3ub6n

It is actually a slightly modified version of the slashdot scraping example included in the simplehtmldom script . Nothing special.

It is executed inside WP but OUTSIDE the loop ..

8
  • The 3rd parameter is step and its default value is 1, so it is not necessary to use it in your case. Commented Mar 28, 2013 at 23:37
  • @fedorqui , Thanks . I know , in fact the second example I do not use it . I was just checking why it fails .. Commented Mar 28, 2013 at 23:39
  • I have also been checking and the performance is OK in all cases given. Aren´t you nesting different foreach or changing values inside the loop? Otherwise it sounds very strange. Commented Mar 28, 2013 at 23:40
  • @fedorqui - yep .. a lot of nesting :-) see update for whole script Commented Mar 28, 2013 at 23:50
  • There's no appreciable difference between range() and a manual array with the same contents. Commented Mar 29, 2013 at 0:17

1 Answer 1

3

It must be in the rest of your code, because this works fine. please share more of the script.

It looks like the foreach is nested within a similair foreach,

 $arts_id = range (900, 920);
 foreach ($arts_id as $art_id){
    foreach (range (900,$art_id) as $art_id2){
        echo $art_id2."<br/>";
    }
 }

This produces an output you've described

EDIT

Personally i'd add the the function scraping_slashdot a reset of the variable $ret just in case.

for example: $ret = array();

Currently the echo of $output is within the loop, which creates an output like the following:

Article 1
Article 1, Article 2
Article 1, Article 2, Article 3
etc.

place echo $output outside the loop, or $ouptut = ''; inside the loop.

Sign up to request clarification or add additional context in comments.

3 Comments

I already spotted the echo thing and got it out . It was only debug.. But the loop is really executing multiple times (insert post) and i do not understand why a manual insert of the array will not duplicate also with the echo in the same place ..
If the echo is the problem, why a manual array insert does not give duplicated results ?
of course ! $ret = array(); should bethe right answer .. the scraping_slashdot is adding to array without resetting it . so it is adding to it on each iteration. I still have to verify , but it looks like it is correct. Not an echo or nesting problem. still it will not answer why with manual array insert it works OK...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.