按值合并两个多维数组

时间:2016-02-16 17:57:59

标签: php arrays multidimensional-array

我试图通过一个共同的值合并两个数组(它们来自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).")";

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键合并两个数组,怎么做?

感谢您的帮助!

2 个答案:

答案 0 :(得分: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连接语句(在您的选择中)。

不要犹豫。

答案 1 :(得分: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 1}}(或INNER JOIN,它们是别名)。

另外,我看到你的数组有数字和关联键:如果使用PDO驱动程序,你可以执行->fetchAll( PDO::FETCH_ASSOC )只有关联键。

请注意:我没有测试过该查询,所以 - 如果它没有正常工作 - 请随时发表评论。