1

我将以下数组存储在 $_SESSION

[Bookings] => Array
    (
        [date] => Array
             (
                [0] => 1/12/2013
                [1] => 1/19/2013
                [2] => 2/03/2013
            )

        [price] => Array
            (
                [0] =>  100
                [1] =>  150
                [2] =>  120
             )

   )

但是我想使用一个 foreach 循环并对数组中的两个值执行计算。我似乎无法弄清楚如何使用 foreach 来容纳多值,我有一个我在下面写的 foreach 示例我正在努力实现。任何人都指出我正确的方向。

foreach ($_SESSION['Bookings'] as $bookings) 
{
   myDate = $bookings[date];
   myPrice = $bookings[price];

   // Some other stuff here
}
4

3 回答 3

3
foreach ($_SESSION['Bookings']['date'] as $key => $value) {
    $myDate = $value;
    $myPrice = $_SESSION['Bookings']['price'][$key];
}

我猜更简单:)

于 2013-01-04T13:35:27.863 回答
2
foreach (array_keys($_SESSION['Bookings']['date']) as $key) 
{
    $myDate  = $_SESSION['Bookings']['date'][$key];
    $myPrice = $_SESSION['Bookings']['price'][$key];
}

应该管用?

一些信息:array_keys

于 2013-01-04T13:30:27.530 回答
0

只需循环您的子数组并从另一个子数组中读取相应的值

foreach ( $_SESSION['Bookings'][ 'date' ] as $key => $myDate) {

    $myPrice = $_SESSION['Bookings'][ 'price' ][ $key ];


    // here you can access to $myDate and $myPrice

    // Some other stuff here
}
于 2013-01-04T13:35:12.780 回答