json api call error, what am I doing wrong - php

I made an api call successfully but am getting errors after indexing it
$json = file_get_contents('https://app.morenaija.ng/#api/search/YouTube/'.$search_term.'');
$yt_data = json_decode($json);
?>
below is the output response
{
"items": [
{
"id": "CevxZvSJLk8",
"title": "Katy Perry - Roar (Official)",
"thumbDefault": "https://i.ytimg.com/vi/CevxZvSJLk8/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLDY5ZX7PoN7KNzXXEdQ_Yvch8qqQQ",
"channelTitle": "Katy Perry",
"channelId": "UCYvmuw-JtVrTZQ-7Y4kd63Q",
"publishedAt": "6 years ago",
"duration": "4:30",
"viewCount": "3068657449"
},
{
"id": "UW5SMrURVbM",
"title": "Roar - Katy Perry (Lyrics) ??",
"thumbDefault": "https://i.ytimg.com/vi/UW5SMrURVbM/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLBFmmujndykkc75SeCIl05VPHaglw",
"channelTitle": "DopeLyrics",
"channelId": "UCvR2R7j218tzejtTsb_X6Rw",
"publishedAt": "1 year ago",
"duration": "3:51",
"viewCount": "14073787"
},
{
"id": "FqkfBzRb43o",
"title": "Katy Perry - Roar : Part 2 (Official Cover by 10 year-old Mariangeli from HitStreak)",
"thumbDefault": "https://i.ytimg.com/vi/FqkfBzRb43o/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLBnXgcoefqzxawvmhB-QbCvMUBENg",
"channelTitle": "ShowMobile",
"channelId": "UCYBFx-THKRWqAxq66qZk_ww",
"publishedAt": "6 years ago",
"duration": "3:47",
"viewCount": "307490768"
},
{
"id": "Rh47oTsRf-w",
"title": "Katy Perry - Roar (From “The Prismatic World Tour Live”)",
"thumbDefault": "https://i.ytimg.com/vi/Rh47oTsRf-w/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLAsKxSyujUT_ZCQqsAdpVQAGnxP3Q",
"channelTitle": "Katy Perry",
"channelId": "UCYvmuw-JtVrTZQ-7Y4kd63Q",
"publishedAt": "4 years ago",
"duration": "4:33",
"viewCount": "21381621"
},
{
"id": "KRlhDkwJkHU",
"title": "Katy Perry - Roar (OFFICIAL 1 Hour)",
"thumbDefault": "https://i.ytimg.com/vi/KRlhDkwJkHU/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLCOvC3IMGpgbitf9nn58BxMFpqJnw",
"channelTitle": "SONGS 1 HOUR",
"channelId": "UCEo7MW5ArVEzQCQRpTuNGDg",
"publishedAt": "4 years ago",
"duration": "1:01:11",
"viewCount": "27253"
},
{
"id": "empcJOD-bA0",
"title": "Roar by Katy Perry Lyrics",
"thumbDefault": "https://i.ytimg.com/vi/empcJOD-bA0/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLBcizW9oc4KLGEo5o3Z2kOmKbKOkQ",
"channelTitle": "mulero ibrahim",
"channelId": "UCEl6sNUx9xHQ8zyypGJnlWQ",
"publishedAt": "4 years ago",
"duration": "3:45",
"viewCount": "423025"
},
{
"id": "K-w2sYAqMKw",
"title": "Katy Perry - Roar (One Love Manchester)",
"thumbDefault": "https://i.ytimg.com/vi/K-w2sYAqMKw/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLDjF35LoaRotGJ8WvhB4Nc9oZPvqQ",
"channelTitle": "BBC Music",
"channelId": "UCZtDUmC3W7j25XHZWFT_XgQ",
"publishedAt": "2 years ago",
"duration": "3:50",
"viewCount": "12996443"
},
{
"id": "sLZvdAdlQq4",
"title": "Play Doh ROAR - Katy Perry Barbie Doll Inspired Costume",
"thumbDefault": "https://i.ytimg.com/vi/sLZvdAdlQq4/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLA-i8iXAj-galqb2-0P1w82JdDXcQ",
"channelTitle": "Fondanista",
"channelId": "UCVNrpO7KiVMdwOdFZwLkWRA",
"publishedAt": "4 years ago",
"duration": "2:58",
"viewCount": "14677198"
},
{
"id": "0KSOMA3QBU0",
"title": "Katy Perry - Dark Horse (Official) ft. Juicy J",
"thumbDefault": "https://i.ytimg.com/vi/0KSOMA3QBU0/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLDa1ue7JhfY14YUSCsVhgf7o2kWyw",
"channelTitle": "Katy Perry",
"channelId": "UCYvmuw-JtVrTZQ-7Y4kd63Q",
"publishedAt": "6 years ago",
"duration": "3:45",
"viewCount": "2773465353"
},
{
"id": "Z0Q8j3HJagE",
"title": "Katy Perry - Making of the \"Roar\" Music Video",
"thumbDefault": "https://i.ytimg.com/vi/Z0Q8j3HJagE/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLAj6fVgZFc8eblIa5hC6CwvxasbNQ",
"channelTitle": "Katy Perry",
"channelId": "UCYvmuw-JtVrTZQ-7Y4kd63Q",
"publishedAt": "6 years ago",
"duration": "22:31",
"viewCount": "20739183"
},
{
"id": "EyLCXC3rV2M",
"title": "**LYRICS** Katy Perry - Roar",
"thumbDefault": "https://i.ytimg.com/vi/EyLCXC3rV2M/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLBGnc2_UQBdmVbEvfzHuZf6GVldlQ",
"channelTitle": "ilpescepalla",
"channelId": "UCTnd7uvo-uWwlB9qvh0aucA",
"publishedAt": "6 years ago",
"duration": "4:04",
"viewCount": "2940062"
},
{
"id": "pQQ24JwtRUI",
"title": "Olivia Sings Roar | The Voice Kids Australia 2014",
"thumbDefault": "https://i.ytimg.com/vi/pQQ24JwtRUI/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLCKE6eUp7Iye7_Wx8_0YOOPwpWamw",
"channelTitle": "The Voice Kids Australia",
"channelId": "UCUDR5r3ow3goI1iZgGlnoyw",
"publishedAt": "5 years ago",
"duration": "4:28",
"viewCount": "95223791"
},
{
"id": "aqhCJW890So",
"title": "Mariangeli from HitStreak - ROAR : Part 1",
"thumbDefault": "https://i.ytimg.com/vi/aqhCJW890So/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLCj4EwhFnnFVcjJtF_3hUBYQ83WZw",
"channelTitle": "ShowMobile",
"channelId": "UCYBFx-THKRWqAxq66qZk_ww",
"publishedAt": "6 years ago",
"duration": "0:33",
"viewCount": "32701801"
},
{
"id": "KlyXNRrsk4A",
"title": "Katy Perry - Last Friday Night (T.G.I.F.)",
"thumbDefault": "https://i.ytimg.com/vi/KlyXNRrsk4A/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLAR2QvImCYQhf_dP5WFJiWVKU00fw",
"channelTitle": "Katy Perry",
"channelId": "UCYvmuw-JtVrTZQ-7Y4kd63Q",
"publishedAt": "8 years ago",
"duration": "8:11",
"viewCount": "1239885938"
},
{
"id": "UUT2GTujJI0",
"title": "Katy Perry - Roar Lyrics",
"thumbDefault": "https://i.ytimg.com/vi/UUT2GTujJI0/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLDUKvQC8DQIg7dD39mEjDetWdHMsA",
"channelTitle": "K- Hype",
"channelId": "UCcoS9-5e2PPrO5gIqC9Fy4Q",
"publishedAt": "6 years ago",
"duration": "3:45",
"viewCount": "90138"
},
{
"id": "QGJuMBdaqIw",
"title": "Katy Perry - Firework (Official)",
"thumbDefault": "https://i.ytimg.com/vi/QGJuMBdaqIw/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLCVFJ6nMr1OyDp5P9wNafG76miRoA",
"channelTitle": "Katy Perry",
"channelId": "UCYvmuw-JtVrTZQ-7Y4kd63Q",
"publishedAt": "9 years ago",
"duration": "3:54",
"viewCount": "1233775759"
},
{
"id": "ZKmuyy6xEBU",
"title": "Katy Perry - Firework, Roar & Never Really Over LIVE - SHEIN Together",
"thumbDefault": "https://i.ytimg.com/vi/ZKmuyy6xEBU/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLAqsqPbUkw6GHnIB_my6JIuS9ondA",
"channelTitle": "KPEZ",
"channelId": "UCv1DBOB5sc6vrjYEkwNsHcw",
"publishedAt": "1 day ago",
"duration": "11:52",
"viewCount": "2688"
},
{
"id": "qvBzVWdwZFg",
"title": "Katy Perry - Roar (Live # BBC Radio 1's Big Weekend)",
"thumbDefault": "https://i.ytimg.com/vi/qvBzVWdwZFg/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLDujpTCwyTAWK3t-MozoxcjvBhtjw",
"channelTitle": "HeyHey Hey",
"channelId": "UCMYxD0p4_-GxQ1qYYDsK-FQ",
"publishedAt": "3 years ago",
"duration": "4:38",
"viewCount": "98831"
},
{
"id": "Sl8Wl4EFBGQ",
"title": "KATY PERRY WITH ZERO BUDGET! (Roar PARODY)",
"thumbDefault": "https://i.ytimg.com/vi/Sl8Wl4EFBGQ/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLCybyCTPmI82feN8Rzcmu_0ERrVpA",
"channelTitle": "LankyBox",
"channelId": "UCSf0s2ogUVYpJPuzW1zpAOg",
"publishedAt": "6 months ago",
"duration": "1:24",
"viewCount": "2759481"
}
]
}
below is how I get the things I want for this call
<?php echo $yt_data->items[0]->title; ?>
<?php echo $yt_data->items[0]->channelTitle; ?>
e.t.c
error_log
[20-Jan-2023 09:34:50 UTC] PHP Notice: Undefined index: title in /home/patch/aheadv.com/search_main.php on line 11
PHP Notice: Undefined variable: yt_data in /home/patch/aheadv.com/search_main.php on line 81
PHP Notice: Trying to get property 'items' of non-object in /home/patch/aheadv.com/search_main.php on line 81
PHP Notice: Trying to get property 'title' of non-object in /home/patch/aheadv.com/search_main.php on line 81
PHP Notice: Undefined variable: yt_data in /home/patch/aheadv.com/search_main.php on line 82
e.tc
Ho do I solve the error issues
<?php
require_once('admin/config.php');
require_once('includes/url_slug.php');
if(isset($_GET['change'])){ header("Location: ".$site_url."/".cano($_GET['q'])); }
$search_term = urlencode($_GET['q']);
$save_search = cano($_GET['q']);
$search_title = str_replace("Audio", " ", $save_search);
$search_title = ucwords($search_title);
$yt_id = $_GET["q"];
$url = 'https://app.morenaija.ng/#api/search/YouTube/'.$search_term;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$json = curl_exec($ch);
curl_close($ch);
if ($json === false) {
echo "Error: ".curl_error($ch);
} else {
$yt_data = json_decode($json);
if (json_last_error() !== JSON_ERROR_NONE) {
echo "Error: ".json_last_error_msg();
} else {
if(isset($yt_data->items[0]->title)){
$first_item_title = $yt_data->items[0]->title;
echo $first_item_title;
}
if(isset($yt_data->items[0]->channelId)){
$first_item_channelId = $yt_data->items[0]->channelId;
echo $first_item_channelId;
}
}
}
?>
<title><?php echo $yt_data->items[0]->title; ?> Mp4 mp3 Download - <?php echo $site_title;?></title>

