How to retrieve values from multilevel json - php

Im trying to get the values from the various sections of this json entry.
{
"gallery_show": "1",
"gallery": {
"path": ["images\/slide2.jpg", "images\/slide1.jpg", "images\/slide2.jpg", "images\/slide1.jpg", "images\/slide2.jpg", "images\/slide1.jpg"],
"title": ["", "", "", "", "", ""],
"caption": ["", "", "", "", "", ""],
"thumb": ["\/admin\/cache\/thumbs_200x150\/slide2_200x150.jpg",
"\/admin\/cache\/thumbs_200x150\/slide1_200x150.jpg",
"\/admin\/cache\/thumbs_200x150\/slide2_200x150.jpg",
"\/admin\/cache\/thumbs_200x150\/slide1_200x150.jpg",
"\/admin\/cache\/thumbs_200x150\/slide2_200x150.jpg",
"\/admin\/cache\/thumbs_200x150\/slide1_200x150.jpg"]
},
"videos_show": "1",
"videos": {
"title": ["Arnie", "Arnie", "Arnie", "Arnie", "Arnie"],
"sharelink": ["http:\/\/youtu.be\/7kTz6MVrBlY", "http:\/\/youtu.be\/7kTz6MVrBlY", "http:\/\/youtu.be\/7kTz6MVrBlY", "http:\/\/youtu.be\/7kTz6MVrBlY", "http:\/\/youtu.be\/7kTz6MVrBlY"]
},
"attachments_show": "1",
"attachments": {
"path": ["images\/slide2.jpg", "images\/slide1.jpg"],
"title": ["Attchment", "Attchment"],
"caption": ["Attachment Description", "Attachment Description"]
},
"links_show": "1",
"links": {
"title": ["Link1", "Link2"],
"httplink": ["http:\/\/www.mydomain.com", "http:\/\/www.mydomain.com"],
"targetlink": ["_blank", "_blank"]
}
}
If i use this method
$entries= json_decode($this->item->entries);
and then i echo this
echo $entries->gallery_show;
I get the desired result = 1
But how do i now get the things beneath it if gallery_show=1 and display them as values i can use inside my php?
Any help for this old brain would be much appreciated.
Cheers in advance
Jonny

You could do somewhat like this...
$arr = json_decode($json);
foreach($arr as $k=>$v)
{
if($k=='gallery_show' && $v==1)
{
foreach($arr->gallery->path as $path)
{
echo "<img src=$path />";
}
break;
}
}
Demo
Code update..
$arr = json_decode($json);
$i=0;
foreach($arr as $k=>$v)
{
if($k=='gallery_show' && $v==1)
{
foreach($arr->gallery as $gall)
{
echo "Title :".$arr->gallery->title[$i]."<br/>";
echo "Caption :".$arr->gallery->caption[$i]."<br/>";
echo "Thumbnail :".$arr->gallery->thumb[$i]."<br>";
echo "<img src=".$arr->gallery->path[$i]." /><br>";
$i++;
}
break;
}
}
Demo

Related

How to display info on double array JSON (php)

I would like to know how to display the information below.
Link (JSON):
https://my.callofduty.com/api/papi-client/ce/v1/title/bo4/platform/psn/match/11337378706913618925/matchMapEvents
What I wish to have:
"teams": [
[
{
"provider": "psn",
"username": "Germania1992"
},
{
"provider": "psn",
"username": "killzoneprofi"
},
{
"provider": "psn",
"username": "ayozetf87"
},
{
"provider": "psn",
"username": "Seith911"
},
{
"provider": "psn",
"username": "domibreu92"
}
],
[
{
"provider": "psn",
"username": "Thejuankarboy"
},
{
"provider": "psn",
"username": "Gamermad101"
},
{
"provider": "psn",
"username": "Izdrap"
},
{
"provider": "psn",
"username": "Guerra_sv"
},
{
"provider": "psn",
"username": "TriX_FollOoW_YT"
}
]
],
I want to display the nicks of the different teams
Example: Team 1 = Germania1992, killzoneprofi, ayozetf87, Seith911,
domibreu92
Thanks
You will have to do a double foreach that for sure.
<?php
// Get the json of the team
$team = team("https://my.callofduty.com/api/papi-client/ce/v1/title/bo4/platform/psn/match/11337378706913618925/matchMapEvents");
// Display the teams info
foreach($team as $nb=>$data){
echo "Team $nb<br />";
foreach($data as $key=>$value){
echo "Provider: ".$value->provider."<br />";
echo "Username: ".$value->username."<br />";
}
echo "<hr>";
}
// Returns the team array of the json
function team($jsonURL){
$content=file_get_contents($jsonURL);
$data=json_decode($content);
return $data->data->teams;
}
?>
You could increment the team number by 1 to avoid the first being 0
The above will display the below screenshot (You can format the output as you please)
$result = [];
$counter = 0;
dump($array = json_decode(file_get_contents('https://my.callofduty.com/api/papi-client/ce/v1/title/bo4/platform/psn/match/11337378706913618925/matchMapEvents')));
dump($t = array_column((array)$array, 'teams'));
foreach ($t as $r) {
foreach ($r as $p) {
$counter++;
foreach ($p as $value){
$result["team$counter"][] = $value->username;
}
}
}
var_dump($result);

