簡體   English   中英

按值合並兩個多維數組

[英]merge two multidimensional arrays by value

我試圖通過一個公共值合並兩個數組(它們來自mysql查詢),但是不幸的是,到目前為止沒有運氣。

因此,基本上,我有兩個單獨的數組,如下所示,第一個數組稱為$ step1,第二個數組稱為$ step2

Array
(
[0] => Array
    (
        [0] => 4
        [inventory_id] => 4
        [1] => 13
        [box] => 13
        [2] => 4
        [wine_id] => 
        [3] => 34
        [quantity] => 34
        [4] => xx@googlemail.com
        [email] => xx@googlemail.com
    )

[1] => Array
    (
        [0] => 9
        [inventory_id] => 9
        [1] => 0
        [box] => 0
        [2] => 17672
        [wine_id] => 
        [3] => 538
        [quantity] => 538
        [4] => xx@googlemail.com
        [email] => xx@googlemail.com
    )
)

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).")";

庫存用戶的數據庫結構為:

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鍵合並兩個數組,怎么辦?

謝謝您的幫助!

盡管我同意您應該在查詢中而不是在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 join語句(在您的Select中)也有疑問。

不要猶豫了。

您可以通過以下方式直接在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 )僅具有關聯鍵。

請注意:我尚未測試查詢,因此-如果無法正常工作-請隨時發表評論。


暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM