1

I've got the following array stored in a $_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
             )

   )

However I want to use a foreach loop and perform calculation on both values within the array.I can't seem to fugure out how I can use the foreach to accomodate multivalues, I've got a sample of a foreach I wrote below of what I'm trying to achieve. Anyone point me in the right direction.

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

   // Some other stuff here
}
2
  • doesn't $myDate[0] give '1/12/2013' ? Commented Jan 4, 2013 at 13:29
  • Alternative solution with MultipleIterator: stackoverflow.com/a/14724544/664108 Commented Feb 6, 2013 at 9:04

3 Answers 3

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

simpler I guess :)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks TomTom, yours worked as well.Just been looking into array_keys, which might be useful in the future.I've bumped your answer to useful. cheers.
2
foreach (array_keys($_SESSION['Bookings']['date']) as $key) 
{
    $myDate  = $_SESSION['Bookings']['date'][$key];
    $myPrice = $_SESSION['Bookings']['price'][$key];
}

Should work?

Some info on: array_keys

Comments

0

just loop through on of your subarrays and read the corresponding value from the other

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

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


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

    // Some other stuff here
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.