Get json object that matching maximum words from a string using php

I have a json response like this:
{
"status": 200,
"msg": "OK",
"result": {
"folders": [
{
"id": "3812454",
"name": ".subtitles"
},
{
"id": "3812455",
"name": ".videothumb"
}
],
"files": [
{
"name": "Angamaly Diaries HD.MP4.mp4",
"cblock": null,
"sha1": "fcc2c99f2db6e3e8a700c3247206a1c2148e14cb",
"folderid": "3812453",
"upload_at": "1510255141",
"status": "active",
"size": "713544705",
"content_type": "video/mp4",
"download_count": "0",
"cstatus": "ok",
"linkextid": "PjUv5IYA2J8"
},
{
"name": "Take Off 2017.MP4.mp4",
"cblock": null,
"sha1": "2fe7fb4d45322a085d41239d6429d1cc8e94e2ce",
"folderid": "3812453",
"upload_at": "1510255141",
"status": "active",
"size": "954148848",
"content_type": "video/mp4",
"download_count": "0",
"cstatus": "ok",
"linkextid": "BIBcjWqF0_I"
},
{
"name": "Rangoon 2017 Tamil.MP4.mp4",
"cblock": null,
"sha1": "c685e7c11636982860ae7f34b671a20fc746feee",
"folderid": "3812453",
"upload_at": "1510255141",
"status": "active",
"size": "779899588",
"content_type": "video/mp4",
"download_count": "0",
"cstatus": "ok",
"linkextid": "00D7GzP6mls"
},
{
"name": "The Zookeeper’s Wife 2017.MP4.mp4.mp4",
"cblock": null,
"sha1": "a143faafbd8a6eaf2276f25cd642ac3019d71ffc",
"folderid": "3812453",
"upload_at": "1510256266",
"status": "active",
"size": "550126461",
"content_type": "video/mp4",
"download_count": "0",
"cstatus": "ok",
"linkextid": "bwUhqbiJJWQ"
}
]
}
}
And I have a string with this text:
"Watch Take Off 2017 Malayalam Full Movie Online Free"
Now I need to get the linkextid of
"name": "Take Off 2017.MP4.mp4"
from JSON response. There is one more thing that I have so much similar data on the JSON response, but I need to the get name that matches maximum words from string using PHP.
<?php
$json_data='{"status":200,"msg":"OK","result":{"folders":[{"id":"3812454","name":".subtitles"},{"id":"3812455","name":".videothumb"}],"files":[{"name":"Angamaly Diaries HD.MP4.mp4","cblock":null,"sha1":"fcc2c99f2db6e3e8a700c3247206a1c2148e14cb","folderid":"3812453","upload_at":"1510255141","status":"active","size":"713544705","content_type":"video/mp4","download_count":"0","cstatus":"ok","linkextid":"PjUv5IYA2J8"},{"name":"Take Off 2017.MP4.mp4","cblock":null,"sha1":"2fe7fb4d45322a085d41239d6429d1cc8e94e2ce","folderid":"3812453","upload_at":"1510255141","status":"active","size":"954148848","content_type":"video/mp4","download_count":"0","cstatus":"ok","linkextid":"BIBcjWqF0_I"},{"name":"Rangoon 2017 Tamil.MP4.mp4","cblock":null,"sha1":"c685e7c11636982860ae7f34b671a20fc746feee","folderid":"3812453","upload_at":"1510255141","status":"active","size":"779899588","content_type":"video/mp4","download_count":"0","cstatus":"ok","linkextid":"00D7GzP6mls"},{"name":"The Zookeeper’s Wife 2017.MP4.mp4.mp4","cblock":null,"sha1":"a143faafbd8a6eaf2276f25cd642ac3019d71ffc","folderid":"3812453","upload_at":"1510256266","status":"active","size":"550126461","content_type":"video/mp4","download_count":"0","cstatus":"ok","linkextid":"bwUhqbiJJWQ"}]}}';
$data=json_decode($json_data,true);
$files=$data['result']['files'];
$search="Watch Take Off 2017 Malayalam Full Movie Online Free";
$search_array=explode(' ',$search);
foreach($search_array as $key=>$row){
$search_array[$key]=trim($row);
}
$match=[];
foreach($files as $key=>$row){
$match_count=0;
foreach($search_array as $s){
if(preg_match('/'.$s.'/',$row['name'])){
$match_count+=1;
}
}
$match[$key]=$match_count;
}
rsort($match);
print_r($files[$match[0]]['linkextid'])
?>
Not the best solution in the world but this should work:
<?php
function compare_strings($s1, $s2) {
$s1Words = explode(' ', $s1);
$s2Words = explode(' ', $s2);
$wordCount = 0;
foreach ($s1Words as $word) {
if ( strpos($s2Words, $word) ) {
$wordCount++;
}
}
return $wordCount;
}
function get_movie_linkextid($data, $name) {
$bestMatchIndex = 1;
$bestMatchScore = 0;
foreach ($data['result']['files'] as $i => $movie) {
$currentScore = compare_strings($movie['name'], $name);
if ($currentScore > $bestMatchScore) {
$bestMatchScore = $currentScore;
$bestMatchIndex = $i;
}
}
return $data['result']['files'][$bestMatchIndex]['linkextid'];
}
$data = json_decode($yourJsonData, true);
$name = "Watch Take Off 2017 Malayalam Full Movie Online Free";
echo get_movie_linkextid($data, $name);
?>
The code just parses your JSON into a variable and than goes through all the 'files' comparing their names with the string you provided the determine the best match. After that it just return the linkextid.
I Didn't tested this code but the important is to get the idea since you will have to adapt it to be more generic anyway.

