2013-05-15 84 views
0

我有2個數組。是否有可能創建輸出數組並添加鍵/值從第一個到第二個?我的數組是這樣的:數組搜索和PHP中的組合

第一:

Array 
    (
     [0] => Array 
      (
       [id] => 11 
       [expire] => undefined 
      ) 

     [1] => Array 
      (
       [id] => 12 
       [expire] => undefined 
      ) 

     [2] => Array 
      (
       [id] => 6 
       [expire] => 8 
      ) 

     [3] => Array 
      (
       [id] => 10 
       [expire] => 4 
      ) 
    ) 

第二:

Array 
    (
     [0] => Array 
      (
       [id] => 6 
       [realname] => to_es.gif 
       [extension] => gif 
       [filesize] => 57885 
      ) 

     [1] => Array 
      (
       [id] => 10 
       [realname] => to_joomla_2_customer_view.gif 
       [extension] => gif 
       [filesize] => 77182 
      ) 

     [2] => Array 
      (
       [id] => 11 
       [realname] => to_nl.gif 
       [extension] => gif 
       [filesize] => 10990 
      ) 

     [3] => Array 
      (
       [id] => 12 
       [realname] => to_PL_1.gif 
       [extension] => gif 
       [filesize] => 52826 
      ) 

    ) 

我應得的輸出數組:

Array 
     (
      [0] => Array 
       (
        [id] => 6 
        [realname] => to_es.gif 
        [extension] => gif 
        [filesize] => 57885 
        [expire] => 8 
       ) 

      [1] => Array 
       (
        [id] => 10 
        [realname] => to_joomla_2_customer_view.gif 
        [extension] => gif 
        [filesize] => 77182 
        [expire] => 4 
       ) 

      [2] => Array 
       (
        [id] => 11 
        [realname] => to_nl.gif 
        [extension] => gif 
        [filesize] => 10990 
        [expire] => undefined 
       ) 

      [3] => Array 
       (
        [id] => 12 
        [realname] => to_PL_1.gif 
        [extension] => gif 
        [filesize] => 52826 
        [expire] => undefined 
       ) 

     ) 
+2

這裏'foreach'沒有錯;你嘗試過什麼嗎? – Passerby

+0

數組元素的順序是否起作用?在第一個數組中,索引爲[0]的數組的ID爲11.在第二個數組中,第一個數組的第一個數組的ID爲6.那麼你希望如何合併它?你想通過數組索引還是通過id來合併它? – thpl

+0

你好,是應該被id合併。不是索引。 – user889349

回答

1

混合兩個陣列嘗試這種情況:上Ideone

function my_array_merge($first,$second) { 
    $new = array(); 
    foreach ($first as $f_item) { 
     foreach ($second as $i => $s_item) { 
      if ($f_item['id']===$s_item['id']) { 
       $new[] = $f_item + $s_item; 
       unset($second[$i]); 
       break; 
      } 
     } 
    } 
    usort($new, function($a,$b) { 
     return $a['id'] - $b['id']; 
    }); 
    return $new; 
} 

示例:http://ideone.com/TQIIkH

+0

謝謝!有用... – user889349

1

我假設你第一個數組爲$array1,第二個數組爲$array2,您可以在第一個數組中循環,然後在第二個數組中循環ND conmparing id並且當發現在一個新

$new_array = array(); 
for($i=0; $i<count($array1); $i++) 
{ 
    foreach($array2 as $key => $data) 
    { 
     if($data['id'] == $array1[$i]['id']) 
     { 
      $new_array[] = array('id' => $array1[$i]['id'], 'realname' => $data['realname'], 'extension' => $data['extension'], 'filesize' => $data['filesize'], 'expire' => $array1[$i]['expire']); 
     } 
    } 
} 

var_dump($new_array); 

這將輸出

array(4) { 
[0]=> 
array(5) { 
    ["id"]=> 
    string(2) "11" 
    ["realname"]=> 
    string(9) "to_nl.gif" 
    ["extension"]=> 
    string(3) "gif" 
    ["filesize"]=> 
    string(5) "10990" 
    ["expire"]=> 
    string(9) "undefined" 
} 
    [1]=> 
    array(5) { 
    ["id"]=> 
    string(2) "12" 
    ["realname"]=> 
    string(11) "to_PL_1.gif" 
    ["extension"]=> 
    string(3) "gif" 
    ["filesize"]=> 
    string(5) "52826" 
    ["expire"]=> 
    string(9) "undefined" 
    } 
    // and so on 

live working code

+0

謝謝!看起來不錯。 – user889349

+0

@ user889349歡迎您 – Fabio