1

I have a multidimensional array like this:

    // Create an empty array
    $noti_log = array();

    // Add first array within a multidimensional array
    $noti_log = array(
        array('hello world')
    );

And then, when I want to add a new array inside, I do:

$noti_log[] = array('Greetings');

The problem is that whenever I add a new array, it ends up underneath the old ones. Instead, I want new arrays to line up on top of the old ones.

So in this case, array('Greetings') should be on top of array('hello world')...

How do I add new arrays that land on top?

Henrik Petterson
  • 6,862
  • 20
  • 71
  • 155

1 Answers1

7

Use array_unshift():

array_unshift() prepends passed elements to the front of the array. Note that the list of elements is prepended as a whole, so that the prepended elements stay in the same order. All numerical array keys will be modified to start counting from zero while literal keys won't be touched.

array_unshift($noti_log, array('Greetings'));
John Conde
  • 217,595
  • 99
  • 455
  • 496