How to parse a multidimensional JSON? - php
I've got a JSON object containing some voter registration information that I'm trying to parse. I would like to retrieve the PersonKey from this object and store it in a variable.
[{"VoterKey":"31165750","PersonKey":"31165750","RNCID":"9959639415","State":"DC","Juriscode":"1100000000","Jurisname":"District of Columbia","CountyFIPS":"0","MCD":"","CNTY":"","Town":"","Ward":"07","Precinct":"097","Ballotbox":"","PrecinctName":"097","CD":"0","SD":"0","LD":"0","LDS":"","NamePrefix":"","FirstName":"MARCUS","MiddleName":"L","LastName":"JONES","NameSuffix":"","Sex":"M","BirthYear":"1982","BirthMonth":"11","BirthDay":"27","OfficialParty":"D","RNCCalcParty":"5","StateVoterID":"000129543","JurisdictionVoterID":"","RegistrationDate":"20030521","VoterStatus":"A","PermAbs":"","SelfReportedDemographic":"","ModeledEthnicity":"E1","ModeledReligion":"P","ModeledEthnicGroup":"Z","HHSEQ":"194061","HTSEQ":"2","RegistrationAddressKey":"19811832","RegistrationAddr1":"231 51ST ST NE","RegistrationAddr2":"","RegCity":"WASHINGTON","RegSta":"DC","RegZip5":"20019","RegZip4":"5420","RADR_LastCleanse":"2013-02-04","RADR_LastGeoCode":"2013-02-04","MailingAddressKey":"19811832","MailingAddr1":"231 51ST ST NE","MailingAddr2":"","MailCity":"WASHINGTON","MailSta":"DC","MailZip5":"20019","MailZip4":"5420","MADR_LastCleanse":"2013-02-04","AreaCode":"---","TelephoneNUm":"-------"},{"VoterKey":"31145016","PersonKey":"31145016","RNCID":"9959778025","State":"DC","Juriscode":"1100000000","Jurisname":"District of Columbia","CountyFIPS":"0","MCD":"","CNTY":"","Town":"","Ward":"07","Precinct":"102","Ballotbox":"","PrecinctName":"102","CD":"0","SD":"0","LD":"0","LDS":"","NamePrefix":"","FirstName":"MARCUS","MiddleName":"J","LastName":"JONES","NameSuffix":"","Sex":"M","BirthYear":"1900","BirthMonth":"01","BirthDay":"01","OfficialParty":"N","RNCCalcParty":"3","StateVoterID":"100019252","JurisdictionVoterID":"","RegistrationDate":"20100716","VoterStatus":"A","PermAbs":"","SelfReportedDemographic":"","ModeledEthnicity":"E1","ModeledReligion":"P","ModeledEthnicGroup":"Z","HHSEQ":"194001","HTSEQ":"1","RegistrationAddressKey":"19888718","RegistrationAddr1":"109 36TH ST NE","RegistrationAddr2":"2","RegCity":"WASHINGTON","RegSta":"DC","RegZip5":"20019","RegZip4":"2602","RADR_LastCleanse":"2013-02-04","RADR_LastGeoCode":"2013-02-04","MailingAddressKey":"19888718","MailingAddr1":"109 36TH ST NE","MailingAddr2":"2","MailCity":"WASHINGTON","MailSta":"DC","MailZip5":"20019","MailZip4":"2602","MADR_LastCleanse":"2013-02-04","AreaCode":"---","TelephoneNUm":"-------"},{"VoterKey":"31371499","PersonKey":"31371499","RNCID":"9959695444","State":"DC","Juriscode":"1100000000","Jurisname":"District of Columbia","CountyFIPS":"0","MCD":"","CNTY":"","Town":"","Ward":"07","Precinct":"097","Ballotbox":"","PrecinctName":"097","CD":"0","SD":"0","LD":"0","LDS":"","NamePrefix":"","FirstName":"MARCUS","MiddleName":"D","LastName":"JONES","NameSuffix":"","Sex":"M","BirthYear":"1900","BirthMonth":"01","BirthDay":"01","OfficialParty":"D","RNCCalcParty":"5","StateVoterID":"070016862","JurisdictionVoterID":"","RegistrationDate":"20070822","VoterStatus":"A","PermAbs":"","SelfReportedDemographic":"","ModeledEthnicity":"E1","ModeledReligion":"P","ModeledEthnicGroup":"Z","HHSEQ":"194398","HTSEQ":"1","RegistrationAddressKey":"126729320","RegistrationAddr1":"5019 FITCH PL NE","RegistrationAddr2":"","RegCity":"WASHINGTON","RegSta":"DC","RegZip5":"20019","RegZip4":"5447","RADR_LastCleanse":"2013-02-04","RADR_LastGeoCode":"2013-02-04","MailingAddressKey":"126729320","MailingAddr1":"5019 FITCH PL NE","MailingAddr2":"","MailCity":"WASHINGTON","MailSta":"DC","MailZip5":"20019","MailZip4":"5447","MADR_LastCleanse":"2013-02-04","AreaCode":"---","TelephoneNUm":"-------"}]
Any suggestions how best to handle this?
Use json_decode -- here's the entry in the PHP manual.
Example:
$data = '[{"VoterKey":"31165750","PersonKey":"31165750","RNCID":"9959639415"}]';
$arr = json_decode($data);
$person_key = $arr[0]->PersonKey;
Related
How to echo item from geoip array in php
I want to get each item from geoplugin array separately. Here is my example code - $location_info = file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']); echo$location_info; and here the response in array a:24:{s:17:"geoplugin_request";s:12:"76.180.83.10";s:16:"geoplugin_status";i:200;s:15:"geoplugin_delay";s:3:"1ms";s:16:"geoplugin_credit";s:145:"Some of the returned data includes GeoLite data created by MaxMind, available from http://www.maxmind.com.";s:14:"geoplugin_city";s:7:"Buffalo";s:16:"geoplugin_region";s:8:"New York";s:20:"geoplugin_regionCode";s:2:"NY";s:20:"geoplugin_regionName";s:8:"New York";s:18:"geoplugin_areaCode";s:0:"";s:17:"geoplugin_dmaCode";s:3:"514";s:21:"geoplugin_countryCode";s:2:"US";s:21:"geoplugin_countryName";s:13:"United States";s:14:"geoplugin_inEU";i:0;s:19:"geoplugin_euVATrate";b:0;s:23:"geoplugin_continentCode";s:2:"NA";s:23:"geoplugin_continentName";s:13:"North America";s:18:"geoplugin_latitude";s:7:"42.9297";s:19:"geoplugin_longitude";s:8:"-78.7434";s:32:"geoplugin_locationAccuracyRadius";s:1:"5";s:18:"geoplugin_timezone";s:16:"America/New_York";s:22:"geoplugin_currencyCode";s:3:"USD";s:24:"geoplugin_currencySymbol";s:5:"$";s:29:"geoplugin_currencySymbol_UTF8";s:1:"$";s:27:"geoplugin_currencyConverter";s:1:"1";} Example - I want to get city and echo it from this array $city = ['geoplugin_city']; echo$city;
You forgot to do $location_info = unserialize($location_info); right after the connection.
Parsing an array from single field
I have a table that has a field that contains what appears to be an array in it. Is there an easy way to convert this into an array that I can work with? SELECT data FROM exp_cartthrob_item_options_options Print_r(data); Returns the following: a:19:{i:0;a:6:{s:12:"option_value";s:18:"cp-1202-faraglioni";s:11:"option_name";s:16:"1202 Faraglioni ";s:5:"price";s:0:"";s:5:"color";s:5:"Putty";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:1;a:6:{s:12:"option_value";s:21:"cp-1203-scala-fenicia";s:11:"option_name";s:18:"1203 Scala Fenicia";s:5:"price";s:0:"";s:5:"color";s:5:"Brown";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:2;a:6:{s:12:"option_value";s:19:"cp-1204-castiglione";s:11:"option_name";s:17:"1204 Castiglione ";s:5:"price";s:0:"";s:5:"color";s:5:"Beige";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:3;a:6:{s:12:"option_value";s:19:"cp-1205-villa-jovis";s:11:"option_name";s:17:"1205 Villa Jovis ";s:5:"price";s:0:"";s:5:"color";s:3:"Tan";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:4;a:6:{s:12:"option_value";s:16:"cp-1207-anacapri";s:11:"option_name";s:14:"1207 Anacapri ";s:5:"price";s:0:"";s:5:"color";s:10:"Warm Beige";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:5;a:6:{s:12:"option_value";s:20:"cp-1208-monte-solaro";s:11:"option_name";s:17:"1208 Monte Solaro";s:5:"price";s:0:"";s:5:"color";s:10:"Warm Beige";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:6;a:6:{s:12:"option_value";s:13:"cp-BLCK-black";s:11:"option_name";s:10:"BLCK Black";s:5:"price";s:0:"";s:5:"color";s:6:"Black ";s:13:"grain_texture";s:5:"Full ";s:6:"finish";s:5:"Light";}i:17;a:6:{s:12:"option_value";s:21:"cd-0061-mexican-ochre";s:11:"option_name";s:18:"0061 Mexican Ochre";s:5:"price";s:0:"";s:5:"color";s:10:"Warm Beige";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:18;a:6:{s:12:"option_value";s:12:"cd-0063-soho";s:11:"option_name";s:9:"0063 SoHo";s:5:"price";s:0:"";s:5:"color";s:6:"Orange";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:19;a:6:{s:12:"option_value";s:13:"cd-0064-cocoa";s:11:"option_name";s:10:"0064 Cocoa";s:5:"price";s:0:"";s:5:"color";s:5:"Brown";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:20;a:6:{s:12:"option_value";s:18:"cd-0065-brownstone";s:11:"option_name";s:15:"0065 Brownstone";s:5:"price";s:0:"";s:5:"color";s:5:"Brown";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:21;a:6:{s:12:"option_value";s:13:"cd-0066-tabac";s:11:"option_name";s:10:"0066 Tabac";s:5:"price";s:0:"";s:5:"color";s:5:"Brown";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:22;a:6:{s:12:"option_value";s:18:"cd-0067-toro-black";s:11:"option_name";s:15:"0067 Toro Black";s:5:"price";s:0:"";s:5:"color";s:5:"Black";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:23;a:6:{s:12:"option_value";s:23:"cd-0068-sun-dried-brick";s:11:"option_name";s:20:"0068 Sun-Dried Brick";s:5:"price";s:0:"";s:5:"color";s:12:"Medium Brown";s:13:"grain_texture";s:5:"Full ";s:6:"finish";s:5:"Light";}i:24;a:6:{s:12:"option_value";s:14:"cd-0069-gothic";s:11:"option_name";s:11:"0069 Gothic";s:5:"price";s:0:"";s:5:"color";s:5:"Brown";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:25;a:6:{s:12:"option_value";s:23:"cd-0070-rembrandt-brown";s:11:"option_name";s:23:"0070 Rembrandt Brown ";s:5:"price";s:0:"";s:5:"color";s:12:"Medium Brown";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:27;a:6:{s:12:"option_value";s:15:"cd-0078-pompeii";s:11:"option_name";s:12:"0078 Pompeii";s:5:"price";s:0:"";s:5:"color";s:3:"Red";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:30;a:6:{s:12:"option_value";s:21:"cd-0082-gulmard-green";s:11:"option_name";s:18:"0082 Gulmard Green";s:5:"price";s:0:"";s:5:"color";s:10:"Dark Green";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:31;a:6:{s:12:"option_value";s:21:"cd-0084-prussian-blue";s:11:"option_name";s:18:"0084 Prussian Blue";s:5:"price";s:0:"";s:5:"color";s:9:"Dark Blue";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}}
Yes, you can use unserialize() to achieve that: SELECT data FROM exp_cartthrob_item_options_options $array = unserialize($data); var_dump($array);
Get specific text from JSON file
I am looking for this type of text in a json file: {"id":"*****number*******","name":"****namehere******"} For example: {"id":"100007015692915","name":"Ida Ka"} The JSON file is a complicated JSON file that looks like this (this is part of the JSON file): updated_time":"2014-02-19T15:15:31+0000","likes":{"data":[{"id":"1487307502","name":"Svatava Dohnalov\u00e1"},{"id":"662920773","name":"Alena Al Hr\u010dkov\u00e1"},{"id":"100007015692915","name":"Ida Ka"},{"id":"100000533191263","name":"Zlati V\u00e1\u0148ov\u00e1"},{"id":"1658256962","name":"Ji\u0159\u00ed Jager"},{"id":"1054706643","name":"Pavla Bubikova Winter"}],"paging": How can I recover all of these names and ids from a JSON file, and put them in an array? I am using PHP and the solution would include regex probably.
You're looking for json_decode. $json = '{"id":"100007015692915","name":"Ida Ka"}'; $arr = json_decode($json, true); print_r($arr); Returns: Array ( [id] => 100007015692915 [name] => Ida Ka )
$data = json_decode($json, true); $like_data = $data['likes']['data']; foreach($like_data as $like) { echo "ID: {$like['id']} Name: {$like['name']}<br/>"; }
You can use this regex: "id":"(\d+)","name":"(.*?)" Check the capturing groups in this working demo MATCH 1 1. [65-75] `1487307502` 2. [85-107] `Svatava Dohnalov\u00e1` MATCH 2 1. [117-126] `662920773` 2. [136-162] `Alena Al Hr\u010dkov\u00e1` MATCH 3 1. [172-187] `100007015692915` 2. [197-203] `Ida Ka` MATCH 4 1. [213-228] `100000533191263` 2. [238-265] `Zlati V\u00e1\u0148ov\u00e1` MATCH 5 1. [275-285] `1658256962` 2. [295-315] `Ji\u0159\u00ed Jager` MATCH 6 1. [325-335] `1054706643` 2. [345-366] `Pavla Bubikova Winter`
Decoding JSON into a PHP array, removing identical entries and encoding back to PHP
I'm having some trouble figuring out how to take JSON results from a MySQL query; turn them into a PHP array; remove the identical fields from that array, and turn the array back into JSON. The [1] is the part of the row with the actual JSON in it. Am I missing something? Having trouble finding any similar questions on the site. Thanks! $data = mysql_fetch_row($result); print_r($data); $json = json_decode($data[1], TRUE); var_dump($json); print_r($json); $distinctresult = array_unique($json); print_r($distinctresult); $final = json_encode($distinctresult); {"rows":[{"level":"ERROR","key":"Standard Not found","value":"RI.1.8"},{"level":"ERROR","key":"Standard Not found",{"level":"ERROR","key":"Standard Not found","value":"RI.K.9"},{"level":"ERROR","key":"Standard Not found","value":"RI.K.9"},{"level":"ERROR","key":"Standard Not found","value":"RI.K.9",}]} Here's the MySQL query I'm using: "select distinct d.valueField from etllogs t inner join etllogdetails d on t.uid = d.etllogID and d.valueField like '%ERROR%' where t.transformationName like 'CM Data Extract' and (t.timestamp >= (now() - interval 24 hour)) order by t.timestamp desc;";
It seems that you are trying to access array elements in a JSON encoded string ($data[1]). I had success with the following code: $data = array(0=>array('column1'=>'value1','column2'=>'value2'), 1=>array('column3'=>'value3','column4'=>'value3')); $data_json=json_encode($data); echo"ORIGINAL JSON:<pre>".print_r($data_json,true)."</pre>"; $data_php=json_decode($data_json,true); echo"PHP ARRAY:<pre>".print_r($data_php,true)."</pre>"; $data_chunk=$data_php[1]; echo"PHP ARRAY CHUNK:<pre>".print_r($data_chunk,true)."</pre>"; $distinctresult = array_unique($data_chunk); echo"UNIQUE CHUNK:<pre>".print_r($distinctresult,true)."</pre>"; $final = json_encode($distinctresult); echo"FINAL JSON:<pre>".print_r($final,true)."</pre>"; http://phpfiddle.org/main/code/7dg-nnb
Json Decode Presentation With Category
I got these Json data: [{"category":"Pizza","name":"Beef Pronto","desc":"Description of Beef Pronton here","price":"12"}, {"category":"Drink","name":"Cool Delight","desc":"Description of Coold Delight here","price":"5"}, {"category":"Drink","name":"Cola","desc":"Description of Cola","price":"4"} ] With Javascript I have successfully managed data to present as follow: Pizza -Beef Pronto: Description of Beef Pronton here: 12 Drink -Cool Delight: Description of Coold Delight here: 5 -Cola: Description of Cola: 4 any Idea how to do it with PHP ? -->Ok guys, this is How I do it with PHP: <? $listedmenuJSON = '[{"category":"Pizza","name":"Beef Pronto","desc":"Description of Beef Pronton here","price":"12"}, {"category":"Drink","name":"Cool Delight","desc":"Description of Coold Delight here","price":"5"}, {"category":"Drink","name":"Cola","desc":"Description of Cola","price":"4"} ]'; $json_decoded = json_decode($listedmenuJSON); foreach ($json_decoded as $categoryvalue){ //echo $categoryvalue->category."<br/>"; $tempcategoryvalue[] = $categoryvalue->category; $arrunique = array_unique($tempcategoryvalue); } foreach ($arrunique as $tmpcategory){ echo '<br/><b>'.$tmpcategory.'</b></br>'; foreach ($json_decoded as $tempo){ if($tempo->category == $tmpcategory){ echo $tempo->name.'<br/>'; echo '<i>'.$tempo->desc.'.......</i>'; echo $tempo->price.'<br/>'; } } } ?> It will generate as following: Pizza Beef Pronto Description of Beef Pronton here.......12 Drink Cool Delight Description of Coold Delight here.......5 Cola Description of Cola.......4
If you got json in PHP Code Use Following line to convert json to PHP array and manipulate array for desire output $array = json_decode($json,TRUE); In javascript user below code to convert your json to array. var obj = jQuery.parseJSON(responce);
Try using the PHP function 'json_decode' (http://php.net/manual/en/function.json-decode.php). This will create an associative array in PHP. It should be pretty simple to access and manipulate data thereon.
If you use a SQL server then you can select form the table using "group by category" Else, you can just verify many times the arrays. SQL: <?php $query = "SELECT * FROM 'table' GROUP BY category having (category=\"Pizza\");" --SELECT * FROM 'table' --GROUP BY category --having (category="Drink"); if you use this in php you must use sql_query("query")