数据库中的PHP数组,将一行设置为数组标题

我得到以下代码来从数据库中获取数据以存储在PHP的数组中:


<?php 

    $keyemail = "testmail@testmailer.com";

    $sql = mysqli_query($con, "SELECT * FROM crypto_data WHERE email = '$keyemail'");


    while($row = mysqli_fetch_array($sql)){

    $myCoins[] = array($row['currency'], $row['amount'], $row['price_when_bought']);

    }


    var_dump($myCoins);

?>

我得到的数组看起来像这样:


array(2) {

    [0] => array(3) {

        [0] => string(3) "BTC"[1] => string(1) "2"[2] => string(2) "23"

    }

    [1] => array(3) {

        [0] => string(3) "IOT"[1] => string(1) "6"[2] => string(2) "74"

    }

}

如何更改我的代码以获取如下所列的阵列?这样$ row ['currency']是每个数组的数组标题。


array(2) {

    ["BTC"] => array(2) {

        ["balance"] => int(2) ["boughtprice"] => int(23)

    }

    ["ETH"] => array(2) {

        ["balance"] => int(6) ["boughtprice"] => int(74)

    }

}


噜噜哒
浏览 160回答 1
1回答

一只萌萌小番薯

只是改变$myCoins[]&nbsp;=&nbsp;array($row['currency'],&nbsp;$row['amount'],&nbsp;$row['price_when_bought']);至$myCoins[$row['currency']]&nbsp;=&nbsp;array('balance'&nbsp;=>&nbsp;$row['amount'],&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'boughtprice'&nbsp;=>&nbsp;$row['price_when_bought']);
打开App,查看更多内容
随时随地看视频慕课网APP