How to count same value from a php array - php

i have a json of specification of a mobile, i want to count repeated value of a particular node. my json in as below:
[{
"other": {
"1": {
"type": "Network",
"label": "Technology",
"value": "GSM / HSPA / LTE"
},
"2": {
"type": "Network",
"label": "2G bands",
"value": "GSM 850 / 900 / 1800 / 1900 - SIM 1 & SIM 2"
},
"3": {
"type": "Network",
"label": "3G bands",
"value": "HSDPA 850 / 900 / 1900 / 2100"
},
"4": {
"type": "Network",
"label": "4G bands",
"value": "LTE band 1(2100), 3(1800), 5(850), 7(2600), 8(900), 20(800), 38(2600), 40(2300), 41(2500)"
},
"5": {
"type": "Network",
"label": "Speed",
"value": "HSPA 42.2/5.76 Mbps, LTE-A (2CA) Cat12 600/50 Mbps"
},
"6": {
"type": "Network",
"label": "GPRS",
"value": "Yes"
},
"7": {
"type": "Network",
"label": "EDGE",
"value": "Yes"
},
"8": {
"type": "Launch",
"label": "Announced",
"value": "2019, January"
},
"9": {
"type": "Launch",
"label": "Status",
"value": "Available. Released 2019, February"
},
"10": {
"type": "Body",
"label": "Dimensions",
"value": "156.4 x 74.5 x 8.8 mm (6.16 x 2.93 x 0.35 in)"
},
"11": {
"type": "Body",
"label": "Weight",
"value": "186 g (6.56 oz)"
},
"12": {
"type": "Body",
"label": "SIM",
"value": "Dual SIM (Nano-SIM, dual stand-by)"
},
"13": {
"type": "Display",
"label": "Type",
"value": "PLS TFT capacitive touchscreen, 16M colors"
},
"14": {
"type": "Display",
"label": "Size",
"value": "6.3 inches, 97.4 cm2 (~83.6% screen-to-body ratio)"
},
"15": {
"type": "Display",
"label": "Resolution",
"value": "1080 x 2340 pixels, 19.5:9 ratio (~409 ppi density)"
},
"16": {
"type": "Platform",
"label": "OS",
"value": "Android 8.1 (Oreo); Experience 9.5"
},
"17": {
"type": "Platform",
"label": "Chipset",
"value": "Exynos 7904 (14 nm)"
},
"18": {
"type": "Platform",
"label": "CPU",
"value": "Octa-core (2x1.8 GHz Cortex-A73 & 6x1.6 GHz Cortex-A53)"
},
"19": {
"type": "Platform",
"label": "GPU",
"value": "Mali-G71 MP2"
},
"20": {
"type": "Memory",
"label": "Card slot",
"value": "microSD, up to 1 TB (dedicated slot)"
},
"21": {
"type": "Memory",
"label": "Internal",
"value": "64 GB, 4 GB RAM or 32 GB, 3 GB RAM"
},
"22": {
"type": "Main Camera",
"label": "Dual",
"value": "13 MP, f/1.9, 1/3.1\", 1.12um, PDAF"
},
"23": {
"type": "Main Camera",
"label": "Dual",
"value": "5 MP, f/2.2, 12mm (ultrawide)"
},
"24": {
"type": "Main Camera",
"label": "Features",
"value": "LED flash, panorama, HDR"
},
"25": {
"type": "Main Camera",
"label": "Video",
"value": "1080p#30fps"
},
"26": {
"type": "Selfie camera",
"label": "Single",
"value": "8 MP, f/2.0, 25mm (wide)"
},
"27": {
"type": "Selfie camera",
"label": "Features",
"value": "HDR"
},
"28": {
"type": "Selfie camera",
"label": "Video",
"value": "1080p#30fps"
},
"29": {
"type": "Sound",
"label": "Loudspeaker",
"value": "Yes"
},
"30": {
"type": "Sound",
"label": "3.5mm jack",
"value": "Yes"
},
"31": {
"type": "Sound",
"value": "Active noise cancellation with dedicated mic"
},
"32": {
"type": "Sound",
"value": "Dolby Atmos sound"
},
"33": {
"type": "Comms",
"label": "WLAN",
"value": "Wi-Fi 802.11 b/g/n, WiFi Direct, hotspot"
},
"34": {
"type": "Comms",
"label": "Bluetooth",
"value": "5.0, A2DP, LE"
},
"35": {
"type": "Comms",
"label": "GPS",
"value": "Yes, with A-GPS, GLONASS, BDS"
},
"36": {
"type": "Comms",
"label": "Radio",
"value": "FM radio, RDS, recording"
},
"37": {
"type": "Comms",
"label": "USB",
"value": "2.0, Type-C 1.0 reversible connector"
},
"38": {
"type": "Features",
"label": "Sensors",
"value": "Fingerprint (rear-mounted), accelerometer, gyro, proximity, compass"
},
"39": {
"type": "Battery",
"value": "Non-removable Li-Po 5000 mAh battery"
},
"40": {
"type": "Battery",
"label": "Charging",
"value": "Fast battery charging 15W"
},
"41": {
"type": "Misc",
"label": "Colors",
"value": "Ocean Blue, Charcoal Black"
},
"42": {
"type": "Misc",
"label": "Models",
"value": "SM-M205F, SM-M205FN, SM-M205G"
},
"43": {
"type": "Misc",
"label": "SAR EU",
"value": "0.25 W/kg (head) 1.59 W/kg (body)"
},
"44": {
"type": "Misc",
"label": "Price",
"value": "About 220 EUR"
},
"45": {
"type": "Tests",
"label": "Performance",
"value": "Basemark OS II: 1846 / Basemark OS II 2.0: 2025Basemark X: 8434"
},
"46": {
"type": "Tests",
"label": "Display",
"value": "Contrast ratio: 1333:1 (nominal), 2.960 (sunlight)"
},
"47": {
"type": "Tests",
"label": "Camera",
"value": "Photo / Video"
},
"48": {
"type": "Tests",
"label": "Loudspeaker",
"value": "Voice 67dB / Noise 66dB / Ring 68dB"
},
"49": {
"type": "Tests",
"label": "Audio quality",
"value": "Noise -90.5dB / Crosstalk -91.7dB"
},
"50": {
"type": "Tests",
"label": "Battery life",
"value": "Endurance rating 103h"
}
}
}]
I want to count how many times Network, Body, Display ..., Tests are there in this JSON.
I have tried below code but unable to found exact information regarding the same
$arr1 = above shared json;
$arr1 = json_decode($arr, true);
foreach ($arr1 as $key=>$value){
$other = $value['other']; //array
$counter = 0;
$lenOther = count($other); //get length of other array
//reading each element of other array
foreach($other as $k=>$v){
$other[$k] = array_count_values($v);
print_r($other[$k]);
}
}
Any modification or suggestion of this question would be appreciated.

