tags:

views:

67

answers:

1

hello, i have the following array:

["addToCart"]=>
  array(3) {
    [1]=>
    array(5) {
      ["aantal"]=>
      int(1)
      ["film_id"]=>
      string(1) "1"
      ["zaal_id"]=>
      string(1) "1"
      ["dag"]=>
      string(7) "maandag"
      ["seats"]=>
      array(4) {
        [0]=>
        string(2) "67"
        [1]=>
        string(2) "68"
        [2]=>
        string(2) "69"
        [3]=>
        string(2) "70"
      }
    }

You can see that i have an array called "seats" inside the "addToCart" array.There are 4 items in the "seats" array. what i would like to have is 4 separate arrays, they should all have the same content but each of them needs to have 1 value of "seats".

+4  A: 

I'm not sure I got exactly what you're looking to do, but this would result in an array of arrays where each has only one seat:

$seatArrays = array();

foreach ($addToCart as $arr)
{
  foreach ($arr["seats"] as $seat)
  {
    $seatArr = $arr;            // Copy the original array
    $seatArr["seats"] = $seat;  // Replace the "seats" subarray with the current seat
    $seatArrays[] = $seatArr;    
  }
}
Daniel Vandersluis
This looks to be exactly what i am looking for but i asked the question wron, its actually a session array, and i need it to convert in the way you described but it should end up being a session array again. Maybe i sound dumb for not doing it by myself but i have no clue how to do it. I'm still learning from books etc and its harder than i thought.
vincent
I tried to adapt you code and mess around a bit with it but i found out that if you trace the following: foreach ($addToCart as $arr)loses all the id's, it gives the following output.'array(5) { [0]=> int(1) [1]=> string(1) "1" [2]=> string(1) "1" [3]=> string(7) "maandag" [4]=> array(2) { [0]=> string(2) "21" [1]=> string(2) "22" }}'
vincent
I'm not really sure what you're talking about... if the subarrays of `$addToCart` have keys, then `$arr` in `foreach ($addToCart as $arr)` will as well. And in terms of sessions, you can just put `$seatArray` back into your session...
Daniel Vandersluis