How to save JSON into ini-file with php? - php

I have a datastream as JSON, I want to parse it and want to save result in ini-file :
{
"books": [{
"id": "1",
"date": "2017-03-12",
"date_text": "sunday 12 march",
"title": "title text"
}, {
"id": "2",
"date": "2017-03-12",
"date_text": "sunday 12 march",
"title": "title text"
}]
}
This is my sample data and I would like to know if there is a way to save it into a file no matter if it contain 1 or more "id:s" (Items)
I know how to parse the JSON but not how to save it down to a file in correct format for an ini.
Preferable format:
[Books 0]
id= 1
date= 2017-03-12
date_text=sunday 12 march
title= title text
[Books 1]
id"=2
date=2017-03-12
date_text=sunday 12 march
title=title text

You can try Zend Config for this task. Open your terminal and add zend-config to your project as dependency (assuming you already using composer):
composer require zendframework/zend-config
Now you can try following,
$json = <<<JSON
{
"books": [{
"id": "1",
"date": "2017-03-12",
"date_text": "sunday 12 march",
"title": "title text"
}, {
"id": "2",
"date": "2017-03-12",
"date_text": "sunday 12 march",
"title": "title text"
}]
}
JSON;
$config = new \Zend\Config\Config(json_decode($json, true), true);
$writer = new \Zend\Config\Writer\Ini();
echo $writer->toString($config);
The output will be:
[books]
0.id = "1"
0.date = "2017-03-12"
0.date_text = "sunday 12 march"
0.title = "title text"
1.id = "2"
1.date = "2017-03-12"
1.date_text = "sunday 12 march"
1.title = "title text"
Your JSON format should be look like below to produce desired output you wrote in question:
{
"books 0": {
"id": "1",
"date": "2017-03-12",
"date_text": "sunday 12 march",
"title": "title text"
},
"books 1" : {
"id": "2",
"date": "2017-03-12",
"date_text": "sunday 12 march",
"title": "title text"
}
}

Related

Pivot data from JSON object

I have a base table where i store records per team in a json object;
[
{
"Id": 1,
"date": "01-01-2022",
"Group": "A",
"Description": "test 1"
},
{
"Id": 2,
"date": "01-01-2022",
"Group": "B",
"Description": "test 2"
},
{
"Id": 3,
"date": "08-01-2022",
"Group": "B",
"Description": "test 3"
},
{
"Id": 4,
"date": "16-01-2022",
"Group": "A",
"Description": "test 4"
},
{
"Id": 5,
"date": "16-01-2022",
"Group": "C",
"Description": "test 5"
}
]
Now for reporting purpose i need to pivot the table like:
Date
A
B
C
01-01-2022
test 1
test 2
-
08-01-2022
-
test 3
-
16-01-2022
test 4
-
test 5
I really dont know how to perform such action with PHP or Javascript.. Tried with for each loops but i only got the date as a unique value.
btw; for empty rows to show "-" is fine.

Array filter JSON data with price parameter