Apply array_count_values() along with array_column
$arr1 = json_decode($arr, true);
print_r(array_count_values(array_column($arr1[0]['other'],'type')));
Output:- https://3v4l.org/ZHRIa
Note:- If you want to combine all of them based on type and want to create same structure what you have:-
$arr1 = json_decode($arr, true);
$finalArray = array();
foreach($arr1[0]['other'] as $key=>$value){
$finalArray[$value['type']][] = $value;
}
$newArray = [];
$newArray[] = array('other'=>$finalArray);
print_r($newArray);
Output:- https://3v4l.org/lUrgv

You get the count by using array_count_values method with array_map
just like below
array_count_values(array_map(function($foo) {
return $foo['type'];
}, $arr['other']));
this will get you the count for each type

you can use foreach with array_key_exists
$jarr = json_decode($json,true);
$r = [];
foreach($jarr[0]['other'] as $v){
array_key_exists($v['type'], $r) ? ($r[$v['type']]+=1) : ($r[$v['type']]=1);
}
Working example

Related

PHP to extract specific words

I have a string like the below in a variable called $CPU. The text is below, my target is to pull specific element of the below text.
[{
"#attributes": {
"name": "processors"
},
"entry": ["2", "1", "successful"],
"entries": {
"#attributes": {
"name": "processor"
},
"entry": ["Intel(R) Core(TM) i5-7200U CPU # 2.50GHz", "GenuineIntel", "2500000000", "3100000000", "2", "4", "true", "9", "100 MHz", "7"],
"entries": {
"#attributes": {
"name": "caches"
},
"entries": [{
"#attributes": {
"name": "cache"
},
"entry": ["Level 1 Cache", "131072"]
}, {
"#attributes": {
"name": "cache"
},
"entry": ["Level 2 Cache", "524288"]
}, {
"#attributes": {
"name": "cache"
},
"entry": ["Level 3 Cache", "3145728"]
}]
}
}
}]
The thing I want to extract specifically is this bit is the "Intel(R) Core(TM) i5-7200U CPU # 2.50GHz" however I'm really unsure how to achieve this easily.
It's a JSON! So if it works for JS it will work for PHP:
$arr = json_decode($CPU, true);
echo $arr[0]["entries"]["entry"][0];
var x = [{
"#attributes": {
"name": "processors"
},
"entry": ["2", "1", "successful"],
"entries": {
"#attributes": {
"name": "processor"
},
"entry": ["Intel(R) Core(TM) i5-7200U CPU # 2.50GHz", "GenuineIntel", "2500000000", "3100000000", "2", "4", "true", "9", "100 MHz", "7"],
"entries": {
"#attributes": {
"name": "caches"
},
"entries": [{
"#attributes": {
"name": "cache"
},
"entry": ["Level 1 Cache", "131072"]
}, {
"#attributes": {
"name": "cache"
},
"entry": ["Level 2 Cache", "524288"]
}, {
"#attributes": {
"name": "cache"
},
"entry": ["Level 3 Cache", "3145728"]
}]
}
}
}]
console.log(x[0]["entries"]["entry"][0])

