在PHP中将三维数组值添加到新数组中

时间:2014-06-22 21:56:45

标签: php arrays

我有一个三维数组,现在我正在尝试解析有用的信息。我遇到了一些困难。首先,我将向您展示json的结构,然后我们将获得我的代码:

//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

我尝试做的是计算数组'coins'中的元素,即66,然后在获取有用信息的同时循环遍历这些元素。

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

1 个答案:

答案 0 :(得分:1)

这就是你想要的吗?

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