开发者

Targetting a specific part of json array, if present create table

I have a complex json array which I want to target a specific line/string and if present then add a small table to web page...

This is the array

{
"coastalWarnings":[{
  "loc":"ABEL",
  "warn":"GALE"
  },{
  "loc":"CASTLEPOINT",
  "warn":"STORM"
  },{
  "loc":"CHALMERS",
  "warn":"GALE"
  },{
  "loc":"CHATHAM ISLANDS",
  "warn":"GALE"
  },{
  "loc":"CONWAY",
  "warn":"STORM"
  },{
  "loc":"COOK",
  "warn":"STORM"
  },{
  "loc":"FOVEAUX",
  "warn":"GALE"
  },{
  "loc":"GREY",
  "warn":"GALE"
  },{
  "loc":"MILFORD",
  "warn":"STORM"
  },{
  "loc":"PORTLAND",
  "warn":"GALE"
  },{
  "loc":"PUYSEGUR",
  "warn":"STORM"
  },{
  "loc":"RAGLAN",
  "warn":"GALE"
  },{
  "loc":"RANGITATA",
  "warn":"GALE"
  },{
  "loc":"STEPHENS",
  "warn":"GALE"
  }
  ],
"isAdvisory":true,
"isOutlook":true,
"isVhf":true,
"isWatch":true,
"liftedWarnings":[
  ],
"oceanicWarnings":[{
  "loc":"FORTIES",
  "warn":"GALE"
  },{
  "loc":"PACIFIC",
  "warn":"GALE"
  },{
  "iceAccretion":true,
  "loc":"SOUTHERN",
  "warn":"GALE"
  },{
  "loc":"SUBTROPIC",
  "warn":"GALE"
  }
  ],
"roadSnowWarnings":[
  ],
"severeWarnings":[{
  "loc":"BULLER",
  "warn":["ISSUE"
    ]
  },{
  "loc":"CANTERBURY",
  "warn":["ISSUE"
    ]
  },{
  "loc":"FIORDLAND",
  "warn":["ISSUE"
    ]
  },{
  "loc":"HAWKES BAY",
  "warn":["ISSUE"
    ]
  },{
  "loc":"MANAWATU",
  "warn":["ISSUE"
    ]
  },{
  "loc":"MARLBOROUGH",
  "warn":["ISSUE"
 开发者_JAVA技巧   ]
  },{
  "loc":"NELSON",
  "warn":["ISSUE"
    ]
  },{
  "loc":"OTAGO",
  "warn":["ISSUE"
    ]
  },{
  "loc":"SOUTHLAND",
  "warn":["ISSUE"
    ]
  },{
  "loc":"TAIHAPE",
  "warn":["ISSUE"
    ]
  },{
  "loc":"TARANAKI",
  "warn":["ISSUE"
    ]
  },{
  "loc":"TAUMARUNUI",
  "warn":["ISSUE"
    ]
  },{
  "loc":"TAUPO",
  "warn":["ISSUE"
    ]
  },{
  "loc":"WAIRARAPA",
  "warn":["ISSUE"
    ]
  },{
  "loc":"WAITOMO",
  "warn":["ISSUE"
    ]
  },{
  "loc":"WANGANUI",
  "warn":["ISSUE"
    ]
  },{
  "loc":"WELLINGTON",
  "warn":["ISSUE"
    ]
  },{
  "loc":"WESTLAND",
  "warn":["ISSUE"
    ]
  }
  ],
"thunderstormWarnings":[
  ]
}

As you can see the array is broken down into parts -

"coastalWarnings"
"liftedWarnings"
"oceanicWarnings"
"roadSnowWarnings"
"severeWarnings"
"thunderstormWarnings"

I wish to target "severeWarnings" and in particular "loc":"CANTERBURY" which displays as -

  },{
  "loc":"CANTERBURY",
  "warn":["ISSUE"
    ]
  },{

so when present in array it will trigger a table such as

<table align="center" width="100%" bgcolor="red">
     <tr><td><strong>Severe Weather Warning in effect for CANTERBURY</strong></td></tr>
</table>

The biggest problem is the array changes all the time so need to target I guess by searching for specific words or wording unless it can be broken down into parts reliably.

Could something as simple as this work?

<?
$table = <table align="center" width="100%" bgcolor="red"><tr><td><strong>Severe Weather Warning in effect for CANTERBURY</strong></td></tr></table>;

$file = file_get_contents("filename.ext");
if(!strpos($file, ""loc":"CANTERBURY"")) {
echo $table;
}
?

Unsure how to go about this and my php skills are fairly limited so if you would be so kind as to show me a script or a step by step on how to achieve this, I would be ever so appreciative.

Thanks in advance.


As Alex suggested, use json_decode

$s = getData(); // returning the string from the question
$data = json_decode($s, true);

foreach( $data['severeWarnings'] as $cw ) {
  if ( 'CANTERBURY'===$cw['loc'] ) {
    foreach( $cw['warn'] as $msg ) {
      echo "-- ", $msg, " --\n";
    }
  }
}

prints -- ISSUE --

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