php - Showing JSON data using foreach loop -


i have following code:

<?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); # reference  ?>   <table border="1">     <tr>         <td><?php if (!empty($string)) {                 foreach ($string $key => $value) {                     echo $value['data_type'];                 }             } else {                 echo 'there nothing here';             }              ?>         </td>     </tr>    </table> 

what trying json data url , create foreach loop goes through items in array. have converted raw data associative array . having trouble not know use key reference each object. i've tried couple of things , experimented using keys lists in raw data. help, or point in right direction appreciated.

edit:

this getting line says referencing it:

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 (+) [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 (-) [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 [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 [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 [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 [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 [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 [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 [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 ) ) 

and code outputs this:

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 (+) [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 (-) [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 [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 [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 [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 [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 [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 [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 [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 

it looks me gives me value object entitled "data_type" 'm trying figure out how reference 1 of arrays instead of printing out of them.

you can reference individual object in $string array using it's index such $string[0].

edit

if wanted group objects data_type separate arrays can use following;

<?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 $key => $value) {         $power_type_objects[$value['data_type']][] = $string[$key];     }      echo '<pre>';     print_r($power_type_objects); # reference     echo '</pre>';  ?> 

which give following output:

[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 (+)                 [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             )      ) 

....


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -