繁体   English   中英

根据值推送到带有键的数组

[英]Push to array with key based on value

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

看起来像这样:

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

首先将new_array放入my_array并使用usort

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM