Use a column as array key

Viewed 65

I want to use a column with numbers as array key.

I use a loop after fetchAll but I would like to know if is it possible to make it direclty.

My query:

$RequeteSql = $db->query("
    SELECT *
    FROM PouvoirsNumeros
        LEFT JOIN Pouvoirs ON (Pouvoirs.Pouvoir_Id = PouvoirsNumeros.Pouvoir_Id)
    WHERE
        PouvoirsNumeros.TypePerso = 'Ange'
    ORDER BY
        Pouvoirs.Pouvoir_Nom");
$ResultatRequete = $RequeteSql->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_ASSOC);

With it, I have:

(
    [0] => Array
        (
            [TypePerso] => Ange
            [Numero_Des] => 711
            [Pouvoir_Id] => 157
            ...
        )
...
)

I would like to replace the key with Pouvoir_Id:

(
    [157] => Array
        (
            [TypePerso] => Ange
            [Numero_Des] => 711
            [Pouvoir_Id] => 157
            ...
        )
...
)

With PDO::FETCH_GROUP|PDO::FETCH_ASSOC:

(
    [157] => Array
        (
            [0] => Array
                (
                    [TypePerso] => Ange
                    [Numero_Des] => 711
                    [Pouvoir_Id] => 157
                    ...
                )
        )
...
)

Is it possible to make it directly with PDO ?

1 Answers

Think this will work...

$RequeteSql = $db->query("
    SELECT Pouvoir_Id as ind, TypePerso, Numero_Des, Pouvoir_Id
    FROM PouvoirsNumeros
        LEFT JOIN Pouvoirs ON (Pouvoirs.Pouvoir_Id = PouvoirsNumeros.Pouvoir_Id)
    WHERE
        PouvoirsNumeros.TypePerso = 'Ange'
    ORDER BY
        Pouvoirs.Pouvoir_Nom");
$ResultatRequete = $RequeteSql->fetchAll(PDO::FETCH_UNIQUE|PDO::FETCH_ASSOC);

I'll delete it when I can find the dupe.

Related