I have this JSON data and I am using this code to match the title. It works, but now I want to go one step further and list all products which are less than some price, or more than some price, or equal to some price
$json = '[{
"id": 2042049298481,
"title": "Test Product 53",
"variants": [{
"id": 15733087797297,
"title": "Default Title",
"price": "100.00",
"sku": "no"
}]
}, {
"id": 2196682342449,
"title": "its test yar",
"variants": [{
"id": 16385289879601,
"title": "Default Title",
"price": "144.00",
"sku": "no"
}]
}, {
"id": 4175970041905,
"title": "uhjkjhkhk",
"variants": [{
"id": 30302326554673,
"title": "Default Title",
"price": "1000.00",
"sku": "10"
}]
}, {
"id": 4183828791345,
"title": "kaminsss",
"variants": [{
"id": 30346449518641,
"title": "Default Title",
"price": "111.00",
"sku": "no"
}]
}, {
"id": 4247884890161,
"title": "hellooo",
"variants": [{
"id": 30579860832305,
"title": "Default Title",
"price": "1111.00",
"sku": "no"
}]
}, {
"id": 4248143822897,
"title": "10oct latest collection",
"variants": [{
"id": 31157780938801,
"title": "50",
"price": "111.00",
"sku": "no-1"
}, {
"id": 31157802270769,
"title": "60",
"price": "101.00",
"sku": ""
}, {
"id": 31157804761137,
"title": "70",
"price": "111.00",
"sku": ""
}]
}, {
"id": 4284600746033,
"title": "18oct2019",
"variants": [{
"id": 31595410030641,
"title": "120 / black",
"price": "100.00",
"sku": "10"
}]
}, {
"id": 4285596860465,
"title": "test2222",
"variants": [{
"id": 30877916921905,
"title": "Default Title",
"price": "111.00",
"sku": "no"
}]
}]';
$array = json_decode($json,1);
$filter_name ='title';
$filter_value ='Test';
$expected88 = array_filter($array, function($el) use ($filter_value, $filter_name) {
return ($el[$filter_name] >= $filter_value);
});
echo json_encode($expected88,true);
This works flawlessly for title but we want to filter based on variant price. There may be several prices for one variant and if $filter_value is greater than, equal to, or less than, anyone can occur.
For example user may search for product whose minimum price is 50, so in that case, if any variant is matching criteria then it will list those products, or user may search that all variants' prices should be less than 50.
We tried like this
$expected = array_filter($array, function ($var) use ($filter_value) {
return ($var[$filter_name] >= $filter_value);
});
but it did not work.
We tried foreach and tested again but it always gave empty result
Here is possible solution
$filter_name ='variants';
$filter_value = 200;
$filter_field = "price";
$expected88 = array_filter($array, function($el) use ($filter_value, $filter_name, $filter_field) {
return ((int)$el[$filter_name][0][$filter_field] >= (int)$filter_value);
});

php getting value from object [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
I've got basic array from my api:
{ "title": "offer title", "body": "offer body",
"specialities": { "lang": "en", "id": "1", "icon": "0",
"name": "speciality name 1" }, "region": "region1" }
I want to get id value from request for my php variable lets say: $idVariable. How can I do it?
I tried something like:
$idVariable = $request->specialities[0]->id
but it seems not working. What is the right way?
Then how should I work with the arrays of object in this case:
{ "title": "offer title", "body": "offer body",
"specialities": [
{ "lang": "en", "id": "1", "icon": "0", "name": "speciality name 1" },
{ "lang": "en", "id": "2", "icon": "0", "name": "speciality name 2" },
{ "lang": "en", "id": "2", "icon": "0", "name": "speciality name 3" },
etc...], "region": "region1" }
To get id's of every object in specialities array? I know that it could be a duplicate question, but I ask for just a basic example.
I tried to use json decode like below:
json_decode($request->get('specialties'))->id
edit:
The almost-right way to do it is to decode json file first:
$data = json_decode($request);
and then get the right property from the array:
$id = $data['specialities'][0]['id'];
the problem now is that id is a string not an integer and by simply using:
$int_id = intval($id)
I've got $int_id = 0 instead of 1,2,3 etc
You are getting a response from API in JSON you should use json_decode() and then use the data. Try this code.
$json = '{ "title": "offer title", "body": "offer body",
"specialities": { "lang": "en", "id": "1", "icon": "0",
"name": "speciality name 1" }, "region": "region1" }';
$data = json_decode($json);
echo $data->specialities->id;

Displaying key/value in PHP object

