2016-02-16 174 views
0

我想通過一個共同的值合併兩個數組(它們來自mysql的查詢),但很遺憾,直到現在沒有運氣。按值合併兩個多維數組

所以基本上,我有兩個單獨的數組,你可以看到下面,第一個被稱爲$第一步,第二個$第二步

Array 
(
[0] => Array 
    (
     [0] => 4 
     [inventory_id] => 4 
     [1] => 13 
     [box] => 13 
     [2] => 4 
     [wine_id] => 
     [3] => 34 
     [quantity] => 34 
     [4] => [email protected] 
     [email] => [email protected] 
    ) 

[1] => Array 
    (
     [0] => 9 
     [inventory_id] => 9 
     [1] => 0 
     [box] => 0 
     [2] => 17672 
     [wine_id] => 
     [3] => 538 
     [quantity] => 538 
     [4] => [email protected] 
     [email] => [email protected] 
    ) 
) 

Array 
( 
[0] => Array 
    (
     [0] => 4 
     [wine_id] => 4 
     [1] => Ajaccio (CORSE) 
     [villesetregion] => Ajaccio (CORSE) 
     [2] => Ajaccio 
     [villes] => Ajaccio 
     [3] => (CORSE) 
     [regions] => (CORSE) 
     [4] => Clos d'Alzeto 2008 
     [nometannee] => Clos d'Alzeto 2008 
     [5] => Clos d'Alzeto 
     [nom] => Clos d'Alzeto 
     [6] => 2008 
     [annee] => 2008 
     [7] => 8 à 11 € 
     [prix] => 8 à 11 € 
     [8] => Guide 2010 
     [anneeduguide] => Guide 2010 
     [9] => 2 
     [etoile] => 2 
    ) 

[1] => Array 
    (
     [0] => 17642 
     [wine_id] => 17642 
     [1] => Pauillac (BORDELAIS) 
     [villesetregion] => Pauillac (BORDELAIS) 
     [2] => Pauillac 
     [villes] => Pauillac 
     [3] => (BORDELAIS) 
     [regions] => (BORDELAIS) 
     [4] => Chateau Latour 2007 
     [nometannee] => Chateau Latour 2007 
     [5] => Chateau Latour 
     [nom] => Chateau Latour 
     [6] => 2007 
     [annee] => 2007 
     [7] => 75 à 100 € 
     [prix] => 75 à 100 € 
     [8] => Guide 2011 
     [anneeduguide] => Guide 2011 
     [9] => 2 
     [etoile] => 2 
    ) 
) 

在MySQL查詢有以下幾種:

$step1 : 
$query2 = "SELECT* FROM `inventory_users` WHERE email='".$email_user."'"; 

$step2 : 
$query3 = "SELECT * FROM `vins_tbl` WHERE wine_id IN (".implode(',', $wine).")"; 

inventory_users數據庫結構是:

1 inventory_id bigint(20)   
2 box int(11)   
3 wine_id int(11)   
4 quantity int(11)   
5 email text latin1_swedish_ci  

vins_tb

1 wine_id int(5)   Oui NULL   
2 villesetregion varchar(65) utf8_general_ci  
3 villes varchar(50) utf8_general_ci    
4 regions varchar(30) utf8_general_ci   
5 nometannee varchar(105) utf8_general_ci   
6 nom varchar(100) utf8_general_ci  
7 annee varchar(4) utf8_general_ci  
8 prix varchar(13) utf8_general_ci  
9 anneeduguide varchar(10) utf8_general_ci  
10 etoile varchar(2) utf8_general_ci  Oui NULL  

,我想這兩個數組由wine_id鍵合併,怎麼能做到呢?

謝謝你的幫助!

+0

1)這些數組來自MySQL查詢? 2)在你的第一個數組中'wine_id'顯示爲空。 – fusion3k

+0

mmm ...並且我想要一個「Chateau Latour」的_verre_ ... – fusion3k

+0

是的,他們來自mysql查詢 – BrunoGG

回答

0

您可以通過這種方式連接值直接在MySQL的查詢:

$query = "SELECT * 
      FROM inventory_users 
     LEFT JOIN vins_tbl 
       ON inventory_users.wine.id = vins_tbl.wine_id 
      WHERE email='{$email_user}' 
"; 

我用LEFT JOIN,也將檢索inventory_users沒有任何vins_tbl.wine_id,但 - 因爲也許它不是你的情況 - 你可以替換LEFT JOIN用簡單的JOIN(或INNER JOIN,它們是別名)。

此外,我看到您的數組有數字鍵和關聯鍵:如果您使用PDO驅動程序,您可以執行->fetchAll(PDO::FETCH_ASSOC)只有關聯鍵。

請注意:我沒有測試過查詢,所以 - 如果它不能正常工作 - 隨時發表評論。


+0

非常感謝!試圖解決PHP中的問題花了很多時間,並且在SQL中非常容易! – BrunoGG

0

雖然我同意你的意見,你應該加入他們的查詢,而不是在PHP(假設可能在你的情況下),這裏是一個簡單的方法來加入陣列。

$array1 = array(
array(
    'id' => 1, 
    'name' => 'John' 
), 
array(
    'id' => 2, 
    'name' => 'Bob' 
) 
); 
$array2 = array(
array(
    'id' => 1, 
    'money' => 3000 
), 
array(
    'id' => 2, 
    'money' => 5000 
), 
); 

foreach($array1 as $part1){ 
$found = false; 
foreach($array2 as $part2){ 
    if($part1['id'] == $part2['id']){ 
     $found = true; 
     break; 
    } 
} 
if($found){ 
    $part[] = $part1 + $part2; 
} 
} 
var_dump($part); 

您有任何問題,甚至對SQL連接語句(在你的選擇)。

不要猶豫,問。

+0

感謝您的回答,我選擇了SQL連接語句。 – BrunoGG