Extract the currency rate from bank api [duplicate] - php

Here is the JSON which is sent asynchronously to my php page. It is essentially a product list, which will be inserted into my mySQL database.
My issue is decoding the JSON in PHP. I can do this fine in js with the 'eval' function, but in PHP my efforts have resulted in a complicated series of explode and implode functions.
{
"Product": [
{
"Product_Title": "Cloth",
"Product_Description": "Here is cloth",
"Price": "100",
"Category_ID": "1"
},
{
"Product_Title": "Cloth",
"Product_Description": "Here is cloth",
"Price": "100",
"Category_ID": "1"
},
{
"Product_Title": "Cloth",
"Product_Description": "Here is cloth",
"Price": "100",
"Category_ID": "1"
}
]
}
I know php has a built in json_decode function, but in the PHP documentation they only show how to handle an array.
Any advice or help is really appreciated
Taylor

If you call json_decode($data,true);, your data will be:
Array(
"Product"=>Array(
Array(
"Product_Title"=>"Cloth",
"Product_Description"=>"Here is cloth",
"Price"=>"100",
"Category_ID"=>"1"
),
Array(
.............
)
)
);
What is wrong with that?

If you want to preserve the stdClass objects, you need to use the object-property syntax.
<?php
$json = '{
"Product": [
{
"Product_Title": "Cloth",
"Product_Description": "Here is cloth",
"Price": "100",
"Category_ID": "1"
},
{
"Product_Title": "Cloth",
"Product_Description": "Here is cloth",
"Price": "100",
"Category_ID": "1"
},
{
"Product_Title": "Cloth",
"Product_Description": "Here is cloth",
"Price": "100",
"Category_ID": "1"
}
]
}
';
$json_decoded = json_decode($json);
// Note, it's usually a bad idea to use use count() like this;
// cache the count before the for() in a variable and use that.
// This is for demo purposes only. :)
for ($i = 0; $i < count($json_decoded->{'Product'}); $i++) {
echo "Products:
" . $json_decoded->{'Product'}[$i]->{'Product_Title'} . "
" . $json_decoded->{'Product'}[$i]->{'Product_Description'} . "
" . $json_decoded->{'Product'}[$i]->{'Price'} . "
" . $json_decoded->{'Product'}[$i]->{'Category_ID'} . "
";
}
?>
Outputs:
Products:
Cloth
Here is cloth
100
1
Products:
Cloth
Here is cloth
100
1
Products:
Cloth
Here is cloth
100
1
http://codepad.org/JxYAO5De

Related

Recursive same lines from json link [duplicate]

Here is the JSON which is sent asynchronously to my php page. It is essentially a product list, which will be inserted into my mySQL database.
My issue is decoding the JSON in PHP. I can do this fine in js with the 'eval' function, but in PHP my efforts have resulted in a complicated series of explode and implode functions.
{
"Product": [
{
"Product_Title": "Cloth",
"Product_Description": "Here is cloth",
"Price": "100",
"Category_ID": "1"
},
{
"Product_Title": "Cloth",
"Product_Description": "Here is cloth",
"Price": "100",
"Category_ID": "1"
},
{
"Product_Title": "Cloth",
"Product_Description": "Here is cloth",
"Price": "100",
"Category_ID": "1"
}
]
}
I know php has a built in json_decode function, but in the PHP documentation they only show how to handle an array.
Any advice or help is really appreciated
Taylor
If you call json_decode($data,true);, your data will be:
Array(
"Product"=>Array(
Array(
"Product_Title"=>"Cloth",
"Product_Description"=>"Here is cloth",
"Price"=>"100",
"Category_ID"=>"1"
),
Array(
.............
)
)
);
What is wrong with that?
If you want to preserve the stdClass objects, you need to use the object-property syntax.
<?php
$json = '{
"Product": [
{
"Product_Title": "Cloth",
"Product_Description": "Here is cloth",
"Price": "100",
"Category_ID": "1"
},
{
"Product_Title": "Cloth",
"Product_Description": "Here is cloth",
"Price": "100",
"Category_ID": "1"
},
{
"Product_Title": "Cloth",
"Product_Description": "Here is cloth",
"Price": "100",
"Category_ID": "1"
}
]
}
';
$json_decoded = json_decode($json);
// Note, it's usually a bad idea to use use count() like this;
// cache the count before the for() in a variable and use that.
// This is for demo purposes only. :)
for ($i = 0; $i < count($json_decoded->{'Product'}); $i++) {
echo "Products:
" . $json_decoded->{'Product'}[$i]->{'Product_Title'} . "
" . $json_decoded->{'Product'}[$i]->{'Product_Description'} . "
" . $json_decoded->{'Product'}[$i]->{'Price'} . "
" . $json_decoded->{'Product'}[$i]->{'Category_ID'} . "
";
}
?>
Outputs:
Products:
Cloth
Here is cloth
100
1
Products:
Cloth
Here is cloth
100
1
Products:
Cloth
Here is cloth
100
1
http://codepad.org/JxYAO5De

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);
});

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"
}