Laravel - Format JSON

I have a code that outputs the json result below:
{
"Ghost in the Shell": {
"id": 1203,
"Pipeline": {
"id": 6144,
"name": "Pipeline",
"status": "New",
"item_id": 5962,
"TotalTasksOpen": 2
}
}
}
Now how can I format it in the my desired json format below:
Note* I have to remove some result.
{
"Ghost in the Shell":
"id": 6144,
"name": "Pipeline",
"status": "New",
"item_id": 5962,
"TotalTasksOpen": 2
}
Will appreciate if anyone can help me please.
I am not sure what is your purpose , but here is what you need
<?php
$json = '{
"Ghost in the Shell": {
"id": 1203,
"Pipeline": {
"id": 6144,
"name": "Pipeline",
"status": "New",
"item_id": 5962,
"TotalTasksOpen": 2
}
}
}';
$array = json_decode($json, true);
$newArray = array();
foreach($array as $key=>$value) {
$newArray[$key] = '';
foreach($value as $k=>$v) {
if(is_array($v)) {
$newArray = array_merge($newArray,$v);
}
}
}
$newJson = json_encode($newArray);
echo $newJson;
?>

Mootools get first value from each

I have one array images from Json:
images=JSON.decode(images);
This is the array value :
[
{
"title": "",
"description": "",
"name": "loc commerciale.jpg"
},
{
"title": "",
"description": "",
"name": "foto2.jpg"
},
{
"title": "",
"description": "",
"name": "foto 1.jpg"
},
{
"title": "",
"description": "",
"name": "a01.jpg"
}
]
I get the value from name :
images.each(function(image){
alert(image.name);
});
I need to get only the first value name
Like php :
$images = (array) json_decode($row->images);
$first = true;
foreach ($images as $k => $image) {
if ($first)
{
$firstimage= $image->name;
$first = false;
}
}
Try:
if (images[0].length){
var firstImageName = images[0].name;
}

