0

I have a three dimensional array and now I am trying to parse the useful information. I have had some difficulties with this. First I will show you the structure of the json and then we'll get to my code:

//Start
array (size=1)
  'coins' => 
    array (size=66)
      'Cachecoin' => 
        array (size=13)
          'tag' => string 'CACH' (length=4)
          'algorithm' => string 'Chacha (Nf13)' (length=13)
          'block_reward' => float 23.3
          'block_time' => int 900
          'last_block' => int 25263
          'difficulty' => float 1.5235383
          'difficulty24' => float 1.5260514266667
          'nethash' => float 7270607.9696705
          'exchange_rate' => float 0.00100001
          'market_cap' => string '$223,995.33' (length=11)
          'volume' => float 7.81123588043
          'profitability' => int 610
          'profitability24' => int 609
//end

What I tried to do is count the elements in the array 'coins', which is 66, and then loop over these elements while getting the useful information.

$jsonmining = file_get_contents('http://www.whattomine.com/coins.json');
$datamining = json_decode($jsonmining, true);

var_dump($datamining);

$coins = count($datamining['coins']);

echo $coins; //66

for ($i = 0; $i <= $coins; $i++) {
    $summary[] = array(
        'Coin' => $datamining['coins'][$i],
        'Difficulty' => $datamining['coins'][$i]['difficulty'],
        'Difficulty24' => $datamining['coins'][$i]['difficulty24'],
        'Profitability' => $datamining['coins'][$i]['profitability'],
        'Profitability24' => $datamining['coins'][$i]['profitability24']
    );
}
4
  • You're missing one level of array in your loop: "Cachecoin"? Commented Jun 22, 2014 at 22:30
  • That's what the [$i] stands for. It looks for each $i (66 times) until the max amount of coins is reached (thus the end of the 2nd array). Commented Jun 22, 2014 at 22:32
  • So, what's the question? Commented Jun 22, 2014 at 22:37
  • rob just solved this. Commented Jun 22, 2014 at 22:47

1 Answer 1

1

is this what you want?

foreach ($datamining['coins'] as $ct => $cv) {
$summary[] = array(
           'Coin' => $ct,
    'Difficulty' => $datamining['coins'][$ct]['difficulty'],
    'Difficulty24' => $datamining['coins'][$ct]['difficulty24'],
    'Profitability' => $datamining['coins'][$ct]['profitability'],
    'Profitability24' => $datamining['coins'][$ct]['profitability24']
     );
}
Sign up to request clarification or add additional context in comments.

Comments

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.