views:

31

answers:

2

Hi All, 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"
    ]
  },{
  "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.

A: 

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

VolkerK
I take it that I can change echo "-- ", $msg, " --\n"; to show the table in my first post?
Gary
like something similar to ----- <table align="center" width="100%" bgcolor="red"> <tr><td><strong>Severe Weather Warning in effect for CANTERBURY</strong></td></tr></table>
Gary
or better still show a banner which would be easier to script?
Gary
You can put any markup code you like "around" $msg, php doesn't care.
VolkerK
A: 

This works well.... thanks to VolkerK

<?php
$img = 'warnbanner.png';
$s = file_get_contents("http://somesite.com/warnings"); // 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 $img;
    }
  }
}

?>
Gary