我得到以下代码来从数据库中获取数据以存储在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 回答
一只萌萌小番薯
TA贡献1795条经验 获得超7个赞
只是改变
$myCoins[] = array($row['currency'], $row['amount'], $row['price_when_bought']);
至
$myCoins[$row['currency']] = array('balance' => $row['amount'], 'boughtprice' => $row['price_when_bought']);
- 1 回答
- 0 关注
- 159 浏览
添加回答
举报
0/150
提交
取消