Fullhdpixel
Fullhdpixel

Reputation: 813

Add three dimensional array values into a new array in PHP

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']
    );
}

Upvotes: 0

Views: 106

Answers (1)

Rob
Rob

Reputation: 224

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']
     );
}

Upvotes: 1

Related Questions