Parsing nested brackets in php [duplicate]

Here is the JSON which is sent asynchronously to my php page. It is essentially a product list, which will be inserted into my mySQL database.
My issue is decoding the JSON in PHP. I can do this fine in js with the 'eval' function, but in PHP my efforts have resulted in a complicated series of explode and implode functions.
{
"Product": [
{
"Product_Title": "Cloth",
"Product_Description": "Here is cloth",
"Price": "100",
"Category_ID": "1"
},
{
"Product_Title": "Cloth",
"Product_Description": "Here is cloth",
"Price": "100",
"Category_ID": "1"
},
{
"Product_Title": "Cloth",
"Product_Description": "Here is cloth",
"Price": "100",
"Category_ID": "1"
}
]
}
I know php has a built in json_decode function, but in the PHP documentation they only show how to handle an array.
Any advice or help is really appreciated
Taylor
If you call json_decode($data,true);, your data will be:
Array(
"Product"=>Array(
Array(
"Product_Title"=>"Cloth",
"Product_Description"=>"Here is cloth",
"Price"=>"100",
"Category_ID"=>"1"
),
Array(
.............
)
)
);
What is wrong with that?
If you want to preserve the stdClass objects, you need to use the object-property syntax.
<?php
$json = '{
"Product": [
{
"Product_Title": "Cloth",
"Product_Description": "Here is cloth",
"Price": "100",
"Category_ID": "1"
},
{
"Product_Title": "Cloth",
"Product_Description": "Here is cloth",
"Price": "100",
"Category_ID": "1"
},
{
"Product_Title": "Cloth",
"Product_Description": "Here is cloth",
"Price": "100",
"Category_ID": "1"
}
]
}
';
$json_decoded = json_decode($json);
// Note, it's usually a bad idea to use use count() like this;
// cache the count before the for() in a variable and use that.
// This is for demo purposes only. :)
for ($i = 0; $i < count($json_decoded->{'Product'}); $i++) {
echo "Products:
" . $json_decoded->{'Product'}[$i]->{'Product_Title'} . "
" . $json_decoded->{'Product'}[$i]->{'Product_Description'} . "
" . $json_decoded->{'Product'}[$i]->{'Price'} . "
" . $json_decoded->{'Product'}[$i]->{'Category_ID'} . "
";
}
?>
Outputs:
Products:
Cloth
Here is cloth
100
1
Products:
Cloth
Here is cloth
100
1
Products:
Cloth
Here is cloth
100
1
http://codepad.org/JxYAO5De

How to process JSON in PHP?

Here is the JSON which is sent asynchronously to my php page. It is essentially a product list, which will be inserted into my mySQL database.
My issue is decoding the JSON in PHP. I can do this fine in js with the 'eval' function, but in PHP my efforts have resulted in a complicated series of explode and implode functions.
{
"Product": [
{
"Product_Title": "Cloth",
"Product_Description": "Here is cloth",
"Price": "100",
"Category_ID": "1"
},
{
"Product_Title": "Cloth",
"Product_Description": "Here is cloth",
"Price": "100",
"Category_ID": "1"
},
{
"Product_Title": "Cloth",
"Product_Description": "Here is cloth",
"Price": "100",
"Category_ID": "1"
}
]
}
I know php has a built in json_decode function, but in the PHP documentation they only show how to handle an array.
Any advice or help is really appreciated
Taylor
If you call json_decode($data,true);, your data will be:
Array(
"Product"=>Array(
Array(
"Product_Title"=>"Cloth",
"Product_Description"=>"Here is cloth",
"Price"=>"100",
"Category_ID"=>"1"
),
Array(
.............
)
)
);
What is wrong with that?
If you want to preserve the stdClass objects, you need to use the object-property syntax.
<?php
$json = '{
"Product": [
{
"Product_Title": "Cloth",
"Product_Description": "Here is cloth",
"Price": "100",
"Category_ID": "1"
},
{
"Product_Title": "Cloth",
"Product_Description": "Here is cloth",
"Price": "100",
"Category_ID": "1"
},
{
"Product_Title": "Cloth",
"Product_Description": "Here is cloth",
"Price": "100",
"Category_ID": "1"
}
]
}
';
$json_decoded = json_decode($json);
// Note, it's usually a bad idea to use use count() like this;
// cache the count before the for() in a variable and use that.
// This is for demo purposes only. :)
for ($i = 0; $i < count($json_decoded->{'Product'}); $i++) {
echo "Products:
" . $json_decoded->{'Product'}[$i]->{'Product_Title'} . "
" . $json_decoded->{'Product'}[$i]->{'Product_Description'} . "
" . $json_decoded->{'Product'}[$i]->{'Price'} . "
" . $json_decoded->{'Product'}[$i]->{'Category_ID'} . "
";
}
?>
Outputs:
Products:
Cloth
Here is cloth
100
1
Products:
Cloth
Here is cloth
100
1
Products:
Cloth
Here is cloth
100
1
http://codepad.org/JxYAO5De

Categories