我有两个json文件,我想将两个JSON文件合并为一个。
腓
<input type="text" name="image" />
<input type="text" name="file" />
第一次输入的Javascript
$('input[name=\'image\']').autocomplete({
minLength: 2,
source: function( request, response ) {
$.getJSON( "files/", {
term: extractLast( request.term )
}, response );
},
create: function () {
$(this).data("autocomplete")._renderItem = function(ul, item) {
return $("<li></li>")
.data("item.autocomplete", item)
.append('<a><img src="files/' + item.label + '.jpg' />
' + item.label + '</c></a>')
.appendTo( ul );
};
},
});
第二次输入的Javascript
$('input[name=\'file\']').autocomplete({
minLength: 2,
source: function( request, response ) {
$.getJSON( "files/", {
term: extractLast( request.term )
}, response );
},
create: function () {
$(this).data("autocomplete")._renderItem = function(ul, item) {
return $("<li></li>")
.data("item.autocomplete", item)
.append('<a>' + item.label + '</c></a>')
.appendTo( ul );
};
},
});
第一次输入的json
if (empty($_GET['term'])) exit ;
$q = strtolower($_GET["term"]);
if (get_magic_quotes_gpc()) $q = stripslashes($q);
$files = array();
foreach(glob('image/*.jpg*', GLOB_BRACE) as $key=>$file) {
$files[] = substr($file, 0, -4); }
$files = array_unique($files);
$files = array_combine($files, $files);
$result = array();
foreach ($files as $key=>$value) {
if (strpos(strtolower($key), $q) !== false) {
array_push($result,
array("id"=>$value, "label"=>$key,
"value" => strip_tags($key))); }
if (count($result) > 11) break; }
echo json_encode($result);
json for second input
if (empty($_GET['term'])) exit ;
$q = strtolower($_GET["term"]);
if (get_magic_quotes_gpc()) $q = stripslashes($q);
$files = array();
foreach(glob('image/*.txt*', GLOB_BRACE) as $key=>$file) {
$files[] = substr($file, 0, -4); }
$files = array_unique($files);
$files = array_combine($files, $files);
$result = array();
foreach ($files as $key=>$value) {
if (strpos(strtolower($key), $q) !== false) {
array_push($result,
array("id"=>$value, "label"=>$key,
"value" => strip_tags($key))); }
if (count($result) > 11) break; }
echo json_encode($result);
如何将所有内容整合到一起? json有一个问题因为第一次输入只显示带预览的图像文件名,第二次只显示文本文件名,那么如何将json文件集成到一个?
答案 0 :(得分:2)
以下是解决方案:
HTML:
<input type="text" name="image" />
<input type="text" name="file" />
使用Javascript:
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$("input[name='image'], input[name='file']").autocomplete({
minLength: 2,
source: function(request, response) {
var thisType = this.element[0].name;
$.getJSON("/test", {
term: extractLast(request.term),
type: thisType
}, response );
}
}).autocomplete( "instance" )._renderItem = function( ul, item ) {
var thisType = this.element[0].name;
if (thisType == 'image') {
return $("<li></li>")
.data("item.autocomplete", item)
.append('<a><img src="files/' + item.label + '.jpg" />' + item.label + '</a>')
.appendTo( ul );
} else {
return $("<li></li>")
.data("item.autocomplete", item)
.append('<a>' + item.label + '</a>')
.appendTo( ul );
}
};
PHP:
<?php
if (empty($_GET['term'])) exit;
$q = strtolower($_GET["term"]);
$type = strtolower($_GET["type"]);
if (get_magic_quotes_gpc()) $q = stripslashes($q);
$files = array();
if ($type == 'image') {
foreach(glob('image/*.jpg*', GLOB_BRACE) as $key=>$file) {
$files[] = substr($file, 0, -4);
}
} else {
foreach(glob('image/*.txt*', GLOB_BRACE) as $key=>$file) {
$files[] = substr($file, 0, -4);
}
}
$files = array_unique($files);
$files = array_combine($files, $files);
$result = array();
foreach ($files as $key=>$value) {
if (strpos(strtolower($key), $q) !== false) {
array_push($result, array("id"=>$value, "label"=>$key, "value" => strip_tags($key)));
}
if (count($result) > 11) break;
}
echo json_encode($result);