Mootools get first value from each - php

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

Related

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.

Search file contents of a json file in PHP and return results

I am trying to create a search a function in php which searches for the data contained inside a Json file. I am able to do this for a normal text file which returns a list of results based on what has been entered in the search field which returns closet match. However does not work when doing this same for a json file and does not return anything.
search.php
if (isset($_POST) && isset($_POST['txt'])) {
$search = $_POST['txt'];
$file = file('data/contacts.json');
$found = false;
$jsonData = json_decode($file, true);
foreach ($jsonData as $line) {
if (strpos($line, $search) !== false) {
$found = true;
echo $line;
}
}
if (!$found) {
echo 'No match found';
}
}
contacts.json
[
{
"id": 3,
"forename": "John",
"surname": "Smith",
"email": "j#hotmail.com",
"address": "addresss",
"telephone": "01232302323"
},
{
"id": 4,
"forename": "Keith",
"surname": "Miller",
"email": "K#hotmail.com",
"address": "ssdsds",
"telephone": "01232302323"
},
{
"id": 5,
"forename": "Doug",
"surname": "Howard",
"email": "d#hotmail.com",
"address": "test",
"telephone": "01232302323"
},
{
"id": 2,
"forename": "r",
"surname": "r",
"email": "email#gmail.com",
"address": "test",
"telephone": "01232302323"
}
]
Change this line $jsonData = json_decode($file, true); to
$jsonData = json_decode(json_encode($file),true);

get unique data from multi-dimensional array list

I have 2 records from 2 different query results. I have merged those data. but i have to display unique data depends on vName key.
Here is my result:
{
"val1": "9",
"vName": "abc",
"bname": "",
"cname": "",
"brid": "",
"cmpid": "154",
"logo": "",
"banner": "",
"description": "",
"catids": "",
"type": "company",
"is_nav": "1",
"bigthumb_logo": "",
"compressthumb_logo": "",
"bigthumb_banner": "",
"compressthumb_banner": "",
"sp_ar": "Aperitif"
},
{
"val1": "9",
"vName": "abc",
"bname": "abc",
"cname": "abc",
"brid": "341",
"cmpid": "154",
"logo": "",
"banner": "6c497c72bfec4c694c1cc2c49066e7a1.png",
"description": "http:\/\/www.abc.com\/",
"catids": "9",
"sp_ar": "Aperitif",
"type": "company",
"is_nav": "1",
"bigthumb_logo": "40cbcede9429cdb44895e0e6f4c050d2.png",
"compressthumb_logo": "40cbcede9429cdb44895e0e6f4c050d2.png",
"bigthumb_banner": "6c497c72bfec4c694c1cc2c49066e7a1.png",
"compressthumb_banner": "6c497c72bfec4c694c1cc2c49066e7a1.png"
}
Expected result should be only one of them, compare with vName:
{
"val1": "9",
"vName": "abc",
"bname": "",
"cname": "",
"brid": "",
"cmpid": "154",
"logo": "",
"banner": "",
"description": "",
"catids": "",
"type": "company",
"is_nav": "1",
"bigthumb_logo": "",
"compressthumb_logo": "",
"bigthumb_banner": "",
"compressthumb_banner": "",
"sp_ar": "Aperitif"
}
How can i get the result?
Convert the data into an array where the key is your unique value.
You can do something like:
$data = array('this is your array of data');
$unique = array();
foreach($data as $value) {
if(!array_key_exists($value['vName'], $unique)) {
$unique[$value['vName']] = $value;
}
}
// $unique now contains only arrays with a unique 'vName', using the first it finds in the original list
$new_array = array_values($unique);
Assuming you have a array of associative array stored in $data, the following code will filter it based on vName and store array of associative arrays with unique vName in $filtered_data
$taken = array();
$filtered_data = array_filter($data, function($value) {
global $taken;
if (in_array($value['vName'], $taken)) {
return false;
}
else {
$taken[] = $value['vName'];
return true;
}
});
Here, the variable $taken keeps track of all the vName values that are currently in the $filtered_data. If you find a duplicate vName (that is currently there in $taken), it discards that element.
Try this, it should work for you.
$input = array_map("unserialize", array_unique(array_map("serialize",$input)));

Filtering JSON Object and outputting another

I'm trying to parse a JSON file which looks like this,
[
{
"id": "539eebdba276db40a4716726",
"name": "Development Task",
"idList": "539eebbb4e9a8d709704b254",
"desc": "",
"url": ""
},
{
"id": "539eebe09b42c971d46b9ba1",
"name": "Design Task",
"idList": "539eebbe50dc4fa2a82474fc",
"desc": "",
"url": ""
}
]
I'm trying to get the desc object from the array with the name Development Task, the system needs to be dynamic so I can't just use json_o[0](desc);
I've tried different methods such as foreaching the data multiple times but I still can't think of a solution, any help would be great, cheers.
Try with this:
$jsonData = '[
{
"id": "539eebdba276db40a4716726",
"name": "Development Task",
"idList": "539eebbb4e9a8d709704b254",
"desc": "",
"url": ""
},
{
"id": "539eebe09b42c971d46b9ba1",
"name": "Design Task",
"idList": "539eebbe50dc4fa2a82474fc",
"desc": "",
"url": ""
}
]';
$encoded = json_decode($jsonData);
foreach($encoded as $data)
{
if('Development Task' == $data->name)
{
echo $data->desc;
}
}
If you search for dynamic fields, i would have gone with :
$jsonData = '[
{
"id": "539eebdba276db40a4716726",
"name": "Development Task",
"idList": "539eebbb4e9a8d709704b254",
"desc": "FirstDescription",
"url": ""
},
{
"id": "539eebe09b42c971d46b9ba1",
"name": "Design Task",
"idList": "539eebbe50dc4fa2a82474fc",
"desc": "SecondDescription",
"url": ""
}
]';
$arrayFromJson = json_decode($jsonData, true);
function searchByKey($keyToSearchIn, $searchName, $array) {
foreach ($array as $key => $val) {
if ($val[$keyToSearchIn] == $searchName) {
return $val['desc'];
}
}
return null;
}
$return = searchByKey("name", "Development Task", $arrayFromJson);
So
var_dump($return);
returns
string(16) "FirstDescription"
Note : If you only need a search by name, you can change the function with :
function searchByName( $searchName, $array) {
foreach ($array as $key => $val) {
if ($val['name'] == $searchName) {
return $val['desc'];
}
}
return null;
}
$return = searchByName("Development Task", $arrayFromJson);
Hope it helps.
The comments in the code explain what happens:
$jsonData = '[
{
"id": "539eebdba276db40a4716726",
"name": "Development Task",
"idList": "539eebbb4e9a8d709704b254",
"desc": "moocow",
"url": ""
},
{
"id": "539eebe09b42c971d46b9ba1",
"name": "Design Task",
"idList": "539eebbe50dc4fa2a82474fc",
"desc": "wowcow",
"url": ""
},
{
"id": "539eebe09rb42c971d46b9ba1",
"name": "Development Task",
"idList": "539eebbe50dc4fa2a82474fc",
"desc": "sowccow",
"url": ""
}
]';
$encoded = json_decode($jsonData, true); //converts the json object to an array
foreach ($encoded as $arrayObject){
if( in_array("Development Task", $arrayObject) ){
print_r($arrayObject['desc'] . "\n");
}
}
-> moocow sowccow
based upon
I'm trying to get the desc object from the array with the name Development Task
This should do what you want and you can easily take the string and make it a variable. There are plenty of built-in functions on PHP.net that are tuned and are probably 10x better than rolling your own code.

How to retrieve values from multilevel json

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

Categories