0

I have a counter variable that is for the id or number of an item in an array. That and the content to be added to an array takes up a position in another array. This other array is thus a 2D array.

Basically what I am doing is taking content from one array, which eventually will be dynamically created and added to, and putting that into another array. And then putting that array into a storage array. For what I am doing, I have to do it this way. Apologies.

What I'm wondering if why my counter variable that increment only increments once as is equal to one when I print_r the array that it and the content i'm adding is a part of.

When I run this code, the structure I should see is:

1, 1's content
2, 2's content
3, 3's content

But what I am seeing is:

1, 1's content
1, 2's content
1, 3's content

Why isn't my counter variable who's value is later given to $id not incrementing and how can I get it to increment. The structure that is set up with the taking from one array, constructing another, and putting that into another array, and then recursively adding the rest of the content pretty much has to stay. I don't have much freedom to change the code. I just can't figure out why the counter variable isn't incrementing.

Here is the code:

$counter = 0;
$added_text = array();
$addMe = array("orange is the keyword of the day. Tomorrows is mop.", "I do not think you understnad how much I want it. I need it and it will happen.", "I love all sorts of music. Do I consider it a gift, I am not sure. That is all I know.");

function thing($contents, $addMe)
{
    $counter++;
    $text = strip_tags($contents);

    $id = $counter;
    $content = array(
        'id'      => $id,
        'content'     => $text
    );

    print_r($content);
    echo "<br /><br />";
    array_push($added_text, $content);

        foreach($addMe as $text){
            if(!in_array($added_text, $text)){
                sleep( 1 );
                thing($text, $addMe);
            }
        }
}

thing('hello i am the text 1 as in the text of the first document', $toAdd);
8
  • 1
    The value that you define initially as $counter is out of scope in the thing() function
    – Mark Baker
    Apr 15, 2013 at 23:05
  • variable scope issue
    – user557846
    Apr 15, 2013 at 23:05
  • add global $counter; as first line in your function thing(...)
    – MatRt
    Apr 15, 2013 at 23:09
  • 1
    global is nasty, don't use it unless you really have to
    – user557846
    Apr 15, 2013 at 23:10
  • serious example on how to pass on a counter by reference for recursive functions
    – dbf
    Apr 15, 2013 at 23:13

1 Answer 1

1

You have to keep $counter in scope, every time you call thing $counter is uninitialized, so is "0"ish, then you call counter++, which sets it to 1.

function thing($contents, $addMe, $counter=0)
{
 $counter++;

...


foreach($addMe as $text){
        if(!in_array($added_text, $text)){
            sleep( 1 );
            thing($text, $addMe, $counter);
        }
    }
5
  • just set counter as a parameter with default zero? $counter = 0
    – dbf
    Apr 15, 2013 at 23:15
  • Oh okay, thank you for explaining. Even though $counter in initialised outside of the function it is unititalised and reinitialised each time the function is called? what does $counter ?: 0; do? I have never seen that notation.
    – Irfan Mir
    Apr 15, 2013 at 23:18
  • @dbf I could do that, but I don't want to have any unnecessary parametres if I can avoid them.
    – Irfan Mir
    Apr 15, 2013 at 23:19
  • @IrfanMir the notation can also be read as $counter = $counter ? $counter : 0;, if $counter is set, then return it's value, if not set it to zero. The parameter is there anyway, the check if the parameter is set or not is pointless, you have to call thing($contents, $addMe, 0); to avoid the missing argument error. Setting it to default 0 will not ;)
    – dbf
    Apr 15, 2013 at 23:22
  • Yeah, the first time I read it I didn't realize it was calling itself, that counter ?: 0 was some leftover code. Updated to set default value.
    – dave
    Apr 15, 2013 at 23:49

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.