2

I have an array which looks like this:

Array
(
    [0] => Array
        (
            [1] => Array
                (
                    [name] => vrij
                    // ...
                )

            [2] => Array
                (
                    [name] => zat
                   // ...
                )
         )
)

I build this array using a for loop; however, I need to push 4 more 'records' to the array, which I can't do in this for loop.

I want the array to look like this, after the pushes:

    Array
(      
    [0] => Array
    (
        [1] => Array
            (
                [name] => vrij
              // ...
            )

        [2] => Array
            (
                [name] => zat
               // ...
            )
         // ...
     )
    [1] => Array
    ( 
          [1] => Array
              (
               [name] => zon
               //...
              )
           [2] // etc
     )
)

The four new records should be pushed to array[1], so I get something like

$array[1][0], $array[1][1], etc. 0 1 2 3 contains the new data. 

I tried quite a lot of stuff, to be honest. I need to do four of these pushes, so I was trying a for loop:

for($i = 0; $i < 4; $i++)
    {
        $day_info = $this->get_day_info($i, $data['init']['next_month'], $data['init']['current_year']);
        $push['name'] = $day_info['day_name'];
        array_push($data['dates'], $push);
    }

and all other kinds of things with [], [1][$i], etc. Sometimes it even adds five arrays! I'm confused as to why it won't just add the [1][1], [1][2],.. I'm probably missing out on something here. Thanks a lot.

If this isn't clear, please do tell and I'll add more code to explain the problem better.

Joris Ooms
  • 11,880
  • 17
  • 67
  • 124
  • What do you want the final array to look like? – PeeHaa Apr 28 '11 at 17:48
  • The second code block has what I want it to look like :) Although maybe that isn't really clear. I'll quickly edit! – Joris Ooms Apr 28 '11 at 17:50
  • `Hi. I have an array which looks like this...` Your first array looks rather strange: it's triply nested array, so taht, for example, you'd write `$x[0][1]['name']` to get 'vrij'. Are you sure that's correct? I'd bet it's not. – leonbloy Apr 28 '11 at 17:50
  • Yeah, that's how it's written, it works fine with the way my application is built. I actually needed the 'triple-nesting' to get the stuff I wanted done.. – Joris Ooms Apr 28 '11 at 17:52
  • @cabaret: Offtopic: wish it was already vrij ;) – PeeHaa Apr 28 '11 at 17:54

1 Answers1

2
$extradates = array(1 => 'zon', 2 => 'maa');
$data['dates'][] = $extradates;

Will add 2 extra dates to the array using a new index.

Although if I see what you trying to accomplish, I think there might be a better way.

This above works though :)

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • Thanks. I just looped my data into an array and used something like your second line here to add it to my $data['dates'] array. Should've stayed away from that array_push() thing I guess; this is way easier. My teacher told me to use array_push() but meh. Thanks! – Joris Ooms Apr 28 '11 at 18:01
  • @cabaret: tell you teacher the following: Most of the time (always???) array_push() is slower. Because a function call is always slower and on top of that array_push takes mixed parameters which also makes it slower. Also note that $array[] looks cleaner and is easier to type :) And yw ofc :) – PeeHaa Apr 28 '11 at 18:09