0

I have a multidimensional array and then another array where the structure is similar but not identical (it shares common keys).

It looks something like this:

[my_Array] => Array
    (
        [0] => Array
            (
                [title] => o1
                [item_details] => original item 1 
                [booking_date] => 2015-02-14
                [booking_start] => 18:30:00
                [booking_end] => 18:35:00
            )
        [1] => Array
            (
                [title] => o2
                [item_details] => original item 2
                [booking_date] => 2015-02-14
                [booking_start] => 19:30:00
                [booking_end] => 19:35:00
            )
    )


[new_array] => Array
    (
        [item_details] => new item 
        [booking_date] => 2015-02-14
        [booking_start] => 18:55:00
    )

The first array is already ordered on the booking_start key, but I want to push the new array item to the first array in order of booking_start.

I'm guessing that array splice is my friend here but how work out what position I need to push to?

So my result would look like this:

[my_Array] => Array
    (
        [0] => Array
            (
                [title] => o1
                [item_details] => original item 1 
                [booking_date] => 2015-02-14
                [booking_start] => 18:30:00
                [booking_end] => 18:35:00
            )
        [1] => Array
            (
                [item_details] => new item 
                [booking_date] => 2015-02-14
                [booking_start] => 18:55:00
            )
        [2] => Array
            (
                [title] => o2
                [item_details] => original item 2 
                [booking_date] => 2015-02-14
                [booking_start] => 19:30:00
                [booking_end] => 19:35:00
            )
    )
5
  • Check php.net/manual/en/function.array-map.php Commented Feb 11, 2015 at 11:13
  • @Tom Do you want to push the new item array in order of booking_start so that in your case thew new array comes on 1st index of existing array. Commented Feb 11, 2015 at 11:18
  • @dev_khan Please see my addition... Commented Feb 11, 2015 at 11:22
  • Why array_splice? Why not just push and reorder with usort? Commented Feb 11, 2015 at 11:24
  • Just an observation: var_export far better than print_r for example data, because with the ouput of var_export: copy&paste, 3 lines of code showing what you want to know, done. With print_r: hm, do I re-format the data for that little question ...nope, next question.... Commented Feb 11, 2015 at 11:41

1 Answer 1

1

First push new_array into my_array and use usort

 array_push($my_Array, $new_arr);
 usort($my_Array, function($a, $b) {
   return $a['booking_start'] - $b['booking_start'];
 }); 
Sign up to request clarification or add additional context in comments.

1 Comment

had to convert the time to seconds but this got me there... thanks

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.