在php中的数组中每2个后添加另一个数组

时间:2016-06-19 06:38:58

标签: php arrays

我有一个数组

$array1 = Array
(
    [0] => Array
        (
            [event_id] => 2
            [event_name] => Vectro
            [event_date_time] => 2016-06-20 15:25:39
            [location] => Edachira
            [gps_latitude] => 9.998671
            [gps_longitude] => 76.350319
            [event_thumb_img] => 
        )

    [1] => Array
        (
            [event_id] => 3
            [event_name] => Illusions
            [event_date_time] => 2016-06-20 15:25:39
            [location] => Vazhakkala
            [gps_latitude] => 0.000000
            [gps_longitude] => 0.000000
            [event_thumb_img] => event_image/3/1464589975358.jpg
        )

    [2] => Array
        (
            [event_id] => 4
            [event_name] => Bass
            [event_date_time] => 2016-06-10 15:25:39
            [location] => Palarivattom
            [gps_latitude] => 10.003811
            [gps_longitude] => 76.346176
            [event_thumb_img] => 
        )

    [3] => Array
        (
            [event_id] => 5
            [event_name] => Bug
            [event_date_time] => 2016-06-16 15:25:39
            [location] => Irumpanam
            [gps_latitude] => 9.998671
            [gps_longitude] => 76.346176
            [event_thumb_img] => 
        )

    [4] => Array
        (
            [event_id] => 22
            [event_name] => Evolution unknown
            [event_date_time] => 2016-06-16 23:00:00
            [location] => Aluva
            [gps_latitude] => 0.000000
            [gps_longitude] => 0.000000
            [event_thumb_img] => event_image/22/IMG-20160405-WA0000.jpg
        )
)

我有另一个阵列

$array2 = Array
(
    [0] => Array
        (
            [adv_name] => Test3
            [adv_url] => 
            [adv_image] => adv/1/zmr.jpg
        )

    [1] => Array
        (
            [adv_name] => Test2
            [adv_url] => 
            [adv_image] => adv/1/zmr.jpg
        )

    [2] => Array
        (
            [adv_name] => Test1
            [adv_url] => 
            [adv_image] => adv/1/zmr.jpg
        )

    [3] => Array
        (
            [adv_name] => Test
            [adv_url] => 
            [adv_image] => adv/1/zmr.jpg
        )
)

我希望这个数组来自这两个:

Array
(
    [0] => Array
        (
            [event_id] => 2
            [event_name] => Vectro
            [event_date_time] => 2016-06-20 15:25:39
            [location] => Edachira
            [gps_latitude] => 9.998671
            [gps_longitude] => 76.350319
            [event_thumb_img] => 
        )

    [1] => Array
        (
            [event_id] => 3
            [event_name] => Illusions
            [event_date_time] => 2016-06-20 15:25:39
            [location] => Vazhakkala
            [gps_latitude] => 0.000000
            [gps_longitude] => 0.000000
            [event_thumb_img] => event_image/3/1464589975358.jpg
        )

    [2] => Array
        (   
            [adv_name] => Test3
            [adv_url] => 
            [adv_image] => adv/1/zmr.jpg
        )  

    [3] => Array
        (
            [event_id] => 4
            [event_name] => Bass
            [event_date_time] => 2016-06-10 15:25:39
            [location] => Palarivattom
            [gps_latitude] => 10.003811
            [gps_longitude] => 76.346176
            [event_thumb_img] => 
        )

    [4] => Array
        (
            [event_id] => 5
            [event_name] => Bug
            [event_date_time] => 2016-06-16 15:25:39
            [location] => Irumpanam
            [gps_latitude] => 9.998671
            [gps_longitude] => 76.346176
            [event_thumb_img] => 
        )

    [5] => Array
        ( 
            [adv_name] => Test2
            [adv_url] => 
            [adv_image] => adv/1/zmr.jpg
        )

    [6] => Array
        (
            [event_id] => 22
            [event_name] => Evolution unknown
            [event_date_time] => 2016-06-16 23:00:00
            [location] => Aluva
            [gps_latitude] => 0.000000
            [gps_longitude] => 0.000000
            [event_thumb_img] => event_image/22/IMG-20160405-WA0000.jpg
        )
)

请提供正确的密码。

1 个答案:

答案 0 :(得分:0)

要做你想做的事,请使用以下代码:

<?php
$final_array = array();
for($k = 0, $j = 0; $j < count($array2); $k += 2, $j++) {

    for($i = 0; $i < 2; $i++) {
        if(isset($array1[$i + $k])
            $final_array[] = $array1[$i + $k];
    }

    if($j % 3 == 0)
        $final_array[] = $array2[$j];

}

print_r($final_array);

此代码将全部$array1,如果某个密钥不存在,则不会添加该密码。当你越过$array2时,是3的倍数(第3次,第6次,第9次),它会将$array2添加到你要查找的最终数组中......

必须添加更多信息...因为我有这种简单代码的心情,但请下次再提供有关您问题的更多信息。 Stack Overflow不是代码服务,我们提供帮助,而不是从0开始。