PHP - Parsing nested JSON array values -
i have hit wall , not sure causing this. parsing json file , creating variables. ones not nested in arrays work great. these 2 below not though , not sure why. $hail var value shows both hail , $wind var , puzzled why.
here snippet of code create variable value.
$hail = isset($currfeature['properties']['parameters']['hailsize'][0]); $wind = isset($currfeature['properties']['parameters']['windgust'][0]);
here how outputted , displayed in html displays shows $hail both var.
<div class="alerts-description"> hazards<br /><? if (isset($hail)) {echo $hail . '" hail';} ?><br /> <? if (isset($wind)) {echo $wind . '" mph winds';} ?></div>
example of array both hailsize , windgust nested under parameters , both [0]
[response] => avoid [parameters] => array ( [hailsize] => array ( [0] => 0.50 ) [windgust] => array ( [0] => 70 ) [vtec] => array ( [0] => /o.new.kfwd.fa.w.0008.170813t1318z-170813t1615z/ ) [eas-org] => array ( [0] => wxr )
any suggestions doing wrong or missing?
edit: link example code hit "run it" button"
http://rextester.com/eele62798
-thanks!
$hail = isset($currfeature['properties']['parameters']['hailsize'][0]);
the above code generate variable value true
or false
. never have value data.
the following php7 code possible solution.
<?php $json = '{"properties":{"parameters":{"hailsize":[0.50],"windgust":[70]}}}'; $currfeature = json_decode($json, true); $hail = $currfeature['properties']['parameters']['hailsize'][0] ?? null; $wind = $currfeature['properties']['parameters']['windgust'][0] ?? null; // check null if ( $hail !== null ) { echo $hail . '" hail'. php_eol; } // check null if ( $wind !== null ) { echo $wind . '" mph winds'. php_eol; } if ( empty($currfeature['properties']['parameters']['windgust'][1]) ) { echo "empty works check missing data\n"; } if ( ! isset($currfeature['properties']['parameters']['windgust'][1]) ) { echo "isset check missing data\n"; }
if run on command line following output:
0.5" hail 70" mph winds empty works check missing data isset check missing data
Comments
Post a Comment