使用基于值的键推送到数组

时间:2015-02-11 11:11:23

标签: php arrays

我有一个多维数组,然后是另一个数组,其结构相似但不相同(它共享公共键)。

它看起来像这样:

[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
    )

第一个数组已在booking_start键上订购,但我想按照booking_start的顺序将新数组项推送到第一个数组。

我猜这个阵列拼接在这里是我的朋友但是如何解决我需要推进的位置?

所以我的结果看起来像这样:

[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
            )
    )

1 个答案:

答案 0 :(得分:1)

首先将new_array推入my_array并使用usort

 array_push($my_Array, $new_arr);
 usort($my_Array, function($a, $b) {
   return $a['booking_start'] - $b['booking_start'];
 });