simonp Posted February 19, 2010 Share Posted February 19, 2010 Hi, I have a script that outputs: [{"domain":"testdomain","tld":".net","result":"Taken"},{"domain":"testdomain","tld":".com","result":"Taken"},{"domain":"testdomain","tld":".mobi","result":"Taken"},{"domain":"testdomain","tld":".biz","result":"Taken"},{"domain":"testdomain","tld":".info","result":"Taken"}] (or similar). I believe this is JSON (which I've not used before!) which I understand is just like XML - human-readable results of a query. So I now need to present this in a nice way in a web page. Is there something special in PHP I can use to do this? Hope someone can help! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/192672-dealing-with-json/ Share on other sites More sharing options...
salathe Posted February 19, 2010 Share Posted February 19, 2010 PHP has JSON support: see http://php.net/json Quote Link to comment https://forums.phpfreaks.com/topic/192672-dealing-with-json/#findComment-1015009 Share on other sites More sharing options...
simonp Posted February 19, 2010 Author Share Posted February 19, 2010 Cheers - I can use json_decode which gives me an array: Array ( [0] => Array ( [domain] => testdomain [tld] => .net [result] => Taken ) [1] => Array ( [domain] => testdomain [tld] => .com [result] => Taken ) [2] => Array ( [domain] => testdomain [tld] => .mobi [result] => Taken ) [3] => Array ( [domain] => testdomain [tld] => .biz [result] => Taken ) [4] => Array ( [domain] => testdomain [tld] => .info [result] => Taken ) ) But how do I display that nicely on a page? I think I need to use something like foreach but I've always been confused by arrays. Any help welcome! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/192672-dealing-with-json/#findComment-1015046 Share on other sites More sharing options...
salathe Posted February 19, 2010 Share Posted February 19, 2010 Here's a loop which outputs some of those values, it should give you an idea of what to do. $json = '[{"domain":"testdomain","tld":".net","result":"Taken"},{"domain":"testdomain","tld":".com","result":"Taken"},{"domain":"testdomain","tld":".mobi","result":"Taken"},{"domain":"testdomain","tld":".biz","result":"Taken"},{"domain":"testdomain","tld":".info","result":"Taken"}]'; $data = json_decode($json, TRUE); foreach ($data as $domain) { echo $domain['domain'] . $domain['tld'] . ' is ' . $domain['result'] . '<br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/192672-dealing-with-json/#findComment-1015050 Share on other sites More sharing options...
simonp Posted February 19, 2010 Author Share Posted February 19, 2010 Thanks salathe - that works great - will do some playing now Quote Link to comment https://forums.phpfreaks.com/topic/192672-dealing-with-json/#findComment-1015053 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.