How would you compare the names in a list to ones in an Array?

I have a list of movies and I want to compare to the Array of movies i got from Facebook Graph API.
Here is an example of what API is getting:
{"data": [
{
"name": "The Drift Bible",
"category": "Movie",
"id": "227431881228",
"created_time": "2011-02-27T21:41:04+0000"
},
{
"name": "Shooter",
"category": "Movie",
"id": "109671005718938",
"created_time": "2011-02-16T09:18:29+0000"
}...
The list I need to compare is pretty big, but here are some:
Wall Street, Shooter, Young Guns, Due Date...
Basically just compare 'Name' in 'data' with 'my list' of Movie titles. But can't figure out the syntax
Something like:
if ($movies->data->name == 'movie titles list') {echo "You like the same movies";}
I found this:
if (in_array('movie titles list', $a)) {echo "You like the same movies";}
Any thoughts would help me out.
Thanks
The data looks to be JSON encoded...
Try something like this: (json_decode)
$large_data = '{"data": [
{
"name": "The Drift Bible",
"category": "Movie",
"id": "227431881228",
"created_time": "2011-02-27T21:41:04+0000"
},
{
"name": "Shooter",
"category": "Movie",
"id": "109671005718938",
"created_time": "2011-02-16T09:18:29+0000"
}]}';
$json_to_array = json_decode($large_data, true);
var_dump(json_decode($large_data, true));
// You should now be able to compare the two array
echo print_r($json_to_array,true);
EDIT:
Improving on what #Eric posted
$large_data = '{"data": [
{
"name": "The Drift Bible",
"category": "Movie",
"id": "227431881228",
"created_time": "2011-02-27T21:41:04+0000"
},
{
"name": "Shooter",
"category": "Movie",
"id": "109671005718938",
"created_time": "2011-02-16T09:18:29+0000"
}]}';
$movie_list = json_decode($large_data, true);
$movieNames = array();
foreach($movie_list as $movies) {
foreach($movies as $movie) {
$movieNames[] = $movie['name'];
}
}
$myMovies = array('Wall Street', 'Shooter', 'Young Guns', 'Due Date');
$common_movies = array_intersect($movieNames, $myMovies);
foreach($common_movies as $common_movie) {
echo "We like the same movie ".$common_movie."<br />\n";
}
Another method:
<?php
$json_data = '{"data": [
{
"name": "The Drift Bible",
"category": "Movie",
"id": "227431881228",
"created_time": "2011-02-27T21:41:04+0000"
},
{
"name": "Shooter",
"category": "Movie",
"id": "109671005718938",
"created_time": "2011-02-16T09:18:29+0000"
}
]}';
$array_data = json_decode($json_data, TRUE);
$compare_list = array(
'Wall Street',
'Shooter',
'Young Guns',
'Due Date'
);
// <Marco Stumper> phpundhtml at web dot de
function in_array_multi($needle, $haystack) {
$found = false;
foreach ($haystack as $value) {
if ((is_array($value) && in_array_multi($needle, $value)) || $value == $needle) {
$found = true;
}
}
return $found;
}
foreach ($compare_list as $item) {
echo '<p>' . $item . (in_array_multi($item, $array_data) ? ' DOES ' : ' does NOT ') . 'exist within $array_data</p>' . PHP_EOL;
}
?>
Output:
<p>Wall Street does NOT exist within $array_data</p>
<p>Shooter DOES exist within $array_data</p>
<p>Young Guns does NOT exist within $array_data</p>
<p>Due Date does NOT exist within $array_data</p>
Use array_intersect(array1, array2, ... arrayN)
$large_data = '{"data": [
{
"name": "The Drift Bible",
"category": "Movie",
"id": "227431881228",
"created_time": "2011-02-27T21:41:04+0000"
},
{
"name": "Shooter",
"category": "Movie",
"id": "109671005718938",
"created_time": "2011-02-16T09:18:29+0000"
}
]}';
$movies = json_decode($large_data, true)['data'];
$movieNames = array();
//Get only the name from the movie list
foreach($movies as $movie) {
$movieNames[] = $movie['name'];
}
//Intersect with internal list
print_r(array_intersect($movieNames, $myMovies));

Categories