2016-06-19 70 views
-3

我有一個數組添加另一個數組在PHP

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

請爲我提供正確的代碼。

回答

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你正在尋找...

必須添加更多的信息最終陣列.. 。因爲我有這種簡單的代碼給你的心情,但是請,下次給你更多關於你的問題的信息。堆棧溢出不是一個代碼服務,我們幫助,而不是從0開始你想要的。

相關問題