将数组值与先前的数组值进行比较

时间:2012-07-02 02:57:33

标签: php

如何将当前数组值与之前的数组值进行比较。示例如果我有以下数组并希望将[BM1367 PD C 70] [ST00576] ['transferfrom']与之前的数组进行比较[BM1367 PD B 85] [ST00576] ['transferfrom']?

    [BM1367    PD  B  85] => Array
        (
            [ST00576] => stdClass Object
                (
                    [transferfrom] => 102
                    [transferto] => 66
                    [BR_ID] => 102
                    [refno] =>  

                )

            [OT01606] => stdClass Object
                (
                    [transferfrom] => 102
                    [transferto] => 66
                    [BR_ID] => 66
                    [refno] => 102 - ST00576

                )

        )

    [BM1367    PD  C  70] => Array
        (
            [ST00576] => stdClass Object
                (
                    [transferfrom] => 102
                    [transferto] => 66
                    [BR_ID] => 102
                    [refno] =>  

                )

            [OT01606] => stdClass Object
                (
                    [transferfrom] => 102
                    [transferto] => 66
                    [BR_ID] => 66
                    [refno] => 102 - ST00576

                )

        )

    [BM1367    PD  C  85] => Array
        (
            [ST00576] => stdClass Object
                (
                    [transferfrom] => 102
                    [transferto] => 66
                    [BR_ID] => 102
                    [refno] =>  

                )

            [OT01606] => stdClass Object
                (
                    [transferfrom] => 102
                    [transferto] => 66
                    [BR_ID] => 66
                    [refno] => 102 - ST00576

                )

        )

    [BM1367    PD  D  85] => Array
        (
            [ST00576] => stdClass Object
                (
                    [transferfrom] => 102
                    [transferto] => 66
                    [BR_ID] => 102
                    [refno] =>  

                )

            [OT01606] => stdClass Object
                (
                    [transferfrom] => 102
                    [transferto] => 66
                    [BR_ID] => 66
                    [refno] => 102 - ST00576

                )

        )

)

2 个答案:

答案 0 :(得分:0)

您有一个STD class objects数组,在这种情况下,您可以按如下方式比较元素: 假设您提供的数组位于另一个数组

$tempArray = {


        [BM1367    PD  B  85] => Array
            (
                [ST00576] => stdClass Object
                    (
                        [transferfrom] => 102
                        [transferto] => 66
                        [BR_ID] => 102
                        [refno] =>  

                    )

                [OT01606] => stdClass Object
                    (
                        [transferfrom] => 102
                        [transferto] => 66
                        [BR_ID] => 66
                        [refno] => 102 - ST00576

                    )

            )

        [BM1367    PD  C  70] => Array
            (
                [ST00576] => stdClass Object
                    (
                        [transferfrom] => 102
                        [transferto] => 66
                        [BR_ID] => 102
                        [refno] =>  

                    )

                [OT01606] => stdClass Object
                    (
                        [transferfrom] => 102
                        [transferto] => 66
                        [BR_ID] => 66
                        [refno] => 102 - ST00576

                    )

            )

        [BM1367    PD  C  85] => Array
            (
                [ST00576] => stdClass Object
                    (
                        [transferfrom] => 102
                        [transferto] => 66
                        [BR_ID] => 102
                        [refno] =>  

                    )

                [OT01606] => stdClass Object
                    (
                        [transferfrom] => 102
                        [transferto] => 66
                        [BR_ID] => 66
                        [refno] => 102 - ST00576

                    )

            )

        [BM1367    PD  D  85] => Array
            (
                [ST00576] => stdClass Object
                    (
                        [transferfrom] => 102
                        [transferto] => 66
                        [BR_ID] => 102
                        [refno] =>  

                    )

                [OT01606] => stdClass Object
                    (
                        [transferfrom] => 102
                        [transferto] => 66
                        [BR_ID] => 66
                        [refno] => 102 - ST00576

                    )

            )

    )

};

您现在可以访问元素

$tempArray['BM1367 PD C 70']['ST0076']->transferfrom

$tempArray['BM1367 PD B 85']['ST0076']->transferfrom

并且为了比较它们

(int)$tempArray['BM1367 PD C 70']['ST0076']->transferfrom == (int)$tempArray['BM1367 PD B 85']['ST0076']->transferfrom

答案 1 :(得分:0)

你问:

  

如何将当前数组值与先前的数组值进行比较

我想你可能想看看下面的PHP函数

  1. current() - http://www.php.net/manual/en/function.current.php
  2. prev() - http://www.php.net/manual/en/function.prev.php
  3. next() - http://php.net/manual/en/function.next.php
  4. 例如:

    <?php
    $transport = array('foot', 'bike', 'car', 'plane');
    $mode = current($transport); // $mode = 'foot';
    $mode = next($transport);    // $mode = 'bike';
    $mode = next($transport);    // $mode = 'car';
    $mode = prev($transport);    // $mode = 'bike';
    $mode = end($transport);     // $mode = 'plane';
    ?>
    

    我能够在计算公式上使用它们,工作很棒!在比较您的上一个或下一个数组的当前值时,它也很有用。