我有以下代码:
<?php
$json = file_get_contents('http://application.deckmonitoring.com/data/latest.json?target_type=node&target_id=1&token=12345678901234567890');
$string = json_decode($json, TRUE);
print_r($string); # for reference
?>
<table border="1">
<tr>
<td><?php if (!empty($string)) {
foreach ($string as $key => $value) {
echo $value['data_type'];
}
} else {
echo 'There is nothing here';
}
?>
</td>
</tr>
</table>
我要做的是从url获取一些JSON数据并创建一个遍历数组中项目的foreach循环。我已将原始数据转换为关联数组。我遇到的问题是,我不知道用什么键来引用每个对象。我尝试了几件事并尝试使用它在原始数据中列出的密钥。一些帮助,或正确方向的一点将不胜感激。
修改
这是我从说明我引用它的那一行得到的:
Array ( [0] => Array ( [time] => 2014-03-10 12:15:00 [seconds_ago] => 1275.3669655323 [data_type] => power [unit] => kW [value] => 0.059038 ) [1] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.73035812378 [data_type] => AC Energy (+) A [unit] => kWh [value] => 0.002 ) [2] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.66972780228 [data_type] => AC Energy (+) B [unit] => kWh [value] => 0.001 ) [3] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.69577527046 [data_type] => AC Energy (+) C [unit] => kWh [value] => 1491.76475 ) [4] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.78512835503 [data_type] => AC Energy (-) A [unit] => kWh [value] => 41.6215 ) [5] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.49988245964 [data_type] => AC Energy (-) B [unit] => kWh [value] => 134.34525 ) [6] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.56096410751 [data_type] => AC Energy (-) C [unit] => kWh [value] => 0 ) [7] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.54191684723 [data_type] => AC Energy A [unit] => kWh [value] => -41.6185 ) [8] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.4393093586 [data_type] => AC Energy B [unit] => kWh [value] => -134.34475 ) [9] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.52092242241 [data_type] => AC Energy C [unit] => kWh [value] => 1491.76625 ) [10] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.3975083828 [data_type] => AC Power A [unit] => kW [value] => -0.001582 ) [11] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.50329494476 [data_type] => AC Power B [unit] => kW [value] => -2.9E-5 ) [12] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.39441418648 [data_type] => AC Power C [unit] => kW [value] => 0.060649 ) [13] => Array ( [time] => 2013-09-19 00:15:43 [seconds_ago] => 14905232.37968 [data_type] => AC Watt Hours [unit] => Wh [value] => 0 ) [14] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.674295187 [data_type] => Apparent Power A [unit] => kVA [value] => 0.001691 ) [15] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.52429628372 [data_type] => Apparent Power B [unit] => kVA [value] => 9.3E-5 ) [16] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.56830334663 [data_type] => Apparent Power C [unit] => kVA [value] => 0.067142 ) [17] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.64755892754 [data_type] => Energy Apparent [unit] => kWh [value] => 1855.07525 ) [18] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.45766568184 [data_type] => Energy Apparent A [unit] => kWh [value] => 60.18575 ) [19] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.75435805321 [data_type] => Energy Apparent B [unit] => kWh [value] => 146.94025 ) [20] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.58755683899 [data_type] => Energy Apparent C [unit] => kWh [value] => 1647.94875 ) [21] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.62746334076 [data_type] => Energy Reactive [unit] => kWh [value] => -609.10125 ) [22] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.60714936256 [data_type] => Energy Reactive A [unit] => kWh [value] => 35.615 ) [23] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.41941714287 [data_type] => Energy Reactive B [unit] => kWh [value] => 55.14325 ) [24] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.47503495216 [data_type] => Energy Reactive C [unit] => kWh [value] => -699.862 ) [25] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.47842907906 [data_type] => Power Factor A [unit] => pf [value] => -0.00935 ) [26] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.48141813278 [data_type] => Power Factor B [unit] => pf [value] => -0.00311 ) [27] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.75786519051 [data_type] => Power Factor C [unit] => pf [value] => 0.00903 ) [28] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.70001840591 [data_type] => Power Reactive [unit] => kVAR [value] => -0.028296 ) [29] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.56455540657 [data_type] => Power Reactive A [unit] => kVAR [value] => 0.000598 ) [30] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.76123142242 [data_type] => Power Reactive B [unit] => kVAR [value] => -8.9E-5 ) [31] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.70475888252 [data_type] => Power Reactive C [unit] => kVAR [value] => -0.028806 ) [32] => Array ( [data_type] => demand today [unit] => kWh [value] => 0.707 ) [33] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.35778808594 [data_type] => generation [unit] => kWh [value] => 1421.3425 ) [34] => Array ( [data_type] => generation today [unit] => kWh [value] => 0.707 ) [35] => Array ( [time] => 2011-05-13 15:40:07 [seconds_ago] => 89153768.815151 [data_type] => net generation [unit] => kWh [value] => -54567 ) [36] => Array ( [time] => 2011-05-05 20:41:15 [seconds_ago] => 89826900.386709 [data_type] => predicted power [unit] => kW [value] => 8.8158451675 ) [37] => Array ( [time] => 2011-05-05 20:41:15 [seconds_ago] => 89826900.386604 [data_type] => predicted power [unit] => kW [value] => 8.8158451675 ) [38] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.82890295982 [data_type] => reactive power [unit] => kVAR [value] => 0 ) [39] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 375.8258600235 [data_type] => received power [unit] => kW [value] => 0 ) [40] => Array ( [time] => 2011-05-05 20:41:15 [seconds_ago] => 89826900.389256 [data_type] => temperature [unit] => °C [value] => 15.783333333333 ) [41] => Array ( [time] => 2012-10-09 07:00:00 [seconds_ago] => 44688975.37283 [data_type] => value [unit] => # [value] => 0 ) )
代码输出:
Array ( [0] => Array ( [time] => 2014-03-10 12:15:00 [seconds_ago] => 1415.2291824818 [data_type] => power [unit] => kW [value] => 0.059038 ) [1] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.70772218704 [data_type] => AC Energy (+) A [unit] => kWh [value] => 0.002 ) [2] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.65894651413 [data_type] => AC Energy (+) B [unit] => kWh [value] => 0.001 ) [3] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.68033218384 [data_type] => AC Energy (+) C [unit] => kWh [value] => 1491.76475 ) [4] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.75129461288 [data_type] => AC Energy (-) A [unit] => kWh [value] => 41.6215 ) [5] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.47772955894 [data_type] => AC Energy (-) B [unit] => kWh [value] => 134.34525 ) [6] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.53927516937 [data_type] => AC Energy (-) C [unit] => kWh [value] => 0 ) [7] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.52115464211 [data_type] => AC Energy A [unit] => kWh [value] => -41.6185 ) [8] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.3775832653 [data_type] => AC Energy B [unit] => kWh [value] => -134.34475 ) [9] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.49898386002 [data_type] => AC Energy C [unit] => kWh [value] => 1491.76625 ) [10] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.30272579193 [data_type] => AC Power A [unit] => kW [value] => -0.001582 ) [11] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.48113703728 [data_type] => AC Power B [unit] => kW [value] => -2.9E-5 ) [12] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.29951524734 [data_type] => AC Power C [unit] => kW [value] => 0.060649 ) [13] => Array ( [time] => 2013-09-19 00:15:43 [seconds_ago] => 14905372.266252 [data_type] => AC Watt Hours [unit] => Wh [value] => 0 ) [14] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.66258645058 [data_type] => Apparent Power A [unit] => kVA [value] => 0.001691 ) [15] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.50254368782 [data_type] => Apparent Power B [unit] => kVA [value] => 9.3E-5 ) [16] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.54574537277 [data_type] => Apparent Power C [unit] => kVA [value] => 0.067142 ) [17] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.6393969059 [data_type] => Energy Apparent [unit] => kWh [value] => 1855.07525 ) [18] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.42687487602 [data_type] => Energy Apparent A [unit] => kWh [value] => 60.18575 ) [19] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.7258336544 [data_type] => Energy Apparent B [unit] => kWh [value] => 146.94025 ) [20] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.56782674789 [data_type] => Energy Apparent C [unit] => kWh [value] => 1647.94875 ) [21] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.61093878746 [data_type] => Energy Reactive [unit] => kWh [value] => -609.10125 ) [22] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.59347748756 [data_type] => Energy Reactive A [unit] => kWh [value] => 35.615 ) [23] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.32574605942 [data_type] => Energy Reactive B [unit] => kWh [value] => 55.14325 ) [24] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.44656801224 [data_type] => Energy Reactive C [unit] => kWh [value] => -699.862 ) [25] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.45034480095 [data_type] => Power Factor A [unit] => pf [value] => -0.00935 ) [26] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.45925021172 [data_type] => Power Factor B [unit] => pf [value] => -0.00311 ) [27] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.72936868668 [data_type] => Power Factor C [unit] => pf [value] => 0.00903 ) [28] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.68556404114 [data_type] => Power Reactive [unit] => kVAR [value] => -0.028296 ) [29] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.54261255264 [data_type] => Power Reactive A [unit] => kVAR [value] => 0.000598 ) [30] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.73253774643 [data_type] => Power Reactive B [unit] => kVAR [value] => -8.9E-5 ) [31] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.68865466118 [data_type] => Power Reactive C [unit] => kVAR [value] => -0.028806 ) [32] => Array ( [data_type] => demand today [unit] => kWh [value] => 0.707 ) [33] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.20262622833 [data_type] => generation [unit] => kWh [value] => 1421.3425 ) [34] => Array ( [data_type] => generation today [unit] => kWh [value] => 0.707 ) [35] => Array ( [time] => 2011-05-13 15:40:07 [seconds_ago] => 89153908.7698 [data_type] => net generation [unit] => kWh [value] => -54567 ) [36] => Array ( [time] => 2011-05-05 20:41:15 [seconds_ago] => 89827040.274846 [data_type] => predicted power [unit] => kW [value] => 8.8158451675 ) [37] => Array ( [time] => 2011-05-05 20:41:15 [seconds_ago] => 89827040.274708 [data_type] => predicted power [unit] => kW [value] => 8.8158451675 ) [38] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.78152966499 [data_type] => reactive power [unit] => kVAR [value] => 0 ) [39] => Array ( [time] => 2014-03-10 12:30:00 [seconds_ago] => 515.77783679962 [data_type] => received power [unit] => kW [value] => 0 ) [40] => Array ( [time] => 2011-05-05 20:41:15 [seconds_ago] => 89827040.277765 [data_type] => temperature [unit] => °C [value] => 15.783333333333 ) [41] => Array ( [time] => 2012-10-09 07:00:00 [seconds_ago] => 44689115.258793 [data_type] => value [unit] => # [value] => 0 ) )
powerAC Energy (+) AAC Energy (+) BAC Energy (+) CAC Energy (-) AAC Energy (-) BAC Energy (-) CAC Energy AAC Energy BAC Energy CAC Power AAC Power BAC Power CAC Watt HoursApparent Power AApparent Power BApparent Power CEnergy ApparentEnergy Apparent AEnergy Apparent BEnergy Apparent CEnergy ReactiveEnergy Reactive AEnergy Reactive BEnergy Reactive CPower Factor APower Factor BPower Factor CPower ReactivePower Reactive APower Reactive BPower Reactive Cdemand todaygenerationgeneration todaynet generationpredicted powerpredicted powerreactive powerreceived powertemperaturevalue
在我看来,它给了我来自该对象的价值&#34; data_type&#34;但是我试图弄清楚如何引用其中一个数组而不是打印出所有数组。
答案 0 :(得分:1)
您可以使用它的索引(如$ string [0])引用$ string数组中的单个对象。
修改的
如果要将对象按其data_type分组为单独的数组,可以使用以下内容;
<?php
$json = file_get_contents('http://application.deckmonitoring.com/data/latest.json?target_type=node&target_id=1&token=12345678901234567890');
$string = json_decode($json, true);
$power_type_objects = array();
foreach ($string as $key => $value) {
$power_type_objects[$value['data_type']][] = $string[$key];
}
echo '<pre>';
print_r($power_type_objects); # for reference
echo '</pre>';
?>
将提供以下输出:
[power] => Array ( [0] => Array ( [time] => 2014-03-10 14:15:00 [seconds_ago] => 1890.1586380005 [data_type] => power [unit] => kW [value] => 0.059942 ) ) [AC Energy (+) A] => Array ( [0] => Array ( [time] => 2014-03-10 14:30:00 [seconds_ago] => 990.66510391235 [data_type] => AC Energy (+) A [unit] => kWh [value] => 0.002 ) ) [AC Energy (+) B] => Array ( [0] => Array ( [time] => 2014-03-10 14:30:00 [seconds_ago] => 990.61706137657 [data_type] => AC Energy (+) B [unit] => kWh [value] => 0.001 ) )
...