Related

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.

How to count same value from a php array

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

how to access elements in a nested array in php

When I run following code:
$json2=json_decode($output, true);
echo "<pre>";
echo json_encode($json2, JSON_PRETTY_PRINT);
echo "</pre>";
I get output as following:
[
{
"stream": "live\/cm1",
"size": 1120438999,
"duration": 16233,
"periods": 7,
"path": "\/var\/cache\/nimble\/dvr\/live\/cm1",
"space_available": 212516282368,
"vcodec": "avc1.4d001f",
"resolution": "1280x720",
"bandwidth": 552176,
"timeline": [
{
"start": 1477757618,
"duration": 535
},
{
"start": 1477758226,
"duration": 703
},
{
"start": 1477760431,
"duration": 14295
},
{
"start": 1477829472,
"duration": 559
},
{
"start": 1477879433,
"duration": 40
},
{
"start": 1477881429,
"duration": 79
},
{
"start": 1477881925,
"duration": 22
}
]
},
{
"stream": "live\/cm10",
"size": 1790211828,
"duration": 33976,
"periods": 23,
"path": "\/var\/cache\/nimble\/dvr\/live\/cm10",
"space_available": 212516282368,
"vcodec": "avc1.4d001f",
"resolution": "1280x720",
"bandwidth": 421520,
"timeline": [
{
"start": 1477757606,
"duration": 193
},
{
"start": 1477757817,
"duration": 336
},
{
"start": 1477758226,
"duration": 703
},
{
"start": 1477759027,
"duration": 1378
},
{
"start": 1477760460,
"duration": 14273
},
{
"start": 1477829464,
"duration": 1235
},
{
"start": 1477878029,
"duration": 469
},
{
"start": 1477882600,
"duration": 1260
},
{
"start": 1477883914,
"duration": 208
},
{
"start": 1477911214,
"duration": 1528
},
{
"start": 1477913185,
"duration": 185
},
{
"start": 1477913546,
"duration": 759
},
{
"start": 1477915819,
"duration": 68
},
{
"start": 1478782219,
"duration": 69
},
{
"start": 1478782375,
"duration": 76
},
{
"start": 1478812920,
"duration": 659
},
{
"start": 1478911726,
"duration": 2334
},
{
"start": 1478914138,
"duration": 5
},
{
"start": 1478914233,
"duration": 2872
},
{
"start": 1478917165,
"duration": 1133
},
{
"start": 1478976623,
"duration": 224
},
{
"start": 1478981537,
"duration": 3658
},
{
"start": 1479065575,
"duration": 351
}
]
},
{
"stream": "live\/cm2",
"size": 1320002727,
"duration": 20809,
"periods": 13,
...........
}]
The question I have is how do I access various elements here including how many total sets are there and in each set access a particular object.
Thanks for your help in advance.
Why are you decoding the output and after that you are encoding it again? Look what json_decode does: http://php.net/manual/de/function.json-decode.php
The decoding of your output seems to work well.
So just do the following instead:
$json2=json_decode($output, true);
echo "<pre>";
var_dump($json2);
echo "</pre>";
You will probably get an PHP array containing objects. On this array you can apply anything which is possible with arrays in PHP. You can use count() for instance to count the number of results in the array.
http://php.net/manual/en/language.types.array.php
$json2 is a associative array, access is done using $json[0]["stream"] (for example).
To access all elements, try
foreach( $json2 as $item ) {
echo $item["stream"] . " has a size of " . $item["size"] . " bytes";
foreach( $item["timeline"] as $timeline ) {
//access the timeline-elements (in the case you actually need those)
echo $timeline["start"];
}
}
(See also Parsing JSON object in PHP using json_decode)

