0

Thank you at first. CODE:

 $flag=1;
 foreach( $questionidset as $oneqid)
{

   if($oneqid%23==0)
   {
     if($flag<3)
    {
       array_push($questionidset, 23*$flag);
        $flag++;
    }

   }

}

print_r($questionidset);

QUESTION: how to make the foreach get dynamic $questionidset after being pushed a new element.

Such as, the original $questionidset is {1,2,23}
The output should be : {1,2,23,23,46}

My purpose is that after pushing a new element to the original array named $questionidset, the foreach loop times can get an increment

4
  • 2
    Hi Allen - your $flag variable is getting reset to 1 every time you loop through your array. So $flag will always be 1 by the time you reach your array_push. Commented Mar 20, 2019 at 17:21
  • Great, this answer might shed some light on your problem: stackoverflow.com/a/28217055/213599 Commented Mar 20, 2019 at 18:03
  • Thank you. But it is not a similar one. My purpose is that after pushing a new element to the original array named $questionidset, the foreach loop times can get an increment. Commented Mar 20, 2019 at 18:17
  • Yup, I follow. In that answer it mentions that you cannot add an element to the array within the foreach loop. You'll need to use an additional loop or a different kind of loop. Commented Mar 20, 2019 at 19:05

1 Answer 1

0

Try this,

$flag = 1;
$length = 2;
$questionidset = [1, 2, 23];
for ($i = 0; $i < count($questionidset); $i++) {
    if ($questionidset[$i] % 23 == 0) {
        $questionidset[] = 23 * $flag;
        $flag++;
        if( $flag > $length )
            break;
    }
}

print_r($questionidset);

//This outputs : [1,2,23,23,46]
//if you increase length to 5
//The output will be : [1,2,23,23,46,69,92,115]

Not sure why it does not iterate for n times where n is the number of elements in output, But you can always solve the problem with recursion like below, it iterates exactly n times where n is the number of elements in output.

$flag = 1;
$length = 2;
$questionidset = [1, 2, 23];
$count = count($questionidset);
$start = 0;

function for_loop(&$arr, $count, $i, &$flag, $length) {
    if ($i+1 > $count)
        return;
    if ($arr[$i] % 23 == 0) {
        $arr[] = 23 * $flag;
        $flag++;
        $count++;
        $i++;
        if($flag > $length)
           return;
        else 
           for_loop($arr, $count, $i, $flag, $length);
    }
    $i++;
    for_loop($arr, $count, $i, $flag, $length);
}

for_loop($questionidset, $count, $start, $flag, $length);

print_r($questionidset);

Note:- Some parameters are passed by reference.

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

3 Comments

Thanks for the answer. But I think this code would also end at length of the original array $questionidset. What I am thinking is that after pushing a new element into $questionidset, the condition $i<3 ( in this case) can be changed to $i<4.
@AllenLiu and how you think to stop the for loop, what will be the condition ?
Hi, this code works good. In my working case, I will find out how many children needed to append first. I used a for loop at outermost. Thanks for your answer

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.