4

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.

2 Answers 2

9

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];

    }
}
5
  • That works if the only variables are either 0 or 1. Sometimes they go as high as 100. I just put a limited number of them. But yeah, that will work if switching from 0 to another value. But the switching can happen more than that. It can switch from 1 for a while, then 2 for a while longer ,etc. Aug 13, 2015 at 3:03
  • @user1687621 $vacct gets updated with the new number when it's no longer equal to the current one, so it should keep going past 0 and 1. Have you tried it?
    – Bono
    Aug 13, 2015 at 3:31
  • Yes, @Bono Completely agree with you. Aug 13, 2015 at 4:10
  • 1
    @user1687621 It always update $vacct, whenever it found that the previous value inside $vacct is not equal to the current value (which is $vinfo[1]). Aug 13, 2015 at 4:12
  • @AnkurTiwari Yeah I've tested both your and my own answer, both work perfectly.
    – Bono
    Aug 13, 2015 at 6:51
0

I've replaced your foreach for a normal for so you can get the information of the "previous" iteration. This way you can check if there has been a change in $vacct.

for($i = 0; $i < count($vars); $i++){
    $vinfo = explode(":", $vars[$i]);
    $vgroup = $vinfo[0];
    $vacct = $vinfo[1];

    if($i > 0){ //Make sure we don't get a -1 index
        $previous = $vars[$i - 1]; //Get previous info
        $previous_info = explode(':', $previous);
        $previous_vacct = $previous_info[1];

        if($previous_vacct != $vacct){
            //Do switching function
        }
    }
}
2
  • THat code seems to be defective. As it tries to do the account switching even when the vacct variable doesn't even change. Aug 13, 2015 at 4:07
  • @user1687621 have you tried running these code snippets? My code outputs (with an added 12356:3 in the $vars array) 1 & 3 once. @AnkurTiwari 's code outputs 0, 1 & 3 once. So both answers here are tested and work. What exactly isn't working for you, because this should work.
    – Bono
    Aug 13, 2015 at 6:50

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.