2016-04-05 19 views
0

Ich brauche ein paar Währung ids von db zu bekommen, das ist mein CodePDO doppelte Werte in Array

$arr = []; 

$currency_codes = array("USD", "RUB"); 
$currency_codes_in = implode(',', array_fill(0, count($currency_codes), '?')); 
$query = "SELECT `curr_id` FROM `dictionary_currency` WHERE `curr_code` IN (". $currency_codes_in .")"; 
$stmt = $db->prepare($query); 
foreach ($currency_codes as $k => $id) { 
    $stmt->bindValue(($k+1), $id); 
} 

$stmt->execute(); 
$currencies = $stmt->fetchAll(); 

foreach($currencies as $currency) 
{ 
    foreach($currency as $key => $value) 
    { 
     $arr[] = $value; 
    } 
} 
print_r($arr); 
exit(); 

dies $currencies Array

Array 
(
    [0] => Array 
     (
      [curr_id] => 643 
      [0] => 643 
      [curr_code] => RUB 
      [1] => RUB 
     ) 

    [1] => Array 
     (
      [curr_id] => 840 
      [0] => 840 
      [curr_code] => USD 
      [1] => USD 
     ) 

) 

und das ist $arr

Array 
(
    [0] => 643 
    [1] => 643 
    [2] => 840 
    [3] => 840 
) 

Ich verstehe nicht, warum ich doppelte Werte in Arrays bekomme und wie man es verhindert?

+0

Welchen Schlüssel möchten Sie aus '$ currency' Array? – Apb

Antwort

1

Die Schleife problematisch ist:

foreach($currencies as $currency) { 
    foreach($currency as $key => $value) { 
      $arr[] = $value; 
    } 
} 

nur eine einfache

Edit # 1

foreach($currencies as $currency) { 
    $arr[] = $currency[0]; 
} 
verwenden:

mit Ihrer $currencies und alten Abfrage, bekam ich die folgenden :

Array 
(
    [0] => Array 
    (
     [curr_id] => 643 
     [0] => 643 
     [curr_code] => RUB 
     [1] => RUB 
    ) 

    [1] => Array 
    (
     [curr_id] => 840 
     [0] => 840 
     [curr_code] => USD 
     [1] => USD 
    ) 
) 

Array 
(
    [0] => 643 
    [1] => 643 
    [2] => RUB 
    [3] => RUB 
    [4] => 840 
    [5] => 840 
    [6] => USD 
    [7] => USD 
) 
+0

Danke, es funktioniert! aber ich verstehe immer noch nicht, warum ich doppelte Werte in Arrays bekomme ( – Heidel

+1

bist du sicher mit deinem Dump? benutze deine alte Abfrage, '$ arr' sollte auch die Werte USD und RUB haben .. überprüfe meinen Dump mit deinem' $ Währungen 'Daten .. @Heidel –

-1

Verwendung folgende Abfrage $ query = "SELECT DISTINCT curr_id VON dictionary_currency WHERE curr_code IN (". $ currency_codes_in. ")";

+0

Nein, ich habe versucht, es hat nicht funktioniert – Heidel

2

PDO ist ein Datenbank-Wrapper, der viele Dinge für Sie erledigen kann. Zum Beispiel

Also in der Tat brauchen Sie zwei Mal weniger Code als Sie haben jetzt:

$currency_codes = array("USD", "RUB"); 
$currency_codes_in = implode(',', array_fill(0, count($currency_codes), '?')); 
$query = "SELECT `curr_id` FROM `dictionary_currency` WHERE `curr_code` IN ($currency_codes_in)"; 
$stmt = $db->prepare($query); 
$stmt->execute($currency_codes); 
$arr = $stmt->fetchAll(PDO::FETCH_COLUMN); 

oder I würde eher vorschlagen, es zu machen wie

$query = "SELECT curr_code, curr_id FROM dictionary_currency WHERE `curr_code` IN ($currency_codes_in)"; 
$stmt = $db->prepare($query); 
$stmt->execute($currency_codes); 
$arr = $stmt->fetchAll(PDO::FETCH_KEY_PAIR); 
+0

Vielen Dank für die Hilfe! – Heidel