简体   繁体   English

PHP:解析关联数组以比较数组中的每两个值

[英]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. 即Date1Time1与Date2Time2比较,Date3Time3与Date4Time4比较,依此类推。

So below I am parsing an array using while loop and passing the values to the function creating_detailed_array() . 所以下面我使用while循环解析一个数组,并将值传递给函数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 假设1:我已将$GLOBALS['counter']定义$GLOBALS['counter']值为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. 假设2:这里的Date1,Time1,Date2,Time2是我在PHP页面顶部定义的变量,考虑了以下全局范围。

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. 现在的问题是,当第一次在代码中进行解析时,我将获得正确的值存储在Date1和Time1中,当循环使用值Date2和Time2进行解析时,Date1和Time1的值将设置为null。

As I want to compare first element of Assoc array with second, third with fourth and so on. 因为我想将Assoc数组的第一个元素与第二个,第三个与第四个进行比较,依此类推。

I want to have Date1 and Time1 and Date2 and Time2 values stored in the variables for future use for comparison. 我想将Date1和Time1以及Date2和Time2值存储在变量中,以备将来比较时使用。 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 我使用的PHP版本是5.5

Thanks for the help, I have reduced my line of codes tremendously by this clever solution of current() and key(). 感谢您的帮助,通过current()和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, ....] 现在,正如我之前所描述的,我的数组的格式如下:$ 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). 意思是,我想同样地比较(Date1和Date2,Time1和Time2)(Date3和Date4,Time3和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). 因此,第一次进入数组时,我捕获了Date1,Time1,Date2,Time2,然后我需要跳过它们之间的指针并跳转到Date3,然后捕获(Date3,Date4,Time3和Time4)。 So in a first iteration i want to set value of 所以在第一次迭代中我想设置

$Date1 = 27/03/2016 $ Date1 = 27/03/2016

$Time1 = 12:00 $ Time1 = 12:00

$Date2 = 28/03/2016 $ Date2 = 28/03/2016

$Time2 = 01:00 $ Time2 = 01:00

Then in the second iteration. 然后在第二次迭代中。 I want to set 我要设定

$Date1 = 29/03/2016 $ Date1 = 29/03/2016

$Time1 = 02:00 $ Time1 = 02:00

$Date2 = 30/03/2016 $ Date2 = 30/03/2016

$Time2 = 03:00 $ 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. 因此,我在这里使用if子句跳过一个条件之间的循环,然后跳转到下一个条件,但是由于在进入下一个迭代的循环时变量的值不持久,因此它并未跳过。 So currently above code is fetching me below result. 所以目前上面的代码正在将我抓到下面的结果。

First Iteration: 第一次迭代:

$Date1 = 27/03/2016 $ Date1 = 27/03/2016

$Time1 = 12:00 $ Time1 = 12:00

$Date2 = 28/03/2016 $ Date2 = 28/03/2016

$Time2 = 01:00 $ Time2 = 01:00

Second Iteration: 第二次迭代:

$Date1 = 28/03/2016 $ Date1 = 28/03/2016

$Time1 = 01:00 $ Time1 = 01:00

$Date2 = 29/03/2016 $ Date2 = 29/03/2016

$Time2 = 02:00 $ 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. 在第一次运行中可以看到Date2和Time2的地方在第二秒中看到的是Date1和Time1,这是不希望的。 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. 我只是使用next()跳过一个位置,然后将指针指向下一个位置。下面是代码,它可以按我期望的那样完美地工作。

$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. 此问题可以标记为已解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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