2016-04-05 16 views
0

yinelenen değerleri PDO, bu benim kodudurben db bazı döviz kimlikleri almak gerekir dizide

$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(); 

bu $currencies dizi

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 
     ) 

) 

ve bu $arr

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

Dizilerde neden yinelenen değerler aldığımı ve nasıl önleneceğini anlamıyorum.

+0

$ Para birimleri dizisinden istediğiniz anahtar hangisidir? – Apb

cevap

1

döngü problemlidir:

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

Sadece basit

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

Düzenleme 1. kullanın: $currencies ve eski sorguyu kullanarak

, şu var :

Sorgu $ sorgusu = Aşağıdaki
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

Teşekkür ederim, işe yarıyor! ama hala neden yinelenen değerleri dizilerde anlayamıyorum ( – Heidel

+1

çöplüğünüzden emin misiniz? Eski sorgunuzu kullanarak, $ arr değerleri USD ve RUB olmalıdır). currencies' veri .. @Heidel –

-1

kullanım "SEÇ DISTINCT curr_iddictionary_currency WHERE curr_code İÇİNDE (". $ currency_codes_in. ")";

+0

Hayır, denedim, işe yaramadı – Heidel

2

PDO, sizin için birçok şey yapabilen bir veritabanı sarıcısıdır.

$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); 

ya da ben: Örneğin,

Yani aslında şu anda sahip iki katından az kodu gerekir yürütmek

$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 ürününü nasıl sunacağımı öneririm
+0

Yardım için çok teşekkürler! – Heidel

İlgili konular