Having trouble displaying Multiple Home Card Display on Alexa in Single Response

I have a application running on ALEXA (Amazon Echo). I'm implementing Home card display with images. When for a particular call if the response has only one card. It shows perfectly fine. Screenshot attached
"Response": {
"version" : "1.0",
"sessionAttributes": {
"UserCityId": "1",
"UserCity": "London",
"latitude": "51.50735090",
"longitude": "-0.12775830",
"categoryID": "900",
"company_id": "1154916",
"first_name": "User FName",
"yes_no_for": "",
"cat_start_limit": "0",
"categoryName": "music",
"total_cat_events": "2178",
"show_rec": "5"
},
"response": {
"outputSpeech": {
"type": "PlainText",
"text": "This is what alexa speaks out"
},
"card": {
"type": "Standard",
"title": "Music events in London",
"text": "The speech passed to alexa",
"image": {
"smallImageUrl": "https:\/\/xyz.com\/version\/sports-car-1374425_960_720.jpg",
"largeImageUrl": "https:\/\/xyz.com\/version\/sports-car-1374425_960_720.jpg"
}
},
"reprompt": {
"outputSpeech": {
"type": "PlainText",
"text": "Would you like to know events for music ,sports, or something else. Please select one category"
}
},
"shouldEndSession": false
}
}
The above code works
The problem is listed below :
But if the same response has array of cards (multiple cards), alexa fails giving an error.
"Response": {
"version": "1.0",
"sessionAttributes": {
"UserCityId": "1",
"UserCity": "London",
"latitude": "51.50735090",
"longitude": "-0.12775830",
"categoryID": "900",
"customer_id": "1154916",
"first_name": "UserFNAME",
"yes_no_for": "",
"cat_start_limit": "0",
"categoryName": "music",
"total_cat_events": "2178",
"show_rec": "5"
},
"response" : {
"outputSpeech" : {
"type": "PlainText",
"text": "5th. Baba Yaga's Hut Presents Jambinai, at Corsica Studios, on Monday, May 16th , 7 30 pm"
},
"card": [{
"type": "Standard",
"title": "Music events in London",
"text": "1. Dove Jones Blues Jam!, at Phoenix Artist Club, on Monday, May 16th , 7pm",
"image": {
"smallImageUrl": "https:\/\/xyz.com\/version\/sports-car-1374425_960_720.jpg",
"largeImageUrl": "https:\/\/xyz.com\/version\/sports-car-1374425_960_720.jpg"
}
}, {
"type": "Standard",
"title": "Music events in London",
"text": "2. The Hillbilly Moon Explosion, at The Borderline, on Monday, May 16th, 7pm",
"image": {
"smallImageUrl": "https:\/\/xyz.com\/version\/sports-car-1374425_960_720.jpg",
"largeImageUrl": "https:\/\/xyz.com\/version\/sports-car-1374425_960_720.jpg"
}
}, {
"type": "Standard",
"title": "Music events in London",
"text": "3. Sian Evans Of Kosheen, at 100 Club, on Monday, May 16th , 7 30 pm",
"image": {
"smallImageUrl": "https:\/\/xyz.com\/version\/sports-car-1374425_960_720.jpg",
"largeImageUrl": "https:\/\/xyz.com\/version\/sports-car-1374425_960_720.jpg"
}
}, {
"type": "Standard",
"title": "Music events in London",
"text": "4. Rinocerose, at The Water Rats, on Monday, May 16th , 7 30 pm",
"image": {
"smallImageUrl": "https:\/\/xyz.com\/version\/sports-car-1374425_960_720.jpg",
"largeImageUrl": "https:\/\/xyz.com\/version\/sports-car-1374425_960_720.jpg"
}
}, {
"type": "Standard",
"title": "Music events in London",
"text": "5. Baba Yaga's Hut Presents Jambinai, at Corsica Studios, on Monday, May 16th , 7 30 pm",
"image": {
"smallImageUrl": "https:\/\/xyz.com\/version\/sports-car-1374425_960_720.jpg",
"largeImageUrl": "https:\/\/xyz.com\/version\/sports-car-1374425_960_720.jpg"
}
}],
"reprompt": {
"outputSpeech": {
"type": "PlainText",
"text": "Would you like to know events for music ,sports, or something else. Please select one category"
}
},
"shouldEndSession" : false
}
}
The above code doesn't work.
Here is the thing that I'm trying to convert : Please see the screenshot
For events in london call I get list of 5 events in single card. To each event I want to assign image and create its card.
Can anyone help with the approach and figure out what is the problem using multiple card in JSON response.
Is it that alexa only support single card in response ?
The card attribute on the response takes an object not an array.
Here is the example that Amazon uses on their site:
{
"version": "string",
"sessionAttributes": {
"string": object
},
"response": {
"outputSpeech": {
"type": "string",
"text": "string",
"ssml": "string"
},
"card": {
"type": "string",
"title": "string",
"content": "string",
"text": "string",
"image": {
"smallImageUrl": "string",
"largeImageUrl": "string"
}
},
"reprompt": {
"outputSpeech": {
"type": "string",
"text": "string",
"ssml": "string"
}
},
"shouldEndSession": boolean
}
}
If you wanted to try and make multiple cards, you could try sending multiple responses, but I am not sure that that would work either.
I think your best option is to consider a different interaction with the user for your problem then presenting multiple cards.

