phpmysqlarrayscoinpayments-api

populate array with while loop from database


i want to create a mass withdrawal with coinpayments api. and it should look like this

$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
    );

So i want to populate all withdrawals into one array with while loop thanks in advance this is how i did it

    $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);

UPDATE

I have been able to get this array

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
    )

)

stacked at achieving 'wd[wd1][amount]'


Solution

  • To achieve the array required, replace this part:

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

    With this:

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