Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for a change in variable value in forreach loop

Tags:

loops

foreach

php

So I have a checkbox that sends to a PHP script. Basically, my checkboxes create an array, of course. Seeing it's one of those $var[] ones.

So basically, my array will look like this in code:

$vars = array('12345:0','45678:0','78910:0','3434:1','2345:1');

foreach ($vars as $var) {
    $vinfo = explode(":", $var);
    $vgroup = $vinfo[0];
    $vacct = $vinfo[1];

    // Various function calls with those variables. 
}

Now, as you can see. I am splitting for vinfo and vaccount. In my source. I have two accounts (in this example). Some groups belong to 0 and some groups belong to 1. I want to run the function switchAccount("details"); whenever there's a change in the $vacct variable in the foreach loop. In this case, it should only switch once (seeing 0 is default).

"Account" ($vacct) switches vary based on the user input in the checkbox. But (usually) always goes up one number. Again, depending on admin input.

like image 851
user1687621 Avatar asked Feb 10 '23 07:02

user1687621


1 Answers

Try this

$vars = array('12345:0','45678:0','78910:0','3434:1','2345:1');
$vacct = '';
foreach ($vars as $var)
{
    $vinfo = explode(":", $var);
    $vgroup = $vinfo[0];

    if($vacct != $vinfo[1])
    {
        switchAccount("details");
        $vacct = $vinfo[1];

    }
}
like image 80
Ankur Tiwari Avatar answered Feb 11 '23 20:02

Ankur Tiwari