0

I need to get some currency ids from db, this is my code

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

this is $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
        )

)

and this is $arr

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

I don't understand why I get duplicate values in arrays and how to prevent it?

1
  • Which key you want from $currencies array? Commented Apr 5, 2016 at 8:32

4 Answers 4

2

PDO is a database wrapper that can do many things for you. For example,

So in fact you need two times less code than you have now:

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

or I would rather propose to make it like

$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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for help!
1

The loop is problematic:

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

Just use a simple

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

Edit #1:

Using your $currencies and old query, I got the following:

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
)

2 Comments

Thank you, it works! but I still don't understand why I get duplicate values in arrays (
are you sure with your dump? using your old query, $arr should have the values USD and RUB as well.. checkout my dump using your $currencies data.. @Heidel
0

I know this question is getting old. But here is the solution to prevent the duplicate values from PDO. Just using this:

$stmt->fetchAll(PDO::FETCH_ASSOC);

Instead of this:

$stmt->fetchAll();

Comments

-1

use following query $query = "SELECT DISTINCT curr_id FROM dictionary_currency WHERE curr_code IN (". $currency_codes_in .")";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.