好的我从poloniex API获取信息。
它以类似的形式返回:
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
现在我试图将数组的每个元素放入某种变量中以便稍后使用。
这样的事情:
$id=[id]
$currency=[currency]
$rate=[rate]
$amount=[amount]
$duration=[duration]
$interest=interest]
$fee=fee]
$earned=[earned]
$open=[open]
$close=[close]
我想将它们放入mysqli DB
这就是我现在正在使用的:
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);
}
所有它返回的是
: Currency: . Rate: Duration: Interest: Fee: Earned: Opened: Closed:
我是阵列的新手,但是我和Json一起成功了,这是阵列的第一名。
用这个修补了几天,我无法得到正确的组合
编辑: 对于任何可以使用此信息的人,我得到了下面的答案并将其显示在一行上,我就这样了。
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);
}
答案 0 :(得分:2)
使用3.5.2
将数组键转换为变量。
答案 1 :(得分:1)
这样做:
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."'";
}
答案 2 :(得分:1)
尝试使用php extract()
方法。
// 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 />";
}
<强>输出强>
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'
答案 3 :(得分:0)
您可以将关联数组转换为变量,如下面的代码:
$list = array( 'var1' => 'value1', 'var2' => 'value2' );
foreach ( $list as $key => $value ) {
$$key = $value;
}
echo $var1; //prints 'value1'
echo $var2; //prints 'value2'
这是一种简单的方法并且运作良好