用数据库中的while循环填充数组

时间:2017-08-19 00:58:40

标签: php mysql arrays

我想通过paypayments api创建一个群众提款。 它看起来应该是这样的

$req = array(
    'wd[wd1][amount]' => 1.00, //amount will be fetched from database
    'wd[wd1][address]' => '1BitcoinAddress', //btc address from database
    'wd[wd1][currency]' => 'BTC',
    'wd[wd2][amount]' => 0.0001,
    'wd[wd2][address]' => '1BitcoinAddress',
    'wd[wd2][currency]' => 'BTC',
    'wd[wd3][amount]' => 0.0001,
    'wd[wd3][address]' => '1BitcoinAddress',
    'wd[wd3][currency]' => 'BTC',
//[wd4] for the fifth withdrawal and so on
    );

所以我想用while循环将所有提取填充到一个数组中 提前致谢 这就是我做的事情

    $q = "SELECT amount, ecurrency_address, 1week FROM Tablename WHERE status = 'Processed'";
    $r = mysqli_query($mysqli, $q) or die ("Error");

        $i = 0;
        $j = 0;
    while($f = mysqli_fetch_assoc($r)){
       ++$j;

        if($j === 2){
            $j = 1;
            $i = ++$i;
        }

        $y['wd'.$i]['amount']=$f['amount'];
        $y['wd'.$i]['ecurrency_address']=$f['ecurrency_address'];
        $y['wd'.$i]['currency']='BTC';
}

echo '<pre>';
print_r($y);

更新

我已经能够得到这个数组

Array
(
    [wd1] => Array
        (
        [amount] => 0.05
        [ecurrency_address] => 1BitcoinAddress
        [currency] => BTC
    )

[wd2] => Array
    (
        [amount] => 10
        [ecurrency_address] => 1BitcoinAddress
        [currency] => BTC
    )

[wd3] => Array
    (
        [amount] => 96
        [ecurrency_address] => 1BitcoinAddress
        [currency] => BTC
    )

)

实现&#39; wd [wd1] [金额]&#39;

1 个答案:

答案 0 :(得分:1)

要获得所需的数组,请替换此部分:

$y['wd'.$i]['amount']=$f['amount'];
$y['wd'.$i]['ecurrency_address']=$f['ecurrency_address'];
$y['wd'.$i]['currency']='BTC';

有了这个:

$y['wd[wd'.$i.'][amount]'] = $f['amount'];
$y['wd[wd'.$i.'][ecurrency_address]'] = $f['ecurrency_address'];
$y['wd[wd'.$i.'][currency]'] = $f['BTC'];