Need to extract data from POST request with JSON response

I have a JSON response that I'm trying to gather data from. Most of the data is not needed but the way it is structured I'm having a tough time figuring out how to just get the data I need. The strings and values are paired kinda weird.
I need to be able to pull the data using PHP, and I need the info from the characteristic fields. Specifically, I need to pull the value for "qualificationTier".
What would I use to pull that string/value from this JSON response?
{
"externalId": "testinc-334",
"site": [
{
"id": "site-281",
"href": "qualification/site-281",
"description": "Service Qualification Successful",
"interactionDate": "2022-01-22T21:09:53.843Z",
"interactionDateComplete": "2022-01-22T21:09:59.873Z",
"interactionStatus": "Success",
"place": {
"id": "00000TBKM3",
"href": "site/00000TBKM3",
"characteristic": [
{
"name": "latitude",
"value": "32.98972"
},
{
"name": "longitude",
"value": "-117.271838"
},
{
"name": "region",
"value": "INREGION"
},
{
"name": "npa",
"value": "858"
},
{
"name": "nxx",
"value": "720"
},
{
"name": "lata",
"value": "732"
},
{
"name": "swCLLI",
"value": "DLMRCA12"
},
{
"name": "buildingCLLI",
"value": "SOBHCAWE"
},
{
"name": "ILECVendorName",
"value": "PACIFIC BELL"
},
{
"name": "affiliateName",
"value": "AT&T California"
},
{
"name": "unitType",
"value": "STE"
},
{
"name": "unitValue",
"value": "200"
},
{
"name": "standardAddress",
"value": "215 S HIGHWAY 101 STE 200,SOLANA BEACH,CA,92075-1847"
},
{
"name": "qualificationTier",
"value": "Tier 2"
},
{
"name": "fiberFastQualIndicator",
"value": "Y"
},
{
"name": "fiberStatus",
"value": "Fiber Lit"
}
]
},
"serviceAvailability": {
"service": [
{
"id": "adi0001",
"href": "/service/adi0001",
"name": "Customer Managed ADI",
"availability": "available",
"serviceabilityDate": "2022-01-22T21:09:59.880Z",
"serviceSpecification": [
{
"id": "miseth0000002uo",
"availability": "available",
"href": "/serviceSpecification/miseth0000002uo",
"name": "Customer Managed ADI 2 Mbps Ethernet (Optical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "2"
},
{
"name": "uploadSpeed",
"value": "2"
},
{
"name": "connectivity",
"value": "Optical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
{
"id": "miseth0000002ue",
"availability": "available",
"href": "/serviceSpecification/miseth0000002ue",
"name": "Customer Managed ADI 2 Mbps Ethernet (Electrical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "2"
},
{
"name": "uploadSpeed",
"value": "2"
},
{
"name": "connectivity",
"value": "Electrical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
{
"id": "miseth0000005uo",
"availability": "available",
"href": "/serviceSpecification/miseth0000005uo",
"name": "Customer Managed ADI 5 Mbps Ethernet (Optical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "5"
},
{
"name": "uploadSpeed",
"value": "5"
},
{
"name": "connectivity",
"value": "Optical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
{
"id": "miseth0000005ue",
"availability": "available",
"href": "/serviceSpecification/miseth0000005ue",
"name": "Customer Managed ADI 5 Mbps Ethernet (Electrical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "5"
},
{
"name": "uploadSpeed",
"value": "5"
},
{
"name": "connectivity",
"value": "Electrical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
{
"id": "miseth0000010uo",
"availability": "available",
"href": "/serviceSpecification/miseth0000010uo",
"name": "Customer Managed ADI 10 Mbps Ethernet (Optical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "10"
},
{
"name": "uploadSpeed",
"value": "10"
},
{
"name": "connectivity",
"value": "Optical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
{
"id": "miseth0000010ue",
"availability": "available",
"href": "/serviceSpecification/miseth0000010ue",
"name": "Customer Managed ADI 10 Mbps Ethernet (Electrical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "10"
},
{
"name": "uploadSpeed",
"value": "10"
},
{
"name": "connectivity",
"value": "Electrical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
{
"id": "miseth0000020uo",
"availability": "available",
"href": "/serviceSpecification/miseth0000020uo",
"name": "Customer Managed ADI 20 Mbps Ethernet (Optical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "20"
},
{
"name": "uploadSpeed",
"value": "20"
},
{
"name": "connectivity",
"value": "Optical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
{
"id": "miseth0000020ue",
"availability": "available",
"href": "/serviceSpecification/miseth0000020ue",
"name": "Customer Managed ADI 20 Mbps Ethernet (Electrical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "20"
},
{
"name": "uploadSpeed",
"value": "20"
},
{
"name": "connectivity",
"value": "Electrical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
{
"id": "miseth0000050uo",
"availability": "available",
"href": "/serviceSpecification/miseth0000050uo",
"name": "Customer Managed ADI 50 Mbps Ethernet (Optical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "50"
},
{
"name": "uploadSpeed",
"value": "50"
},
{
"name": "connectivity",
"value": "Optical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
{
"id": "miseth0000050ue",
"availability": "available",
"href": "/serviceSpecification/miseth0000050ue",
"name": "Customer Managed ADI 50 Mbps Ethernet (Electrical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "50"
},
{
"name": "uploadSpeed",
"value": "50"
},
{
"name": "connectivity",
"value": "Electrical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
{
"id": "miseth0000100uo",
"availability": "available",
"href": "/serviceSpecification/miseth0000100uo",
"name": "Customer Managed ADI 100 Mbps Ethernet (Optical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "100"
},
{
"name": "uploadSpeed",
"value": "100"
},
{
"name": "connectivity",
"value": "Optical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
```
Check if this works.
<?php
$data = json_decode($your_json);
$qualificationTier = "qualificationTier";
// qualificationTier name is at position 13 in characteristic array
$QualificationTierName = $data->site->place->characteristic[13]->name;
$QualificationTierValue = "";
// Confirm indeed if the string name at position 13 in characteristics is indeed qualificationTier before retrieving the value at the same position
if(strcmp($qualificationTier, $QualificationTierName) == 0 ){
// qualificationTier value is at position 13 in characteristic array
$QualificationTierValue = $data->site->place->characteristic[13]->value;
echo "QualificationTier Name : " + $QualificationTierName;
echo "QualificationTier Value : " + $QualificationTierValue;
}
?>
A simple way would be to loop the array and get value when the name matches your field. You should first decode the json response so you will have an object to work with. Then just loop and set the variable when you have a match.
<?php
$response = json_decode($_POST['json-name']);
$qualificationTier = null;
foreach ($response->site->place->characteristic as $c) {
if ($c->name === 'qualificationTier') {
$qualificationTier = $c->value;
}
}

Don´t show empty days in business hours template from JReviews FormBuilder

Good morning, we use Joomla and JReviews and in some listings we show the opening hours. That works fine with the business hours template. Now we want to change this template for some other listings with training times.
We want, that for example the training is just on tuesdays all other days will display none.
The Code is like this:
{
"title": "Periods",
"type": "array",
"format": "table",
"items": {
"title": "Kurszeit",
"type": "object",
"properties": {
"day": {
"title": "Tag",
"type": "string",
"$ref": "#\/definitions\/weekeday",
"options": {
"input_width": "15em"
}
},
"hours-start": {
"title": "Von",
"type": "string",
"$ref": "#\/definitions\/hours",
"options": {
"input_width": "15em"
}
},
"hours-end": {
"title": "Bis",
"type": "string",
"$ref": "#\/definitions\/hours",
"options": {
"input_width": "15em"
}
}
}
},
"definitions": {
"weekeday": {
"type": "string",
"enumSource": [
{
"source": [
{
"value": "1",
"title": "Montag"
},
{
"value": "2",
"title": "Dienstag"
},
{
"value": "3",
"title": "Mittwoch"
},
{
"value": "4",
"title": "Donnerstag"
},
{
"value": "5",
"title": "Freitag"
},
{
"value": "6",
"title": "Samstag"
},
{
"value": "7",
"title": "Sonntag"
}
],
"title": "{{item.title}}",
"value": "{{item.value}}"
}
]
},
"hours": {
"type": "string",
"enumSource": [
{
"source": [
{
"value": "5.0",
"title": "05:00 Uhr"
},
{
"value": "5.25",
"title": "05:15 Uhr"
},
{
"value": "5.5",
"title": "05:30 Uhr"
},
{
"value": "5.75",
"title": "05:45 Uhr"
},
{
"value": "6.0",
"title": "06:00 Uhr"
},
{
"value": "6.25",
"title": "06:15 Uhr"
},
{
"value": "6.5",
"title": "06:30 Uhr"
},
{
"value": "6.75",
"title": "06:45 Uhr"
},
{
"value": "7.0",
"title": "07:00 Uhr"
},
{
"value": "7.25",
"title": "07:15 Uhr"
},
{
"value": "7.5",
"title": "07:30 Uhr"
},
{
"value": "7.75",
"title": "07:45 Uhr"
},
{
"value": "8.0",
"title": "08:00 Uhr"
},
{
"value": "8.25",
"title": "08:15 Uhr"
},
{
"value": "8.5",
"title": "08:30 Uhr"
},
{
"value": "8.75",
"title": "08:45 Uhr"
},
{
"value": "9.0",
"title": "09:00 Uhr"
},
{
"value": "9.25",
"title": "09:15 Uhr"
},
{
"value": "9.5",
"title": "09:30 Uhr"
},
{
"value": "9.75",
"title": "09:45 Uhr"
},
{
"value": "10.0",
"title": "10:00 Uhr"
},
{
"value": "10.25",
"title": "10:15 Uhr"
},
{
"value": "10.5",
"title": "10:30 Uhr"
},
{
"value": "10.75",
"title": "10:45 Uhr"
},
{
"value": "11.0",
"title": "11:00 Uhr"
},
{
"value": "11.25",
"title": "11:15 Uhr"
},
{
"value": "11.5",
"title": "11:30 Uhr"
},
{
"value": "11.75",
"title": "11:45 Uhr"
},
{
"value": "12.0",
"title": "12:00 Uhr"
},
{
"value": "12.25",
"title": "12:15 Uhr"
},
{
"value": "12.5",
"title": "12:30 Uhr"
},
{
"value": "12.75",
"title": "12:45 Uhr"
},
{
"value": "13.0",
"title": "13:00 Uhr"
},
{
"value": "13.25",
"title": "13:15 Uhr"
},
{
"value": "13.5",
"title": "13:30 Uhr"
},
{
"value": "13.75",
"title": "13:45 Uhr"
},
{
"value": "14.0",
"title": "14:00 Uhr"
},
{
"value": "14.25",
"title": "14:15 Uhr"
},
{
"value": "14.5",
"title": "14:30 Uhr"
},
{
"value": "14.75",
"title": "14:45 Uhr"
},
{
"value": "15.0",
"title": "15:00 Uhr"
},
{
"value": "15.25",
"title": "15:15 Uhr"
},
{
"value": "15.5",
"title": "15:30 Uhr"
},
{
"value": "15.75",
"title": "15:45 Uhr"
},
{
"value": "16.0",
"title": "16:00 Uhr"
},
{
"value": "16.25",
"title": "16:15 Uhr"
},
{
"value": "16.5",
"title": "16:30 Uhr"
},
{
"value": "16.75",
"title": "16:45 Uhr"
},
{
"value": "17.0",
"title": "17:00 Uhr"
},
{
"value": "17.25",
"title": "17:15 Uhr"
},
{
"value": "17.5",
"title": "17:30 Uhr"
},
{
"value": "17.75",
"title": "17:45 Uhr"
},
{
"value": "18.0",
"title": "18:00 Uhr"
},
{
"value": "18.25",
"title": "18:15 Uhr"
},
{
"value": "18.5",
"title": "18:30 Uhr"
},
{
"value": "18.75",
"title": "18:45 Uhr"
},
{
"value": "19.0",
"title": "19:00 Uhr"
},
{
"value": "19.25",
"title": "19:15 Uhr"
},
{
"value": "19.5",
"title": "19:30 Uhr"
},
{
"value": "19.75",
"title": "19:45 Uhr"
},
{
"value": "20.0",
"title": "20:00 Uhr"
},
{
"value": "20.25",
"title": "20:15 Uhr"
},
{
"value": "20.5",
"title": "20:30 Uhr"
},
{
"value": "20.75",
"title": "20:45 Uhr"
},
{
"value": "21.0",
"title": "21:00 Uhr"
},
{
"value": "21.25",
"title": "21:15 Uhr"
},
{
"value": "21.5",
"title": "21:30 Uhr"
},
{
"value": "21.75",
"title": "21:45 Uhr"
},
{
"value": "22.0",
"title": "22:00 Uhr"
},
{
"value": "22.25",
"title": "22:15 Uhr"
},
{
"value": "22.5",
"title": "22:30 Uhr"
},
{
"value": "22.75",
"title": "22:45 Uhr"
}
],
"title": "{{item.title}}",
"value": "{{item.value}}"
}
]
}
}
The Code for output is this:
<?php
/**
* #package JReviews
* #author ClickFWD LLC (https://www.jreviews.com)
* #copyright Copyright (C) 2010-2018 ClickFWD LLC
* #license GNU GPL version 3 or later
*/
defined('MVC_FRAMEWORK') or die;
$twentyfourHourFormat = true;
$daysArray = [
1 => __l('Montag',true),
2 => __l('Dienstag',true),
3 => __l('Mittwoch',true),
4 => __l('Donnerstag',true),
5 => __l('Freitag',true),
6 => __l('Samstag',true),
7 => __l('Sonntag',true)
];
$data = json_decode($text, true);
if ( (isset($data['periods']) && empty($data['periods'])) || empty($data) ) return;
$periods = isset($data['periods']) ? $data['periods'] : $data;
$timezone = isset($data['timezone']) ? $data['timezone'] : null;
S2App::import('Helper', 'opening_hours', 'jreviews');
$OpeningHours = (new OpeningHours($timezone))->addPeriods($periods);
$days = $OpeningHours
->twentyfour($twentyfourHourFormat)
->getOrderedPeriods();
$openNowLabel = __t("Open now", true);
$closedNowLabel = __t("Closed now", true);
$closedLabel = __t("Closed", true);
?>
<?php echo $this->renderControllerView('fields_phpformat', 'business_hours_schedule', compact('OpeningHours','days','daysArray'));?>
At the moment the Output is like this:
Monday:
Tuesday: 10:00 - 11:00
Wednesday:
Thursday:
Friday:
Saturday:
Sunday:
But we want this:
Tuesday: 10:00 - 11:00
Hopefully we could explain what we want.
Hopefully we could explain what we want.

json decode won't work on string with array

I'm trying to import something from an .xml file that I have no control over. Everything went fine until the content:encoded part.
I'm getting the xml like this to strip it of the CDATA Tags:
$xml_object = simplexml_load_file($file,'SimpleXMLElement', LIBXML_NOCDATA);
The looks like this, I'm trying to get rid of all the tags and just get the "contents.body" parts, but when I try to use json_decode($itemJson); it returns null. What should I do?
[{
"id": 34543,
"type": "0",
"order": 0,
"width": 100,
"height": 67.82337662337663,
"data": {
"areaMargin": 120,
"contentWidthOriginalPx": 760
},
"areas": [{
"id": 345654,
"order": 0,
"contents": [{
"type": "media",
"data": [{
"type": "width",
"value": "1540"
}, {
"type": "height",
"value": "1026"
}, {
"type": "videoSrc",
"value": ""
}, {
"type": "src",
"value": "https://link.com/1567819/98/eipej334.jpg"
}, {
"type": "alt",
"value": ""
}, {
"type": "mirror",
"value": "false"
}, {
"type": "zoom",
"value": "100"
}, {
"type": "rotate",
"value": "0"
}, {
"type": "positiony",
"value": "50"
}, {
"type": "positionx",
"value": "50"
}, {
"type": "url",
"value": ""
}, {
"type": "borderradius",
"value": "0"
}, {
"type": "sticky",
"value": "false"
}, {
"type": "metapic",
"value": "{\"id\":0,\"tags\":{}}"
}]
}],
"width": 100,
"height": 100,
"widthHeightRatioContent": 1.50097,
"widthHeightRatio": 1.47442
}]
}, {
"id": 2131656668,
"type": "0",
"order": 1,
"width": 100,
"height": 67.82337662337663,
"data": {
"areaMargin": 120,
"contentWidthOriginalPx": 760
},
"areas": [{
"id": 1650030480,
"order": 0,
"contents": [{
"type": "media",
"data": [{
"type": "width",
"value": "1540"
}, {
"type": "height",
"value": "1026"
}, {
"type": "videoSrc",
"value": ""
}, {
"type": "src",
"value": "https://links.com/jf/819/156722819/34/343.jpg"
}, {
"type": "alt",
"value": ""
}, {
"type": "mirror",
"value": "false"
}, {
"type": "zoom",
"value": "100"
}, {
"type": "rotate",
"value": "0"
}, {
"type": "positiony",
"value": "50"
}, {
"type": "positionx",
"value": "50"
}, {
"type": "url",
"value": ""
}, {
"type": "borderradius",
"value": "0"
}, {
"type": "sticky",
"value": "false"
}, {
"type": "metapic",
"value": "{\"id\":0,\"tags\":{}}"
}]
}],
"width": 100,
"height": 100,
"widthHeightRatioContent": 1.50097,
"widthHeightRatio": 1.47442
}]
}, {
"id": 1740427509,
"type": "0",
"order": 2,
"width": 100,
"height": 55,
"data": {
"areaMargin": 120,
"contentWidthOriginalPx": 760
},
"areas": [{
"id": 802708308,
"order": 0,
"contents": [{
"type": "body",
"data": [{
"type": "src",
"value": "<p>Lorem ipsum dolor sit amet.</p>"
}, {
"type": "html",
"value": "false"
}]
}],
"width": 100,
"height": 100,
"widthHeightRatioContent": 1.85874,
"widthHeightRatio": 1.81818
}]
}, {
"id": 79506641,
"type": "0",
"order": 3,
"width": 100,
"height": 55,
"data": {
"areaMargin": 120,
"contentWidthOriginalPx": 760
},
"areas": [{
"id": 28476702,
"order": 0,
"width": 100,
"height": 100,
"widthHeightRatioContent": 1.85874,
"widthHeightRatio": 1.81818,
"contents": [{
"type": "body",
"data": [{
"type": "src",
"value": "<p><span style="\
"font-weight:"
bold;\
"="
"><br></span></p><span style="\
"font-weight:"
bold;\
"="
">lorem upisn? </span>"
}, {
"type": "html",
"value": "false"
}]
}]
}]
}]

Retrieving likes of Facebook Posts in php

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

Categories