This question already has answers here:
How to parse JSON and access results
(3 answers)
Closed 1 year ago.
I got the following array called $resource:
{
"id": 24927,
"availability": [],
"block_cost": 0,
"name": "Nachtklettern 11.08.2020",
"parent_id": 0,
"qty": "20",
"sort_order": 0,
"meta_data": [{
"id": 15548,
"key": "kor_reg_status",
"value": "off"
}, {
"id": 15549,
"key": "qty",
"value": "20"
}, {
"id": 15550,
"key": "_arb_reservation_availability",
"value": []
}, {
"id": 15572,
"key": "kor_reg_status",
"value": "off"
}, {
"id": 15573,
"key": "qty",
"value": "20"
}, {
"id": 15574,
"key": "_arb_reservation_availability",
"value": []
}, {
"id": 32463,
"key": "_fusion",
"value": []
}, {
"id": 96581,
"key": "_edit_lock",
"value": "1615588526:3"
}, {
"id": 97192,
"key": "_edit_last",
"value": "3"
}]
}
and want to get the first value of qty -> 20
If I try
echo $resource[5] or
echo $resource["qty"]
I didn't get the value.
What can I do?
Decode the JSON string first, then retrieval of any property is straightforward:
$resource = '{"id":24927,"availability":[],"block_cost":0,"name":"Nachtklettern 11.08.2020","parent_id":0,"qty":"20","sort_order":0,"meta_data":[{"id":15548,"key":"kor_reg_status","value":"off"},{"id":15549,"key":"qty","value":"20"},{"id":15550,"key":"_arb_reservation_availability","value":[]},{"id":15572,"key":"kor_reg_status","value":"off"},{"id":15573,"key":"qty","value":"20"},{"id":15574,"key":"_arb_reservation_availability","value":[]},{"id":32463,"key":"_fusion","value":[]},{"id":96581,"key":"_edit_lock","value":"1615588526:3"},{"id":97192,"key":"_edit_last","value":"3"}]}';
$res = json_decode($resource);
echo $res->qty; // 20
Related
There is json response:
{
"id": "1234567890123456789",
"creation_date": 12345678,
"event": "WAITING_PAYMENT",
"version": "2.0.0",
"data": {
"product": {
"id": 213344,
"name": "Product Name",
"has_co_production": false
},
"affiliates": [
{
"name": "Affiliate name"
}
],
"buyer": {
"email": "buyer#email.com"
},
"producer": {
"name": "Producer Name"
},
"commissions": [
{
"value": 0.65,
"source": "MARKETPLACE"
},
{
"value": 3.10,
"source": "PRODUCER"
}
],
"purchase": {
"approved_date": 1231241434453,
"full_price": {
"value": 134.0
},
"original_offer_price": {
"currency_value": "EUR"
"value": 100.78,
},
"price": {
"value": 150.6
},
"order_date": "123243546",
"status": "STARTED",
"transaction": "HP02316330308193",
"payment": {
"billet_barcode": "03399.33335 33823.303087 198801027 2 876300015000",
"billet_url": "https://billet-link.com/bHP023163303193",
}
},
"subscription": {
"status": "ACTIVE",
"plan": {
"name": "plan name"
},
"subscriber": {
"code": "12133421"
}
}
}
}
My question is how to extract data["buyer"]["email"] in PHP ?
I only need to extract the email information from the buyer table inside the data table.
First, you need to decode the json to a PHP array (or an object), then you can access the requested information from the decoded data.
$data = json_decode('the json string most place here', true);
$email = $data['buyer']['email'];
Place your json string in the first argument of json_decode() function.
i need to combine two jsons in a job task, according to the person's location. I have a Json with people from location type 1, and another from location type 2.
I need to combine in a new array for each time they have the same location, and they are repeated and separate according to type. I tried to do it with a foreach inside the other, but with no success. Here is an example of JSON.
Json 1:
[
{
"id": 1,
"name": "Jacob",
"place": "Brazil",
"type": 1
},
{
"id": 2,
"name": "Izac",
"place": "Brazil",
"type": 1
},
{
"id": 3,
"name": "Anran",
"place": "Brazil",
"type": 1
},
{
"id": 4,
"name": "Irbur",
"place": "Brazil",
"type": 1
},
{
"id": 5,
"name": "Lusos",
"place": "Brazil",
"type": 1
},
{
"id": 6,
"name": "Gamio",
"place": "Brazil",
"type": 1
},
{
"id": 7,
"name": "Nubeil",
"place": "Brazil",
"type": 1
},
{
"id": 8,
"name": "Usgon",
"place": "Brazil",
"type": 1
},
{
"id": 15,
"name": "Fikis",
"place": "England",
"type": 1
}
]
Json 2:
[
{
"id": 9,
"name": "Gipin",
"place": "Brazil",
"type": 0
},
{
"id": 10,
"name": "Paoir",
"place": "Brazil",
"type": 0
},
{
"id": 11,
"cia": "Mutue",
"place": "Brazil",
"type": 0
},
{
"id": 12,
"name": "Ziefel",
"place": "England",
"type": 0
},
{
"id": 13,
"name": "Liedo",
"place": "England",
"type": 0
},
{
"id": 14,
"name": "Vicis",
"place": "England",
"type": 0
}
]
the result might look something like this:
{
"groups": [ // groups (same place)
{
"tipe1": [ // groups type 1: same place
{
"id": 1,
"name": "Jacob",
"place": "Brazil"
},
{
"id": 1,
"name": "Jacob",
"place": "Brazil"
}
]
"tipe2": [ // groups type 2: same place
{
"id": 11,
"name": "Mutue",
"place": "Brazil"
},
]
},
{
"tipe1": [
{
"id": 15,
"name": "Fikis",
"place": "England"
}
]
"tipe2": [
{
"id": 14,
"name": "Vicis",
"place": "England"
},
]
}
],
}
The rule for grouping is, two or more items of type 1 can be grouped with 1 of type 2, in reverse as well. It can be thought of as outward flights, and back flights.
I've some trouble with parsing a JSON file into a MySQL database. It's an export of some Facebookstats.
Because I've multiple export of multiple pages, it's important that I've the corresponding ID in the database.
The JSONfile (or cURL from Facebook) looks like this:
{
"data": [
{
"name": "impressions",
"period": "week",
"values": [
{
"value": 123456789,
"end_time": "2016-01-01T08:00:00+0000"
},
{
"value": 12345678,
"end_time": "2016-01-02T08:00:00+0000"
},
{
"value": 1234567,
"end_time": "2016-01-03T08:00:00+0000"
},
{
"value": 123456,
"end_time": "2016-01-04T08:00:00+0000"
},
{
"value": 12345,
"end_time": "2016-01-05T08:00:00+0000"
}
],
"title": "Weekly Impressions",
"description": "The number of impressions seen of any content associated with your Page. (Total Count)",
"id": "101010101010\/insights\/page_impressions\/week"
}
],
"paging": {
"previous": "1",
"next": "2"
}
}
I would, ideally, parse this data into a MySQL database that looks like this:
id value end_time
101010101010 123456789 2016-01-01T08:00:00+0000
101010101010 12345678 2016-01-02T08:00:00+0000
101010101010 1234567 2016-01-03T08:00:00+0000
101010101010 123456 2016-01-04T08:00:00+0000
101010101010 12345 2016-01-05T08:00:00+0000
I hope someone had some ideas :-)
Use json_decode(). Example:
$jsonString = '{
"data": [
{
"name": "impressions",
"period": "week",
"values": [
{
"value": 123456789,
"end_time": "2016-01-01T08:00:00+0000"
},
{
"value": 12345678,
"end_time": "2016-01-02T08:00:00+0000"
},
{
"value": 1234567,
"end_time": "2016-01-03T08:00:00+0000"
},
{
"value": 123456,
"end_time": "2016-01-04T08:00:00+0000"
},
{
"value": 12345,
"end_time": "2016-01-05T08:00:00+0000"
}
],
"title": "Weekly Impressions",
"description": "The number of impressions seen of any content associated with your Page. (Total Count)",
"id": "101010101010\/insights\/page_impressions\/week"
}
],
"paging": {
"previous": "1",
"next": "2"
}
}';
Then decode it to an associative array:
$assocData = json_decode($jsonString, true); //Setting second optional parameter to true makes it return an associative array.
Then access it however you want:
$data = $assocData['data'];
This question already has an answer here:
How to extract individual elements from a JSON string?
(1 answer)
Closed 5 years ago.
{
"status": "success",
"data": {
"title": "test2",
"json_query": {
"condition": "AND",
"rules": [{
"id": "event",
"field": "event",
"type": "string",
"operator": "equal",
"value": "signup"
}, {
"condition": "AND",
"rules": [{
"id": "event",
"field": "event",
"type": "string",
"operator": "equal",
"value": "signup"
}, {
"condition": "AND",
"rules": [{
"id": "kwd",
"field": "kwd",
"type": "string",
"operator": "equal",
"value": "epub"
}]
}]
}, {
"id": "kwd",
"field": "kwd",
"type": "string",
"operator": "equal",
"value": "arts"
}, {
"condition": "AND",
"rules": [{
"id": "kwd",
"field": "kwd",
"type": "string",
"operator": "equal",
"value": "automotive"
}, {
"condition": "AND",
"rules": [{
"id": "kwd",
"field": "kwd",
"type": "string",
"operator": "equal",
"value": "books"
}, {
"condition": "AND",
"rules": [{
"id": "kwd",
"field": "kwd",
"type": "string",
"operator": "equal",
"value": "business"
}, {
"condition": "AND",
"rules": [{
"id": "kwd",
"field": "kwd",
"type": "string",
"operator": "equal",
"value": "books"
}]
}]
}]
}]
}]
}
}
}
This is my string and i need the regex which will get all id with its value
eg: "id" : "event", "id" : "event", "id" : "kwd", "id" : "kwd"
means every occurrence, so i can replace or add a unique value to id
NOTE:- I dont want to make an array, dont want to do with loop,
finding possibility to replace with preg_match or any other regex method.
I got my answer myself:
preg_match_all('/"id":"(.*)",/U', $json, $matches, PREG_PATTERN_ORDER);
print_r($matches); die;
I am looking for a way to retrieve the number of likes of a Facebook post, based on its post_id. I got hold of a php code from this forum itself..and it's something like
<?php
function fetchUrl($url){
return file_get_contents($url);
}
$json_object = fetchUrl("https://graph.facebook.com/{post_id}/likes?access_token={token}&limit=5000000"); //
$feedarray = json_decode($json_object, true);
$likesNum = count($feedarray['data']); // return the number of items in `data` array
print $likesNum;
?>
But the problem is this method does not retrieve the total likes of a post, since likes are displayed in blocks of 1000, after which theres another link to a different page containing the next set of 1000 likes and so on.
Is there a way to get the total number of likes of a facebook post by a single query?
Add the summary flag ** summary=true**
"https://graph.facebook.com/{post_id}/likes?access_token={token}&summary=true"
{
"data": [
{
"id": "663342380429664",
"name": "Luis Mendoza"
},
{
"id": "1532100840406448",
"name": "Sakazuki Akainu"
},
{
"id": "780666205412913",
"name": "Joaito KoRn"
},
{
"id": "1060933433919925",
"name": "Adrian Sosa"
},
{
"id": "860704407276452",
"name": "Sarah Rosenstrauch"
},
{
"id": "1947974762009431",
"name": "David Prieto"
},
{
"id": "804864302928112",
"name": "Ronal Ortega"
},
{
"id": "1505075359814934",
"name": "Gonzalo Larzen"
},
{
"id": "1431207613804483",
"name": "Victor Clc"
},
{
"id": "508785009283633",
"name": "Rxdry EzDe Cerrx Mcmxii"
},
{
"id": "435355413265946",
"name": "Ángel Fernando Huillca Alonso"
},
{
"id": "163773913961445",
"name": "Pelado Miguel Pin Macias"
},
{
"id": "1479227465674392",
"name": "Releck Solitario"
},
{
"id": "161610054193539",
"name": "MD Sahin MD Sahin"
},
{
"id": "798431050242097",
"name": "Brian Nahuel"
},
{
"id": "624869574305480",
"name": "Saul Alfredo"
},
{
"id": "1642733362665392",
"name": "Junior Zurita"
},
{
"id": "134907406871404",
"name": "Wil Peña"
},
{
"id": "10153052770952668",
"name": "Miguel Peña Cáceres"
},
{
"id": "1461494580846182",
"name": "Darian Suarez"
},
{
"id": "365762500250317",
"name": "Igarashi Ganta"
},
{
"id": "750032685093387",
"name": "Camila Barbé"
},
{
"id": "781013541941152",
"name": "Gonzalo Nievas"
},
{
"id": "756520927743339",
"name": "Jonathan C. Duran Cuellar"
},
{
"id": "1504488093199860",
"name": "Maxi Russo"
}
],
"paging": {
"cursors": {
"before": "NjYzMzQyMzgwNDI5NjY0",
"after": "MTUwNDQ4ODA5MzE5OTg2MAZDZD"
},
"next": "https://graph.facebook.com/v2.3/1009501939072385/likes?access_token=TOKEN..."
},
"summary": {
"total_count": 4303
}
}