0

Ok I am getting info from poloniex API.

It comes back as an array like so:

Array ( [0] => Array ( [id] => 357988064 [currency] => DOGE [rate] => 0.00008000 [amount] => 3134.03982846 [duration] => 0.25540000 [interest] => 0.06403308 [fee] => -0.00960496 [earned] => 0.05442812 [open] => 2017-05-30 23:12:09 [close] => 2017-05-31 05:19:55 ) [1] => Array ... and so on

Now I am trying to get each element of the array into some sort of variable to use later.

So something like this:

$id=[id] 
$currency=[currency]
$rate=[rate]
$amount=[amount]
$duration=[duration]
$interest=interest]
$fee=fee]
$earned=[earned]
$open=[open]
$close=[close]

I want to put them into a mysqli DB

This is what i am using right now:

if (!$return) {
    echo "No Data";
    print_r($return);
} else {
    $arrlength=count($return);
    echo "this many results: " .$arrlength. "<br><br>";
    foreach($return as $x=>$x_value)
      {

        echo $return[0][0]. ": Currency: " .$return[0][1]. ". Rate: " .$return[0][2]. " Duration: " .$return[0][3]. " Interest: " .$return[0][4]. " Fee: " .$return[0][5]. " Earned: " .$return[0][6]. " Opened: " .$return[0][7]. " Closed: " .$return[0][8]. "<br>";

        //  insert into Database here
      }

    // print_r($return);
}

And all it is returning is

 : Currency: . Rate: Duration: Interest: Fee: Earned: Opened: Closed:

I am a newbie with arrays but i have had success with api's with Json this is 1st with Arrays.

Been tinkering for a few days with this and i just cant get the right combo

EDIT: for anyone who can use this info i went the answer below and to display it on one line i went like this.

if (!$return) {
    echo "No Data";
    //print_r($return);
} else {
    $arrlength=count($return);
    echo "this many results: " .$arrlength. "<br><br>";

foreach($return as $x=>$x_value){
    extract($x_value);
    echo $id."   " .$currency."   " .$rate."   " .$duration."   " .$interest."   " .$fee."   " .$earned."   " .strtotime($open)."   " .strtotime($close)."<br>";
    //  insert into Database here
    }
    // print_r($return);
}
1

5 Answers 5

2

Use Extract to convert array key to variable.

https://www.w3schools.com/php/func_array_extract.asp

http://php.net/manual/en/function.extract.php

Sign up to request clarification or add additional context in comments.

1 Comment

please avoid link only is the answer
1

Do like this:

foreach($return as $x=>$x_value)
{
foreach($x_value as $key=>$value){

    echo @$key.":".$return[$x][$key]."<br>";

    //  loop the insert query only columnname and values here
}
//or
extract($x_value);
//echo $id.':'.$currency......;
echo "insert into table_name set id='".$id."', currency='".$currency."',
      rate='".$rate."', amount='".$amount."', duration='".$duration."',
     interest='".$interest."', fee='".$fee."', earned='".$earned."'";
}

8 Comments

this is not working it is showing like this ` id:358010478: Currency: 358010478. Rate: 358010478 Duration: 358010478 Interest: 358010478 Fee: 358010478 Earned: 358010478 Opened: 358010478 Closed: 358010478 id:DOGE: Currency: DOGE. Rate: DOGE Duration: DOGE Interest:`
hey now that is very close! but to put into the DB i need it split by the ID and i would love to read it one line
question how could this work if i am looking for variable to a certain key? like above if i just want for [0] or say [76] ?
when you are extract that will convert key as variable ...If you have number then you can use foreach for better understanding
not sure im following? could you show an example where i know i want the varaibles from say [0] or maybe [98]
|
1

Try php extract() method.

// Static Array.
$data = array();
$data[0]['id']='357988064';
$data[0]['currency']='DOGE';
$data[0]['rate']='0.00008000';
$data[0]['amount']='3134.03982846';
$data[0]['duration']='0.25540000';
$data[0]['interest']='0.06403308';
$data[0]['fee']='-0.00960496';
$data[0]['earned']='0.05442812';

$data[1]['id']='314564564';
$data[1]['currency']='DOGE TWo';
$data[1]['rate']='0.00124564';
$data[1]['amount']='23135.153';
$data[1]['duration']='0.455254';
$data[1]['interest']='0.5456';
$data[1]['fee']='-0.5625';
$data[1]['earned']='0.5464';

// Loop and extract data.
foreach($data as $k=>$var){
    extract($var);
    $ins = "insert into `table_name` set `id`='".$id."', `currency`='".$currency."', `rate`='".$rate."', `amount`='".$amount."', `duration`='".$duration."', `interest`='".$interest."', `fee`='".$fee."', `earned`='".$earned."'";
    echo $ins."<br />";
}

Output

insert into `table_name` set `id`='357988064', `currency`='DOGE', `rate`='0.00008000', `amount`='3134.03982846', `duration`='0.25540000', `interest`='0.06403308', `fee`='-0.00960496', `earned`='0.05442812'
insert into `table_name` set `id`='314564564', `currency`='DOGE TWo', `rate`='0.00124564', `amount`='23135.153', `duration`='0.455254', `interest`='0.5456', `fee`='-0.5625', `earned`='0.5464'

1 Comment

that would work but i its a api and could come back with 300 plus results, thanks tho
1

PHP Multidimensional array to variable

<?php
$array = Array
(
    'success' => '1',
    'result' => array(
        'id' => '12345678ABCDEXXX',
        'userid' => '2950',
        'system_user_id' => '76',
        'coin' => 'TRX',
        'amount' => '11.110000',
        'feePercent' => '0',
        'feeAmount' => '0',
        'memoTag' => '',
        'networkFee' => '0',
        'address' => 'TESTADDRESSHERE',
        'confirmations' => '0',
        'callBackUrl' => 'www.xyz.com/test',
        'transactionStatus' => 'WaitingForTransaction',
        'transactionType' => 'Deposit',
        'createdOn' => '1643692929',
        'modifiedOn' => '1643692929',
        'expiredon' => '1644988929',
        'description' => 'This is description'
    )
);

function extractMultidimentional($array){
    foreach ($array as $key => $value) {
        if(is_array($value)){
            extractMultidimentional($value);
        }else{
            global ${$key};//Set as global 
            ${$key} = $value;//Assign with its value
        }
    }
}

extractMultidimentional($array);


// Now you can call each key as a variable
echo $success;//1
echo $system_user_id;//76
echo $expiredon;//1644988929

If you have multidimensional array and those have same key then the variable will store last array value

Comments

-1

You can convert associative array to variebles like bellow code:

$list = array( 'var1' => 'value1', 'var2' => 'value2' );
foreach ( $list as $key => $value ) {
   $$key = $value; 
}
echo $var1; //prints 'value1'
echo $var2; //prints 'value2'

It's a simple way and work very well

3 Comments

how does that work with my code above? im confused sorry
i tried it and does not do anything i need it to do. thanks
1. $list is not a replica of the sample input 2. Variable variables are a symptom of poor code design. 3. This answer is poorly explained to people who would benefit from it. Ultimately, I would not recommend that anyone use this approach. <- this is why I have downvoted this answer and voted to delete the post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.