我正在使用curl和php从api检索数据。我能够从api成功获取响应并从中检索所需的数据。每当我尝试将数据传递到数组以能够将其返回时,都会出现我的问题,因为当我在数组上使用var_dump()时,它为空。任何有关如何使该数组返回其值的指针都很好
// decode json string into array
$output = json_decode($output, true);
// check if the supplied data is an array
if(is_array($output)){
//loop through the first level of the array
foreach($output as $key => $value) {
//assigning variable values
$btcDominance = ["btc_dominance"];
$errorMsg = $value["error_message"];
$errorCode = $value["error_code"];
$timestamp = $value["last_updated"];
//check if the next level is an array and loop through it
if(is_array($value)) {
foreach($value as $key => $values) {
//check if the next level is an array and loop through it
if(is_array($values)) {
foreach($values as $keys => $value3) {
//check if the next level is an array and loop through it
$marketCap = $value3["total_market_cap"];
$volume = $value3["total_volume_24h"];
$values = array('btc' => $btcDominance,
'errMsg' => $errorMsg);
return $values;
}//foreach3
}//isarray 3
}//foreach2
}//is array 2
}//foreach1
}//is array 1
}//end of qryCmc
我认为getValues函数可能是数组返回为空白的原因,但是如果我从该函数中删除“ =”“,则会收到警告:Cmc :: getValues()的参数1缺失>
function getValues($values ='') {
return $this->output;
}
}// end of Cmc
$cmc = new Cmc();
var_dump($cmc->getValues());