Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've never worked with JSON or multidimensional arrays and I'm quite lost atm... I try to write the api data from coinwarz into a MySQL...

what i get:

{"Success":true,"Message":"999 api calls remaining.","Data":[{"CoinName":"Tagcoin","CoinTag":"TAG","Algorithm":"Scrypt","Difficulty":48.8082576,"BlockReward":30,"BlockCount":35404,"ProfitRatio":8152.1820299065312,"AvgProfitRatio":5538.592714864395,"Exchange":"Cryptsy","ExchangeRate":0.0015114,"ExchangeVolume":9100.58740501,"IsBlockExplorerOnline":true,"IsExchangeOnline":true,"Message":"Warning: The blockchain for this coin is currently having issues. Waiting on a fix from the coin development team.","BlockTimeInSeconds":240,"HealthStatus":"Unhealthy"},{"CoinName":"Lottocoin","CoinTag":"LOT","Algorithm":"Scrypt","Difficulty":12.15941137,"BlockReward":32896,"BlockCount":136845,"ProfitRatio":6884.8588238209768,"AvgProfitRatio":4448.5624688234475,"Exchange":"Cryptsy","ExchangeRate":2.9E-07,"ExchangeVolume":382804480.23628891,"IsBlockExplorerOnline":true,"IsExchangeOnline":true,"Message":"","BlockTimeInSeconds":30,"HealthStatus":"Healthy"},{"CoinName":"RonPaulcoin","CoinTag":"RPC","Algorithm":"Scrypt","Difficulty":30.33885125,"BlockReward":1,"BlockCount":15421,"ProfitRatio":6189.8644790289609,"AvgProfitRatio":8893.1681194248831,"Exchange":"Cryptsy","ExchangeRate":0.0214,"ExchangeVolume":1719.73666494,"IsBlockExplorerOnline":true,"IsExchangeOnline":true,"Message":"","BlockTimeInSeconds":120,"HealthStatus":"Healthy"},{"CoinName":"Franko","CoinTag":"FRK","Algorithm":"Scrypt","Difficulty":1.00168131,"BlockReward":0.25,"BlockCount":406926,"ProfitRatio":6176.3105781107906,"AvgProfitRatio":2511.7635333807175,"Exchange":"Cryptsy","ExchangeRate":0.00282002,"ExchangeVolume":17011.99409075,"IsBlockExplorerOnline":true,"IsExchangeOnline":true,"Message":"","BlockTimeInSeconds":30,"HealthStatus":"Healthy"},

I decoded ($json_array = json_decode($json_source, true);) and got this:

Array ( [Success] => 1 
[Message] => 1,000 api calls remaining. 
[Data] => Array ( 

[0] => Array ( [CoinName] => Tagcoin 
[CoinTag] => TAG 
[Algorithm] => Scrypt 
[Difficulty] => 20.62668283 
[BlockReward] => 30 
[BlockCount] => 35339 
[ProfitRatio] => 19310.667769105 
[AvgProfitRatio] => 6361.5043045259 
[Exchange] => Cryptsy 
[ExchangeRate] => 0.001513 
[ExchangeVolume] => 7711.03038846 
[IsBlockExplorerOnline] => 1 
[IsExchangeOnline] => 1 
[Message] => Warning: The blockchain for this coin is currently having issues. Waiting on a fix from the coin development team. 
[BlockTimeInSeconds] => 240 
[HealthStatus] => Unhealthy ) 

[1] => Array ( 
[CoinName] => Dogecoin 
[CoinTag] => DOGE 
[Algorithm] => Scrypt 
[Difficulty] => 1055.45317084 
[BlockReward] => 500000 
[BlockCount] => 76150 
[ProfitRatio] => 7025.6132683628 
[AvgProfitRatio] => 5524.6490817022 
[Exchange] => Vircurex 
[ExchangeRate] => 1.69E-6 
[ExchangeVolume] => 100375791.08525 
[IsBlockExplorerOnline] => 1 
[IsExchangeOnline] => 1 
[Message] => 
[BlockTimeInSeconds] => 60 
[HealthStatus] => Healthy ) 

[2] => Array ( 
[CoinName] => BBQCoin 
[CoinTag] => BQC 
[Algorithm] => Scrypt 
[Difficulty] => 2.04374031 
[BlockReward] => 42 
[BlockCount] => 742772 
[ProfitRatio] => 5980.0470554874 
[AvgProfitRatio] => 2822.26794798 
[Exchange] => Cryptsy 
[ExchangeRate] => 3.316E-5 
[ExchangeVolume] => 81528.3775313 
[IsBlockExplorerOnline] => 1 
[IsExchangeOnline] => 1 
[Message] => 
[BlockTimeInSeconds] => 60 
[HealthStatus] => Healthy ) 

So if I want so save it into a mysql i would insert

  • $json_array[0][CoinName]
  • .
  • .
  • $json_array[1][CoinName]
  • .
  • .

and so on with a foreach.

Is there a better way?


Tried it this way:

<?php

$db_host                                = "localhost";
$db_user                                = "test";
$db_pw                                  = "test";
$db_name                                = "test";

$db_connect = mysql_connect($db_host,$db_user,$db_pw); // MYSQL-Connection
mysql_select_db($db_name, $db_connect); // DB-Connection


$url = 'http://127.0.0.1/jsontestdaten.json';
$json_source = file_get_contents($url);

// DUMP JSON
//$data = json_decode($JSON);
//var_dump($data);

$json_array = json_decode($json_source, true);


if(is_array($json_array)){
echo 'i am in array ';
    $sql = "INSERT INTO data (id, coinname, cointag, algorithm, difficulty, blockreward, blockcount, profitratio, avgprofitratio, exchange, exchangerate, exchangevolume, isblockexploreronline, isexchangeonline, message, blocktimeinsecon
        $valuesArr = array();
                foreach($json_array as $row){
                        $coinname               = $row['CoinName'];
                        $cointag                = $row['CoinTag'];
                        $algorithm              = $row['Algorithm'];
                        $difficulty             = $row['Difficulty'];
                        $blockreward            = $row['BlockReward'];
                        $blockcount             = $row['BlockCount'];
                        $profitratio            = $row['ProfitRatio'];
                        $avgprofitratio         = $row['AvgProfitRatio'];
                        $exchange               = $row['Exchange'];
                        $exchangerate           = $row['ExchangeRate'];
                        $exchangevolume         = $row['ExchangeVolume'];
                        $isblockexploreronline  = $row['IsBlockExplorerOnline'];
                        $isexchangeonline       = $row['IsExchangeOnline'];
                        $message                = $row['Message'];
                        $blocktimeinseconds     = $row['BlockTimeInSeconds'];
                        $healthstatus           = $row['HealthStatus'];

$valuesArr[] = "('', '$coinname', '$algorithm','$difficulty','$blockreward','$blockcount','$profitratio','$avgprofitratio','$exchange','$exchangerate','$exchangevolume', '$isblockexploreronline','$message','$blocktimeinseconds','$health
                        }
$sql .= implode(',', $valuesArr);


$update = mysql_query($sql);
}

?>

But var_dump($sql); shows:

string(443) "INSERT INTO data (id, coinname, cointag, algorithm, difficulty, blockreward, blockcount, profitratio, avgprofitratio, exchange, exchangerate, exchangevolume, isblockexploreronline, isexchangeonline, message, blocktimeinseconds, healthstatus, datetime) values ('', '', '','','','','','','','','', '','','','', NOW()),('', '9', '9','9','9','9','9','9','9','9','9', '9','9','9','9', NOW()),('', '', '','','','','','','','','', '','','','', NOW())"

What's my mistake?

share|improve this question
    
what about this: stackoverflow.com/questions/10054633/… –  Martin Turjak Jan 28 at 15:53
1  
Since you are using MySQL, you can insert multiple rows at once. If you iterate and perform one insert for each record, you will have significantly more overhead. –  bishop Jan 28 at 15:53
    
tried it with the given example and added output in question... cant get it to work... –  alex Jan 28 at 18:39

1 Answer 1

up vote 0 down vote accepted

You are on the right Track.

foreach($json_array as $apiData){
  //mysql mapping
}

mysql mapping is in this case the way you map the different values (example: $apiData[CoinName]) to the specific Row.

For bigger Results this could cause a long Loading time. So maybe you should look, that you dont do too much at Once. Most API's please you, to only fetch the data you realy need for your Task. If those are general Game Database informations, do a One Time Import Script, to store them. Then it doesnt matter how long it takes. Because you as admin insert them only once.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.