1

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

Allen Liu
  • 29
  • 1
  • 7
  • 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. – Dylan Mar 20 '19 at 17:21
  • Great, this answer might shed some light on your problem: https://stackoverflow.com/a/28217055/213599 – Dylan Mar 20 '19 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. – Allen Liu Mar 20 '19 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. – Dylan Mar 20 '19 at 19:05

1 Answers1

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.

Shoyeb Sheikh
  • 2,659
  • 2
  • 10
  • 19
  • 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. – Allen Liu Mar 22 '19 at 13:38
  • @AllenLiu and how you think to stop the for loop, what will be the condition ? – Shoyeb Sheikh Mar 23 '19 at 04:17
  • 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 – Allen Liu Mar 25 '19 at 20:35