Thanks in prior for advising. I have the following JSON I retrieve from a REST Service.
"events": [
{
"id": 408,
"name": "My Test1",
"modulename": "test",
"instance": 0,
"timestart": 1470370500,
"timeduration": 864000,
},
{
"id": 418,
"name": "Quiz Open",
"modulename": "quiz",
"instance": 225,
"timestart": 1473737880,
"timeduration": 0,
},
{
"id": 423,
"name": "Quiz Close",
"modulename": "quiz",
"instance": 225,
"timestart": 1476665040,
"timeduration": 0,
},
{
"id": 409,
"name": "August event",
"modulename": "event",
"instance": 0,
"timestart": 1467295200,
"timeduration": 2474700,
}
]
I need to read the JSON and output in the following JSON format
"output": [
{
"name":
"Start":
"End":
}
]
My question comes in the point of the name as "Quiz" which is the "modulename" in the JSON input. The Quiz opens at a date (timestart) with modulename as "quiz" and "instance" - 225 as the unique identifier and ends at a different date ( timestart ) with the same instance id. The json contains 100s of events in one big array. My "End" in output json object is the "timestart" + "timeduration".
My Question is what is the effective algorithm to iterate if the module name is "quiz" get the instance and loop through the whole array to find the next occurence of the instance and add to the output json. Any pointers ?
At first make sure that your json is in right format if your json is like that:-
$str='{"events": [
{
"id": 408,
"name": "My Test1",
"modulename": "test",
"instance": 0,
"timestart": 1470370500,
"timeduration": 864000
},
{
"id": 418,
"name": "Quiz Open",
"modulename": "quiz",
"instance": 225,
"timestart": 1473737880,
"timeduration": 0
},
{
"id": 423,
"name": "Quiz Close",
"modulename": "quiz",
"instance": 225,
"timestart": 1476665040,
"timeduration": 0
},
{
"id": 409,
"name": "August event",
"modulename": "event",
"instance": 0,
"timestart": 1467295200,
"timeduration": 2474700
}
]}';
Then you can do this....`
$arr=json_decode($str,true);
$result=array();
$prevInstance=array();
foreach($arr['events'] as $key=>$val){
if($val['modulename']=='quiz' && array_key_exists($val['instance'], $prevInstance)){
$k= $prevInstance[$val['instance']];
$result['output'][$k]['End']=$val['timestart'];
}else{
$tmp['name']=$val['name'];
$tmp['Start']=$val['timestart'];
$tmp['End']=$val['timeduration'];
$result['output'][$val['id']]=$tmp;
}
$prevInstance[$val['instance']]=$val['id'];;
}
$data=array();
foreach ($result['output'] as $ret){
$data['output'][]=$ret;
}
echo "<pre>";
echo json_encode($data);
$array = json_decode($event,true); // first decode your retrieved array
$output = array();
foreach ($array as $key) { // then loop through it
if ($array[$key]['modulename'] == "quiz") { // only select if modulename is "quiz"
$obj = array("name" => $array[$key]['name'], "Start" => $array[$key]['timestart'], "END" => ($array[$key]['timestart'] + $array[$key]['timeend']));
array_push($output, $obj);
} else {
return;
}
}
return json_encode($output); // in last for output in JSON, encode the output array
//output: [{"name":"quiz","Start":123,"END":579},{"name":"quiz","Start":123,"END":579},{"name":"quiz","Start":123,"END":579}]
Related
I have question, is it possible not to duplicate the array object by looping on it? Right now I used laravel as my backend
I have here my response which is the exchange object duplicate itself.
[
{
"exchange": {
"id": 1,
"branch": "BB1",
"old_check_no": "0001",
"cash": "250000",
"bank_deposit": "1000000",
"offset": "250000",
"amount": "10000",
"over_under": null,
"checkDate": "2021-09-11",
"remarks": "1",
"date_closed": "2021-09-11"
},
"exchange_list": {
"exchange_id": 1,
"new_check_no": "001",
"new_check_bank": "bank",
"new_check_branch": "Lagros"
}
},
{
"exchange": {
"id": 1,
"branch": "BB1",
"old_check_no": "0001",
"cash": "250000",
"bank_deposit": "1000000",
"offset": "250000",
"amount": "10000",
"over_under": null,
"checkDate": "2021-09-11",
"remarks": "1",
"date_closed": "2021-09-11"
},
"exchange_list": {
"exchange_id": 1,
"new_check_no": "002",
"new_check_bank": "bank",
"new_check_branch": "Lagros"
}
},
]
Now my goal is to push the exchange without duplication:
[
{
"exchange": {
"id": 1,
"branch": "BB1",
"old_check_no": "0001",
"cash": "250000",
"bank_deposit": "1000000",
"offset": "250000",
"amount": "10000",
"over_under": null,
"checkDate": "2021-09-11",
"remarks": "1",
"date_closed": "2021-09-11"
},
"exchange_list": {
"exchange_id": 1,
"new_check_no": "001",
"new_check_bank": "bank",
"new_check_branch": "Lagros"
},
"exchange_list": {
"exchange_id": 1,
"new_check_no": "002",
"new_check_bank": "bank",
"new_check_branch": "Lagros"
}
}
]
Here is what my foreach loop like and how i push the array object.
$myArray = [];
foreach($exchange_check as $primary_array) {
foreach($exchange_lists as $second_array) {
if($second_array->exchange_id == $primary_array->id) {
array_push($myArray, (object)[
'exchange' => $primary_array,
'exchange_list' => $second_array,
]);
}
}
}
Thanks
You should add exchange data to array only once in first loop:
$myArray = [];
foreach($exchange_check as $primary_array) {
$idx = array_push($myArray, ['exchange' => $primary_array]);
foreach($exchange_lists as $second_array) {
if($second_array->exchange_id == $primary_array->id) {
//array_push() returns the new number of elements in the array,
//to get currently added array element we should subtract 1 from this number
$myArray[$idx-1]['exchange_list'][] = $second_array;
}
}
}
And in second loop add only exchange_list data to array.
I am creating a json file from my array:
$file = json_encode($array);
The json file will look like this:
[
{
"name": "file1.html",
"date": "2019-01-29T20:33:57.000163Z",
"size": "348"
}
{
"name": "file2.xml",
"date": "2019-01-29T20:33:57.000167Z",
"size": "401"
}
{
"name": "file3.html",
"date": "2019-01-29T20:33:57.000171Z",
"size": "1314"
}
]
But I need to create a json file with some little bit different format. The output I need is:
{
"draw": 1,
"recordsTotal": 5000,
"recordsFiltered": 5000,
"data": [
{
"name": "file1.html",
"date": "2019-01-29T20:33:57.000163Z",
"size": "348"
}
{
"name": "file2.xml",
"date": "2019-01-29T20:33:57.000167Z",
"size": "401"
}
{
"name": "file3.html",
"date": "2019-01-29T20:33:57.000171Z",
"size": "1314"
}
]
}
Is this possible with json_encode?
Create a new array with rest of the info and assign current array data into it as well.
$newArray = array(
'draw'=> 1,
'recordsTotal'=> 5000,
'recordsFiltered'=> 5000,
'data'=>$array
);
$file = json_encode($newArray);
I have the table "orders" from woocommerce.
I filtered and get sorted the elements by status with GET request.
The results are copied to a JSON file.
So now, I want to find the elements with the same "product_id" and their values and summarize them to display in screen so I can print them.
For example:
"product_id": 45329,
"variation_id": 0,
"quantity": 1
"product_id": 48911,
"variation_id": 0,
"quantity": 1,
"product_id": 45329,
"variation_id": 0,
"quantity": 1
The output that I want to achieve is this:
45329 quantity 2
48911 quantity 1
Thanks!
Decode JSON with json_decode() and make calculations. Next example uses simplified JSON data, which I think matches the format from WooCommerce (I guess this format is from list orders GET request):
<?php
# JSON
$json = '
[
{
"id": 727,
"line_items": [
{
"id": 315,
"name": "Woo Single #1",
"product_id": 93,
"variation_id": 0,
"quantity": 2,
"tax_class": "",
"subtotal": "6.00",
"subtotal_tax": "0.45",
"total": "6.00",
"total_tax": "0.45",
"taxes": [
{
"id": 75,
"total": "0.45",
"subtotal": "0.45"
}
],
"meta_data": [],
"sku": "",
"price": 3
},
{
"id": 316,
"name": "Ship Your Idea – Color: Black, Size: M Test",
"product_id": 22,
"variation_id": 23,
"quantity": 1,
"tax_class": "",
"subtotal": "12.00",
"subtotal_tax": "0.90",
"total": "12.00",
"total_tax": "0.90",
"taxes": [
{
"id": 75,
"total": "0.9",
"subtotal": "0.9"
}
],
"meta_data": [
{
"id": 2095,
"key": "pa_color",
"value": "black"
},
{
"id": 2096,
"key": "size",
"value": "M Test"
}
],
"sku": "Bar3",
"price": 12
}
]
}
]';
$input = json_decode($json, true);
# Sum
$output = array();
foreach ($input as $order) {
foreach ($order["line_items"] as $item) {
$product_id = $item['product_id'];
$quantity = $item['quantity'];
if (!array_key_exists($product_id, $output)) {
$output[$product_id] = 0;
}
$output[$product_id] += $quantity;
}
}
#Output
foreach ($output as $key => $value) {
echo $key.' quantity '.$value.'<br>';
}
?>
Output:
93 quantity 2
22 quantity 1
I'm trying to figure out how to echo the address1 out of the JSON below. I've tried this - echo "$arr->location[1]->address1<br>";, but it returns this error
Catchable fatal error: Object of class stdClass could not be converted to string in /home/benrud/public_html/student/webdesign/2016/02_benrud/tinker/data/index.php on line 202.
echo $arr; returns the JSON below.
{
"photos": [
"https://s3-media2.fl.yelpcdn.com/bphoto/37El1q8mqM_1tKtQugncZQ/o.jpg",
"https://s3-media1.fl.yelpcdn.com/bphoto/GLsNPPz5do-_NJktIQvz6w/o.jpg",
"https://s3-media3.fl.yelpcdn.com/bphoto/Z4rdHERgb10MZgDXnct5lA/o.jpg"
],
"coordinates": {
"latitude": 33.0479031276,
"longitude": -117.256002333
},
"image_url": "https://s3-media1.fl.yelpcdn.com/bphoto/37El1q8mqM_1tKtQugncZQ/o.jpg",
"is_claimed": false,
"id": "oscars-mexican-seafood-encinitas-2",
"review_count": 48,
"rating": 4.5,
"hours": [
{
"hours_type": "REGULAR",
"is_open_now": true,
"open": [
{
"is_overnight": false,
"end": "2100",
"day": 0,
"start": "0800"
},
{
"is_overnight": false,
"end": "2100",
"day": 1,
"start": "0800"
},
{
"is_overnight": false,
"end": "2100",
"day": 2,
"start": "0800"
},
{
"is_overnight": false,
"end": "2100",
"day": 3,
"start": "0800"
},
{
"is_overnight": false,
"end": "2200",
"day": 4,
"start": "0800"
},
{
"is_overnight": false,
"end": "2200",
"day": 5,
"start": "0800"
},
{
"is_overnight": false,
"end": "2100",
"day": 6,
"start": "0800"
}
]
}
],
"display_phone": "(760) 487-5778",
"categories": [
{
"alias": "seafood",
"title": "Seafood"
},
{
"alias": "mexican",
"title": "Mexican"
}
],
"price": "$",
"phone": "+17604875778",
"name": "Oscars Mexican Seafood",
"location": {
"zip_code": "92024",
"address3": null,
"address1": "115 N El Camino Real",
"country": "US",
"city": "Encinitas",
"state": "CA",
"cross_streets": "Via Molena & Encinitas Blvd",
"display_address": [
"115 N El Camino Real",
"Encinitas, CA 92024"
],
"address2": ""
},
"transactions": [],
"url": "https://www.yelp.com/biz/oscars-mexican-seafood-encinitas-2?adjust_creative=YqqOIA_bNY3Qb_A1TRMMUg&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_lookup&utm_source=YqqOIA_bNY3Qb_A1TRMMUg",
"is_closed": false
}
Location isn't an array, so I'd imagine just $arr->location->address1.
$arr = json_decode($json, true);
the true parameter makes sure it's an array and not an object
You must first decode then call the key so:
// Decode the JSON STRING
$arr = json_decode($arr, true);
/*
* first argument is the JSON STRING,
* Second sets the flag that the string is a dictionary
* (associative array)
*/
Now it is time to call the element. I put it in a conditional so to prevent errors
if (array_key_exists('address1', $arr['location'])) {
echo $arr['location']['address1'];
}
else {
echo "Array element Not Found. Here is what I have:\n\r";
print_r($arr);
}
This Should return your element's value OR Dump the parsed PHP Array for review so you can edit the if statement to get the correct location.
I have the php file
<?php
$str = '{
"champions": [{
"id": 24,
"stats": {
"armor": 27.04,
"attackrange": 125.0,
}
}, {
"id": 37,
"stats": {
"armor": 20.544,
"attackrange": 550.0,
}
}],
"matches": [{
"timestamp": 1433644800,
"champion": 427,
"lane": "TOP"
}, {"timestamp": 1453702800,
"champion": 103,
"lane": "MIDDLE"
}]
}';
$array = json_decode($str,true);// read string to array (true means array, false means object)
var_dump($array);
$champions = $array["champions"];
var_dump($champions);
which outputs null for both var_dumps. What is my mistake? Thanks. Is it maybe a problem that there are square brakets in the json snippet?
You have errors in your JSON.
Remove , in the end of [champions][stats] arrays.
please remove comma separation from every last element of champions->stats like below then do decode
{
"champions": [
{
"id": 24,
"stats": {
"armor": 27.04,
"attackrange": 125
}
},
{
"id": 37,
"stats": {
"armor": 20.544,
"attackrange": 550
}
}
],
"matches": [
{
"timestamp": 1433644800,
"champion": 427,
"lane": "TOP"
},
{
"timestamp": 1453702800,
"champion": 103,
"lane": "MIDDLE"
}
]
}