简体   繁体   中英

PHP: Parsing an Associative array to compare every two values within the array

Below I am trying to parse an Associative Array.

The array looks like:

$array1 = [Date1 => Time1, Date2 => Time2, Date3 => Time3,……..]

Here, when I parse through an array I want to perform operations on every two elements of array. ie Date1Time1 to compare with Date2Time2, Date3Time3 with Date4Time4 and so on.

So below I am parsing an array using while loop and passing the values to the function creating_detailed_array() .

while (list($var, $val) = each($array1)) {
    if($GLOBALS['counter'] < 4) {
        creating_detailed_array($var,$val);
    } else {
        $GLOBALS['counter'] = 1;
        creating_detailed_array($var,$val);
    }
}   

Assumption1: I have defined $GLOBALS['counter'] as global counter with value 1

Assumption2: Here Date1, Time1, Date2, Time2 are the variables which I have defined at the top of the PHP page considering the global scope as follows.

Below is the called function:

//Global Variables
$Date1 = "";
$Time1 = "";
$Date2 = "";
$Time2 = "";


function creating_detailed_array(&$var,&$val)
{
    //Below I am creating variable named Var1 and Var2 dynamically to store the values of Date1 and Time1 and so on
    ${'Var' . $GLOBALS['counter']} = $var;
    ${'Var' . ($GLOBALS['counter'] + 1)} = $val;
    if($GLOBALS['counter'] = 1) {
        //Trying to store the values in the global variables Date1 and Time1 for future use
        $GLOBALS["Date1"] = $Var1;
        $GLOBALS["Time1"] = $Var2;
    } else {
        //Trying to save the values in global variables Date2 and Time2 for future use.
        $GLOBALS["Date2"] = $Var3;
        $GLOBALS["Time2"] = $Var4;
    }
    echo '<br> Value of Variable 1 : ' . $GLOBALS["Date1"] . 'Value of Variable 2 : ' . $GLOBALS["Time1"] . 'Value of Variable 3 : ' . $GLOBALS["Date2"] . 'Value of Variable 4 : ' . $GLOBALS["Time2"] . '<br>';

    $GLOBALS['counter'] = $GLOBALS['counter'] + 2;     
}

Now the issue here is, when the parsing done first time into the code, I am getting correct values stored in Date1 and Time1, when loop parses with values Date2 and Time2, then the values of Date1 and Time1 are getting set to null.

As I want to compare first element of Assoc array with second, third with fourth and so on.

I want to have Date1 and Time1 and Date2 and Time2 values stored in the variables for future use for comparison. But for each parse I am getting either of the pair getting set.

Here I suspect that I am assigning the global variable incorrectly.

It is bit complex to explain. Let me know if you need more information. The PHP version I am using is 5.5

Thanks for the help, I have reduced my line of codes tremendously by this clever solution of current() and key(). But still I am one step away from what I want.

First here is the code:

$Date1 = "";
$Time1 = "";
$Date2 = "";
$Time2 = "";

while (list($var, $val) = each($array1)) {
        echo '<br>##### Value of' . $var . 'is' . $val . '<br>';
        if($Date2 != $var and $Time2 != $val)
        {
        $Date1 = $var;
        $Time1 = $val;
        $Date2 = key($array1);
        $Time2 = current($array1);
         echo '<br> Value : ' . $Date1;
  echo '<br> Value : ' . $Time1;
  echo '<br> Value : ' . $Date2;
  echo '<br> Value : ' . $Time2;

        }
}

Now as I described earlier, my array is in below form $array1 = [27/03/2016 => 12:00, 28/03/2016 => 01:00, 29/03/2016 => 02:00, 30/03/2016 => 03:00, ....]

Now, I want to compare pair of every two elements in this array. means, I want to compare (Date1 with Date2 and Time1 with Time2) likewise (Date3 with Date4 and Time3 with Time4).

So first time when I enter the array, I capture Date1, Time1, Date2, Time2, then I need to skip pointer in between and jump to Date3 and then capture (Date3, Date4 and Time3 and Time4). So in a first iteration i want to set value of

$Date1 = 27/03/2016

$Time1 = 12:00

$Date2 = 28/03/2016

$Time2 = 01:00

Then in the second iteration. I want to set

$Date1 = 29/03/2016

$Time1 = 02:00

$Date2 = 30/03/2016

$Time2 = 03:00

and then go on..............

So I am using an if clause here to skip a loop in between for one condition and then jump to next, but it is not skipping it as value of variables are not persisting when entering the loop for the next iteration. So currently above code is fetching me below result.

First Iteration:

$Date1 = 27/03/2016

$Time1 = 12:00

$Date2 = 28/03/2016

$Time2 = 01:00

Second Iteration:

$Date1 = 28/03/2016

$Time1 = 01:00

$Date2 = 29/03/2016

$Time2 = 02:00

where you can see Date2 and Time2 in the first run is same Date1 and Time1 in second which is not desired. So I tried to use global variable when I tried it first but then it did not work then. So any suggestions here ?

I hope I have explained it better this time.

Thanks guys for the hints provided, I got the answer on my own. I just used next() to skip one position and take the pointer to the next postion Below is the code, it works perfectly as I expect.

$Date1 = "";
$Time1 = "";
$Date2 = "";
$Time2 = "";

while (list($var, $val) = each($array1)) {
        echo '<br>##### Value of' . $var . 'is' . $val . '<br>';
        if($Date2 != $var and $Time2 != $val)
        {
        $Date1 = $var;
        $Time1 = $val;
        $Date2 = key($array1);
        $Time2 = current($array1);
         echo '<br> Value : ' . $Date1;
         echo '<br> Value : ' . $Time1;
         echo '<br> Value : ' . $Date2;
         echo '<br> Value : ' . $Time2;
         next($array1); // Here is the line that was required to make it work
        }
}

Thanks again for the hints provided. This issue can be marked resolved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM