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 :-/

1
  • 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 Commented Oct 28, 2021 at 14:48

1 Answer 1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.