2016-02-16 146 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