Get the values in PHP of three objects in Tripadvisor JSON API file

Been struggling with this for too long now, so am kindly asking for your help.
How can I, using PHP, get the values of the text fields in the reviews of which there are three in this JSON file below.
Want to use a foreach loop for this, thanks for helping me out!
{
"address_obj": {
"street1": "Rustenburgerstreet 384",
"street2": null,
"city": "Amsterdam",
"state": "North Holland Province",
"country": "The Netherlands",
"postalcode": "1072 HG",
"address_string": "Rustenburgerstreet 384, 1072 HG Amsterdam The Netherlands"
},
"percent_recommended": null,
"latitude": "52.35162",
"rating": "5.0",
"attraction_types": [
{
"name": "concerts",
"localized_name": "Concerts"
},
{
"name": "blues bars",
"localized_name": "Blues Bars"
},
{
"name": "jazz bars",
"localized_name": "Jazz Bars"
},
{
"name": "bar/ clubs",
"localized_name": "Bars & Clubs"
}
],
"wikipedia_info": null,
"location_id": "3724036",
"review_rating_count": {
"1": "0",
"2": "0",
"3": "1",
"4": "4",
"5": "35"
},
"ranking_data": {
"ranking_string": "#12 of 73 Theater & Concerts in Amsterdam",
"ranking_out_of": "73",
"geo_location_id": "188590",
"ranking": "12",
"geo_location_name": "Amsterdam"
},
"photo_count": "35",
"location_string": "Amsterdam, North Holland Province",
"trip_types": [
{
"name": "business",
"value": "0",
"localized_name": "Business"
},
{
"name": "couples",
"value": "8",
"localized_name": "Couples"
},
{
"name": "solo",
"value": "7",
"localized_name": "Solo travel"
},
{
"name": "family",
"value": "0",
"localized_name": "Family"
},
{
"name": "friends",
"value": "21",
"localized_name": "Friends getaway"
}
],
"web_url": "Attraction_Review-g188590-d3724036-Reviews-m34757-CC_Music_Cafe-Amsterdam_North_Holland_Province.html",
"reviews": [
{
"id": "353301385",
"lang": "en",
"location_id": "3724036",
"published_date": "2016-03-06T05:20:19-0500",
"rating": 5,
"helpful_votes": "0",
"rating_image_url": "img/cdsi/img2/ratings/traveler/s5.0-34757-5.png",
"url": "ShowUserReviews-g188590-d3724036-r353301385-CC_Music_Cafe-Amsterdam_North_Holland_Province.html#review353301385",
"trip_type": "Solo travel",
"travel_date": "2016-02",
"text": "I am a regular visitor of CC Muziekcafé Amsterdam but have felt at home from the very first time. What I like about CC is the atmosphere where great music and hospitality are mixed in the best way...",
"user": {
"username": "Yvon H",
"user_location": {
"name": "Groningen Province, The Netherlands",
"id": "188570"
}
},
"title": "A great place to hear live music and meet all sorts of interesting people, both local and traveling",
"is_machine_translated": false
},
{
"id": "351658487",
"lang": "en",
"location_id": "3724036",
"published_date": "2016-02-28T11:13:12-0500",
"rating": 5,
"helpful_votes": "0",
"rating_image_url": "img/cdsi/img2/ratings/traveler/s5.0-34757-5.png",
"url": "ShowUserReviews-g188590-d3724036-r351658487-CC_Music_Cafe-Amsterdam_North_Holland_Province.html#review351658487",
"trip_type": "Friends getaway",
"travel_date": "2016-02",
"text": "4th time we have been here, another great night at the music cafe, friendly people and a barman who knows how to just put enough swear words in to sound cool",
"user": {
"username": "Francois S",
"user_location": {
"name": "Cardiff, United Kingdom",
"id": "186460"
}
},
"title": "Jazz funk Jam session night Thursday",
"is_machine_translated": false
},
{
"id": "350605184",
"lang": "en",
"location_id": "3724036",
"published_date": "2016-02-24T10:18:57-0500",
"rating": 5,
"helpful_votes": "1",
"rating_image_url": "img/cdsi/img2/ratings/traveler/s5.0-34757-5.png",
"url": "ShowUserReviews-g188590-d3724036-r350605184-CC_Music_Cafe-Amsterdam_North_Holland_Province.html#review350605184",
"trip_type": "Couples",
"travel_date": "2015-09",
"text": "CC muziekcafe is a very cosy place with excellent live music and interaction with the musicians. What really makes the place is the owner Rene who knows a lot about music and now and then even sings...",
"user": {
"username": "ImagineNL",
"user_location": {
"name": "Schagen, The Netherlands",
"id": "609049"
}
},
"title": "Cupid",
"is_machine_translated": false
}
],
You can use this code
<?php
$json = <<<EOF
{
"address_obj": {
"street1": "Rustenburgerstreet 384",
"street2": null,
"city": "Amsterdam",
"state": "North Holland Province",
"country": "The Netherlands",
"postalcode": "1072 HG",
"address_string": "Rustenburgerstreet 384, 1072 HG Amsterdam The Netherlands"
},
"percent_recommended": null,
"latitude": "52.35162",
"rating": "5.0",
"attraction_types": [
{
"name": "concerts",
"localized_name": "Concerts"
},
{
"name": "blues bars",
"localized_name": "Blues Bars"
},
{
"name": "jazz bars",
"localized_name": "Jazz Bars"
},
{
"name": "bar/ clubs",
"localized_name": "Bars & Clubs"
}
],
"wikipedia_info": null,
"location_id": "3724036",
"review_rating_count": {
"1": "0",
"2": "0",
"3": "1",
"4": "4",
"5": "35"
},
"ranking_data": {
"ranking_string": "#12 of 73 Theater & Concerts in Amsterdam",
"ranking_out_of": "73",
"geo_location_id": "188590",
"ranking": "12",
"geo_location_name": "Amsterdam"
},
"photo_count": "35",
"location_string": "Amsterdam, North Holland Province",
"trip_types": [
{
"name": "business",
"value": "0",
"localized_name": "Business"
},
{
"name": "couples",
"value": "8",
"localized_name": "Couples"
},
{
"name": "solo",
"value": "7",
"localized_name": "Solo travel"
},
{
"name": "family",
"value": "0",
"localized_name": "Family"
},
{
"name": "friends",
"value": "21",
"localized_name": "Friends getaway"
}
],
"web_url": "Attraction_Review-g188590-d3724036-Reviews-m34757-CC_Music_Cafe-Amsterdam_North_Holland_Province.html",
"reviews": [
{
"id": "353301385",
"lang": "en",
"location_id": "3724036",
"published_date": "2016-03-06T05:20:19-0500",
"rating": 5,
"helpful_votes": "0",
"rating_image_url": "img/cdsi/img2/ratings/traveler/s5.0-34757-5.png",
"url": "ShowUserReviews-g188590-d3724036-r353301385-CC_Music_Cafe-Amsterdam_North_Holland_Province.html#review353301385",
"trip_type": "Solo travel",
"travel_date": "2016-02",
"text": "I am a regular visitor of CC Muziekcafé Amsterdam but have felt at home from the very first time. What I like about CC is the atmosphere where great music and hospitality are mixed in the best way...",
"user": {
"username": "Yvon H",
"user_location": {
"name": "Groningen Province, The Netherlands",
"id": "188570"
}
},
"title": "A great place to hear live music and meet all sorts of interesting people, both local and traveling",
"is_machine_translated": false
},
{
"id": "351658487",
"lang": "en",
"location_id": "3724036",
"published_date": "2016-02-28T11:13:12-0500",
"rating": 5,
"helpful_votes": "0",
"rating_image_url": "img/cdsi/img2/ratings/traveler/s5.0-34757-5.png",
"url": "ShowUserReviews-g188590-d3724036-r351658487-CC_Music_Cafe-Amsterdam_North_Holland_Province.html#review351658487",
"trip_type": "Friends getaway",
"travel_date": "2016-02",
"text": "4th time we have been here, another great night at the music cafe, friendly people and a barman who knows how to just put enough swear words in to sound cool",
"user": {
"username": "Francois S",
"user_location": {
"name": "Cardiff, United Kingdom",
"id": "186460"
}
},
"title": "Jazz funk Jam session night Thursday",
"is_machine_translated": false
},
{
"id": "350605184",
"lang": "en",
"location_id": "3724036",
"published_date": "2016-02-24T10:18:57-0500",
"rating": 5,
"helpful_votes": "1",
"rating_image_url": "img/cdsi/img2/ratings/traveler/s5.0-34757-5.png",
"url": "ShowUserReviews-g188590-d3724036-r350605184-CC_Music_Cafe-Amsterdam_North_Holland_Province.html#review350605184",
"trip_type": "Couples",
"travel_date": "2015-09",
"text": "CC muziekcafe is a very cosy place with excellent live music and interaction with the musicians. What really makes the place is the owner Rene who knows a lot about music and now and then even sings...",
"user": {
"username": "ImagineNL",
"user_location": {
"name": "Schagen, The Netherlands",
"id": "609049"
}
},
"title": "Cupid",
"is_machine_translated": false
}
]
}
EOF;
$array = json_decode($json, true);
$texts = array_map(
function($item) {
return $item['text'];
}, $array['reviews']
);
It doesn't seem like you need a foreach to fetch the reviews element. Not sure if I understood your question correct, but did you want something like this:
$assoc_json = json_decode($your_json, true);
var_dump($assoc_json['reviews']);
The above turns your json into an associative array, and just access the review element.
A simple, naive approach would be:
//Loads your json file 'tripadvisor' into jsonString
$jsonString = file_get_contents("/tripadvisor.json");
//Turns your string into an associative array
$tripJsonAssoc = json_decode($jsonString, true);
//Iterate through each review and store it in result
$result = array();
foreach($tripJsonAssoc['reviews'] as $review) {
$result[] = array('text' => $review['text'],
'rating' => $review['rating']);
}
//Do what you need to do with result
//...
Or you can just do your own stuff inside the foreach loop.
This answer gives you another way (more elegant) to create the 'result' array, but since you asked for a 'foreach', I thought I would add my 5 cents.

Categories