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

时间:2019-04-20 13:24:23

标签: php mysql arrays

我得到了这段代码来从数据库中获取数据以存储在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)
    }
}

1 个答案:

答案 0 :(得分:1)

只是改变

java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.Double

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