In PHP, how can I make this data:
{
"items": {
"item": [{
"id": "59",
"type": "Domain",
"relid": "27",
"description": "Sample Monthly Product(01\ / 01\ / 2016 - 31\ / 01\ / 2016)",
"amount": "180.00",
"taxed": "0"
}]
}
}
Have this format:
{
"items[item][0][id]": "59",
"items[item][0][type]": "Domain",
"items[item][0][relid]": "27",
"items[item][0][description]": "Sample Monthly Product (01\/01\/2016 - 31\/01\/2016)",
"items[item][0][amount]": "180.00",
"items[item][0][taxed]": "0"
}
Reason: Setting up a Zap (via Zapier) using email parser to import invoices from WHMCS, and it seems to work with parsing data when 2nd format (items[item][0][id]) to read the line descriptions but not when using the 1st. In the WHMCS API ref it shows it outputting as the 2nd format but can't see why mine looks like 1st (developers.whmcs.com/api-reference/getinvoice)
I think this might work with your current setup:
<?php
//assuming your current dataset isn't in JSON, you can ignore this part
$json = '{
"items": {
"item": [{
"id": "59",
"type": "Domain",
"relid": "27",
"description": "Sample Monthly Product",
"amount": "180.00",
"taxed": "0"
},
{
"id": "203",
"type": "Server",
"relid": "86",
"description": "Sample Yearly Product",
"amount": "290.00",
"taxed": "1"
}]
}
}';
$json = json_decode($json, true);
$parsed = array();
foreach ($json['items']['item'] as $index => $item)
foreach ($item as $attr => $val)
$parsed['items[item][' . $index . '][' . $attr . ']'] = $val;
echo json_encode($parsed);
Output:
{
"items[item][0][id]": "59",
"items[item][0][type]": "Domain",
"items[item][0][relid]": "27",
"items[item][0][description]": "Sample Monthly Product",
"items[item][0][amount]": "180.00",
"items[item][0][taxed]": "0",
"items[item][1][id]": "203",
"items[item][1][type]": "Server",
"items[item][1][relid]": "86",
"items[item][1][description]": "Sample Yearly Product",
"items[item][1][amount]": "290.00",
"items[item][1][taxed]": "1"
}

Display value of json to output

Below is json code where i have o display its values. How to fetch the output as given below
$jsondata = '{
"flowers": [
{
"id": "1",
"name": "Le Grand Bouquet Blanc",
"price": "65",
"currency": "euro"
},
{
"id": "2",
"name": "Roses",
"price": "33",
"currency": "euro"
},
{
"id": "3",
"name": "Mandarine",
"price": "125",
"currency": "euro"
}
]
}';
Output should come like this
Name : Le Grand Bouquet Blanc, Price : 65
Name : Roses, Price : 33
Name : Mandarine, Price : 125
Total: 223 Euro
Any Help?
JSON decode, loop through the data and output the required text like so:
$data = json_decode($jsondata);
$total = 0;
foreach($data->flowers as &$datum) {
printf('Name : %s, Price: %d'.PHP_EOL, $datum->name, $datum->price);
$total += $datum->price;
}
printf('Total: %d Euro'.PHP_EOL, $total);
Read up on some basic PHP functions/concepts:
http://php.net/manual/en/function.json-decode.php
http://php.net/manual/en/control-structures.foreach.php
http://php.net/manual/en/function.printf.php
http://php.net/manual/en/function.echo.php
http://php.net/manual/en/language.operators.arithmetic.php
Try using json_decode() with true as second attribute to convert JSON it into array first.Then use foreach loop and get desired result.
<?php
$jsondata = '{
"flowers": [
{
"id": "1",
"name": "Le Grand Bouquet Blanc",
"price": "65",
"currency": "euro"
},
{
"id": "2",
"name": "Roses",
"price": "33",
"currency": "euro"
},
{
"id": "3",
"name": "Mandarine",
"price": "125",
"currency": "euro"
}
]
}';
$array = json_decode($jsondata,true);
//print_r($array);
$sum = 0;
foreach($array['flowers'] as $flowers)
{
echo "Name : ".$flowers['name'].",Price : ".$flowers['price'].PHP_EOL;
$sum+=$flowers['price'];
$currency = $flowers['currency'];
}
echo "Total:".$sum." ".$currency;
Try this.
$jsondata = '{
"flowers": [
{
"id": "1",
"name": "Le Grand Bouquet Blanc",
"price": "65",
"currency": "euro"
},
{
"id": "2",
"name": "Roses",
"price": "33",
"currency": "euro"
},
{
"id": "3",
"name": "Mandarine",
"price": "125",
"currency": "euro"
}
]
}';
$data = json_decode($jsondata,true);
echo "Name : " . $data['flowers'][0]['name'] . ' , Price: ' . $data['flowers'][0]['price'] ;

Categories