1

The current output is..

Array
(
    [0] => Array
        (
            [ID] => 20
            [name] => Peter
            [datetime] => Tuesday 26th Oct 21 3:50am
        )

    [1] => Array
        (
            [ID] => 21
            [name] => Paul
            [datetime] => Tuesday 26th Oct 21 4:44am
        )
)

I would like the array output to be..

Array
(
    [20] => Array
        (
            [ID] => 20
            [name] => Peter
            [datetime] => Tuesday 26th Oct 21 3:50am
        )

    [21] => Array
        (
            [ID] => 21
            [name] => Paul
            [datetime] => Tuesday 26th Oct 21 4:44am
        )
)

The code I am currently using to generate the array is..

$sql=mysqli_query($conn,"SELECT * FROM `live`");
    /*every time it fetches the row, adds it to array...*/
    while($liveuserdata[]=mysqli_fetch_array($sql, MYSQLI_ASSOC));

I can't show you what i've tried as I don't know where to begin dispite several rephrased searches :-/

Dharman
  • 30,962
  • 25
  • 85
  • 135
Glen Keybit
  • 296
  • 2
  • 15
  • 2
    You can also look into using PDO with the fetch mode `PDO::FETCH_UNIQUE`. The benefit with PDO is not only that it's an abstraction layer (you can swap database engines by changing the connection), it also have a clearer and easier API and it has all kinds of nifty [fetch modes](https://phpdelusions.net/pdo/fetch_modes) – M. Eriksson Oct 28 '21 at 14:48

1 Answers1

5

It is as simple as:

$sql = mysqli_query($conn,"SELECT * FROM `live`");
$liveuserdata = [];
while ($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)) {
    $liveuserdata[$row['ID']] = $row;
}
u_mulder
  • 54,101
  • 5
  • 48
  • 64