I have following JSON Data
"12-305":[{"catid":"12","fname":"SALADS","ord":"0","show":"1","free":"0","extra":"0","hasextra":"1","filterorder":"1","maxS":"6","Valid":"0","Phone":"1","Web":"1","ovalue":"All Salads","id":"305","icon":"","price":"0"}]
"12-270":[{"catid":"12","fname":"SALADS","ord":"1","show":"1","free":"0","extra":"0","hasextra":"1","filterorder":"1","maxS":"6","Valid":"0","Phone":"1","Web":"1","ovalue":"No Salad","id":"270","icon":"","price":"0"}],
"12-273":[{"catid":"12","fname":"SAUCE","ord":"2","show":"1","free":"0","extra":"0","hasextra":"1","filterorder":"1","maxS":"3","Valid":"0","Phone":"1","Web":"1","ovalue":"No Sauce","id":"273","icon":"","price":"0"}],
"15-19":[{"catid":"15","fname":"SAUCE","ord":"2","show":"1","free":"0","extra":"0","hasextra":"1","filterorder":"2","maxS":"3","Valid":"0","Phone":"1","Web":"1","ovalue":"BBQ Sauce","id":"19","icon":"","price":"0"}],
"12-19":[{"catid":"12","fname":"SAUCE","ord":"2","show":"1","free":"0","extra":"0","hasextra":"1","filterorder":"1","maxS":"3","Valid":"0","Phone":"1","Web":"1","ovalue":"BBQ Sauce","id":"19","icon":"","price":"0"}],
"15-20":[{"catid":"15","fname":"SAUCE","ord":"3","show":"1","free":"0","extra":"0","hasextra":"1","filterorder":"2","maxS":"3","Valid":"0","Phone":"1","Web":"1","ovalue":"Garlic Sauce","id":"20","icon":"","price":"0"}],
"12-20":[{"catid":"12","fname":"SAUCE","ord":"3","show":"1","free":"0","extra":"0","hasextra":"1","filterorder":"1","maxS":"3","Valid":"0","Phone":"1","Web":"1","ovalue":"Garlic Sauce","id":"20","icon":"","price":"0"}]`
I want to only grab rows that contains "catid":"12" or key is like "12-" is this posible with using php json?
$json = json_decode('...your json string...');
$rows_that_contains_catid_12 = array();
$allowed_catid_values = array("12","12-");
foreach($json as &$v) {
foreach($v as &$rv){
if(in_array($rv['catid'],$allowed_catid_values))
$rows_that_contains_catid_12[] = $rv;
}
}
$rows_that_contains_catid_12 holds your result...
Regards
I have found the solution by tweaking the Json key. Removed characters after "-" which left following Json File
"12":[{"catid":"12","fname":"SALADS","ord":"0","show":"1","free":"0","extra":"0","hasextra":"1","filterorder":"1","maxS":"6","Valid":"0","Phone":"1","Web":"1","ovalue":"All Salads","id":"305","icon":"","price":"0"}]
"12":[{"catid":"12","fname":"SALADS","ord":"1","show":"1","free":"0","extra":"0","hasextra":"1","filterorder":"1","maxS":"6","Valid":"0","Phone":"1","Web":"1","ovalue":"No Salad","id":"270","icon":"","price":"0"}],
"12":[{"catid":"12","fname":"SAUCE","ord":"2","show":"1","free":"0","extra":"0","hasextra":"1","filterorder":"1","maxS":"3","Valid":"0","Phone":"1","Web":"1","ovalue":"No Sauce","id":"273","icon":"","price":"0"}],
"15":[{"catid":"15","fname":"SAUCE","ord":"2","show":"1","free":"0","extra":"0","hasextra":"1","filterorder":"2","maxS":"3","Valid":"0","Phone":"1","Web":"1","ovalue":"BBQ Sauce","id":"19","icon":"","price":"0"}],
"12":[{"catid":"12","fname":"SAUCE","ord":"2","show":"1","free":"0","extra":"0","hasextra":"1","filterorder":"1","maxS":"3","Valid":"0","Phone":"1","Web":"1","ovalue":"BBQ Sauce","id":"19","icon":"","price":"0"}],
"15":[{"catid":"15","fname":"SAUCE","ord":"3","show":"1","free":"0","extra":"0","hasextra":"1","filterorder":"2","maxS":"3","Valid":"0","Phone":"1","Web":"1","ovalue":"Garlic Sauce","id":"20","icon":"","price":"0"}],
"12":[{"catid":"12","fname":"SAUCE","ord":"3","show":"1","free":"0","extra":"0","hasextra":"1","filterorder":"1","maxS":"3","Valid":"0","Phone":"1","Web":"1","ovalue":"Garlic Sauce","id":"20","icon":"","price":"0"}]`
And pulled only key names 12 with this code
//my Json data is stored in a txt file
$catid= 12;
$data= fopen($_SERVER['DOCUMENT_ROOT']."URL TO DATA",'r');
$json = fgets($data);
$subitems =json_decode($json, TRUE);
$subitems =$subitems[$catid];
Related
Trying to send arraylist data to server using php as bridge from Android (using Volley), and successfully getting data into database table (and also getting response as submit, I mentioned in php) in Android
So everything looks perfect, if we talk about db connection, storage and response, but one unusal issue always getting 2 in table field, instead the original value
::::imageDataInString::::: {"b":"2020-01-01_00:01:11.jpg"}
::::imageDataInString::::: {"b":"2020-01-01_00:01:21.jpg"}
::::imageDataInString::::: {"b":"2020-01-01_00:01:31.jpg"}
::::Response::::: <br />
<b>Warning</b>: Illegal string offset 'image_name' in <b>C:\xampp\htdocs\test\send_data.php</b> on line <b>15</b><br />
<br />
submit
Here is the foreach, I'm using in php to upload all the data available in a list
$content = $_POST['my_images'];
$json = json_decode($content, true);
foreach ($json as $key => $value) {
$image_name = $value["image_name"];
}
android code
ImageData imageData = new ImageData();
imageData.setImageName(array[0]);
String imageDataInString = new Gson().toJson(imageData);
Log.d("::::imageDataInString::::", imageDataInString);
....
params.put("my_images", imageDataInString);
in my csv, available in raw folder of res in Android, I have these data:
2020-01-01_00:01:11.jpg
2020-01-01_00:01:21.jpg
2020-01-01_00:01:31.jpg
There are a few points to note:
GSON converts the variable names of the class (ImageData) as keys and appends the values, to form the JSON. Hence, you need to use the same variable names, as you have used in the ImageData class to fetch the data, in your PHP Code.
ImageData imageData = new ImageData();
imageData.setImageName(array[0]);
String imageDataInString = new Gson().toJson(imageData);
Hence, the array key should be array["ImageName"]
In your PHP code, the second line, where you are JSON decoding the $content variable, the variable $json is already converted in an Array. You don't need to iterate and fetch corresponding keys.
Hence, you should do something like this:
$content = $_POST['my_images'];
$json = json_decode($content, true);
$image_name = $json["ImageName"];
With this, the variable $image_name will hold the value which you are looking for.
I am extracting data from a JSON file using a URL like this:
$html5=file_get_contents("url");
$data = json_decode($html5);
echo $data->a->k->f;
This displays $27.58 which is correct.
Here is my JSON data:
{
"a":{
"k":{
"c":"tr",
"f":"$27.58",
"fb":"$30.35",
"ci":"12873809",
"cname":"Cdkeysgame_com",
"ep":"26.05",
"epb":"28.66",
"a":"5473091",
"it":"steam",
"sl":"0",
"l":null,
"lt":null,
"x":0,
"v":"retail",
"ne":0,
"so":0,
"tr":304,
"r":97,
"cf":1,
"p":"27.58317275",
"pb":"30.3467843",
"eci":"865723abbf1cbb952ad4d2da8a8c925e"
},
"k_1":{
"c":"gb",
"f":"$27.64",
"fb":"$30.40",
"ci":"1065801",
"cname":"World_of_games",
"ep":"26.10",
"epb":"28.71",
"a":"781851",
"it":"steam",
"sl":"0",
"l":null,
"lt":null,
"x":0,
"v":"all",
"ne":0,
"so":0,
"tr":1041328,
"r":99,
"cf":1,
"p":"27.6361155",
"pb":"30.39972705",
"eci":"d01a7cacb0e424123985bfe2e53a0523"
},
"k_2":{
"c":"ch",
"f":"$27.68",
"fb":"$30.44",
"ci":"696012",
"cname":"G_hard",
"ep":"26.14",
"epb":"28.75",
"a":"1287052",
"it":"steam",
"sl":"0",
"l":null,
"lt":null,
"x":0,
"v":"retail",
"ne":0,
"so":0,
"tr":10818,
"r":99,
"cf":1,
"p":"27.6784697",
"pb":"30.44208125",
"eci":"a6666c0a47acb70d14b757cd52f1b9cc"
}}
I need to display the same data without using k. For example I want to:
echo $data->a->'Something that specifies first element of 'a' i.e k and not'k_1' and 'k_2'->f;
Since I am scraping this content I cannot change anything like structure or data in this JSON file.
If you just want the first entry under a, then you can decode as an array:
$data = json_decode($json, true);
Then get the first element of $data['a'] and use that. current() will also work:
echo reset($data['a'])['f'];
Or re-index $data['a'] numerically and access the first one 0:
echo array_values($data['a'])[0]['f'];
To catch them all:
foreach($data['a'] as $values) {
echo $values['f'];
//break; if you only want the first one
}
You can do this so:
$object = json_decode($html5);
foreach ($object->a as $element) {
echo $element->f;
}
How can i create one Json with many small sets of data separated by comma?
Instead of one big Json enclosed by double curly brackets?
I do receive a Json and with php i do use foreach to loop over it, making a lot of data processing inside.
Then generate a new Json, just to avoid the data processing on the client side wich will be processed by angularjs ng-repeat.
All the json data is mixed into one big json set (inside double curly brackets)
My goal is to separate into small sets of data.
I can use the NrType property. In this script the NrType receives the last atribution and just the last received is available.
//The php script
$arr = json_decode($returnedJson); //The original json to be pre-processed
$processedData = "[]";
$processedJson = json_decode($processedData,true);
foreach($arr as $key=>$value) {
foreach($value as $vkey=>$vvalue) {
if( $value[$i]->NrType == 1 ) {
$VlMIni = $value[$i]->QttyInitial;
$VlMSub = $value[$i]->QttyPeriod + $value[$i]->QttyRealAfter;
$VlMRec = $value[$i]->RealValue;
$VlMTotal += $VlMesRece;
//much more data processing going on here ...
} elseif( $value[$i]->NrType == 2 ) {
.
.
.
//and much more data processing going on here ...
}
}
//simple data atribution here
$processedJson['labelDesIni'] = 'Instruments';
$processedJson['labelValueMIni'] = $lblVlMIni;
$processedJson['labelValuePIni'] = $lblVlPlIni;
$processedJson['labelValueAIni'] = $lblVlAIni;
$processedJson['labelValuePAIni'] = $lblVlPAIni;
$processedJson['labelValuePercInic'] = $lblVlPercInic;
$processedJson['labelValuePerc2Inic'] = $lblVlPerc2Inic;
//much more data atribution ...
echo json_encode($processedJson); //the new hgenerated Json
The generated Json :
{
labelDesI: "Inspection",
labelValueMI: "2357",
labelValuePlI: "3914066",
labelValueAI: "1389406",
labelValuePAI: "2431425",
labelValuePercI: 57.143691456656,
labelValuePerc2I: 35.497766261478,
labelDesR: "Instruments",
labelValueMR: "734.54",
labelValuePR: "819.14",
labelValueAR: "660.05",
labelValuePAR: "877.94",
labelValuePercR: 80.087,
labelValuePerc2R: 44.739,
labelDesAcfi: "Fiscalização",
labelValueMAcfi: "343",
labelValuePlAcfi: "29907",
labelValueAAcfi: "16718",
labelValuePAAcfi: "16493",
labelValuePercAcfi: 101.36421512157,
labelValuePerc2Acfi: 55.899956531916,
labelDesT: "Totals",
labelValueMT: 365.59,
labelValuePlT: 547.62,
labelValueAnT: 909.63,
labelValuePAnT: 957.63,
labelValuePercT: 22949,
labelValuePerc2T: 25065
}
The desired format Would be this :
{
label: "Inspection",
labelValue1: "2357",
labelValue2: "3914066",
labelValue3: "1389406",
labelValue4: "2431425",
labelValue5: 57.1456656,
labelValue6: 35.4961478
},
{
labelDesR: "Instruments",
labelValue1: "734.54",
labelValue2: "819.14",
labelValue3: "660.05",
labelValue4: "877.94",
labelValue5: 80.087,
labelValue6: 44.739
},
{
labelDesT: "Totals",
labelValue1: 365.59,
labelValue2: 547.62,
labelValue3: 909.63,
labelValue4: 957.63,
labelValue5: 22949,
labelValue6: 25065
}
Thank´s in advance
generate all of your object separately, create an array of these objects and json_encode the array:
$processedJsonElement[] = ['labelDesT' => "Totals", 'labelValue1' => $whatTheValueIs, . . .];
and add it to you main object:
$processedJson[] = $processedJsonElement;
do this for each section of Json you want to represent. Not sure how you're structuring your Foreach loop as your code doesn't match the output, but whatever you structure is whatever you will will output when you call json_encode.
Basically, you need to structure your foreach loop to be able to compartmentalize the objects you wish to represent as an array of json objects.
I've some JSON data in a javascript file
Data is build using javascript object literal notation;
var CMS = window.CMS = window.CMS || {};
CMS.Data = window.CMS.Data = window.CMS.Data || {};
CMS.Data['LANGUAGES'] = [
{id:"chi", value:"Chinese"},
{id:"spa", value:"Spanish"},
{id:'eng', value:"English"},
{id:"hin", value:"Hindi"},
];
CMS.Data['AGE_RANGE'] = [{id:'18', value:"18"}, {id:'25', value:"25"}, {id:'30', value:"30"}, {id:'35', value:"35"}, {id:'40', value:"40"}, {id:'50', value:"50"}, {id:'60', value:"60"}, {id:'70', value:"70"}, {id:'80', value:"80"}, {id:'90', value:"90"}, {id:'100', value:"100"}];
CMS.Data['HEIGHT_RANGE'] = [{id:'140-150', value:"140-150"}, {id:'150-160', value:"150-160"}, {id:'160-170', value:"160-170"}, {id:'180-190', value:"180-190"}];
Complete data is available at https://gist.github.com/mithunqb/675613fe985fe2afbbcf
I need to make it available in PHP array.
I cannot simply load the content and apply json_decode
$json_string = file_get_contents('data.json');
$json_array = json_decode($json_string, true);
As the content inside the json file is not actually a valid JSON string, the above operation will result NULL;
How can I do this?
If the JSON string is not valid then there is no way to convert it to PHP array. Not a chance at any circumstances. It's obvious.
You must provide a valid JSON string.
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