如何测量两个json文件?

时间:2015-06-24 14:47:56

标签: php json

相关问答here

首先是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:

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

如何将所有内容整合到一起?我有2个输入字段,json第一个输入只显示带预览的图像文件名,第二个只显示文本文件名,然后如何将json文件集成到一个?

3 个答案:

答案 0 :(得分:2)

假设您在代码片段末尾有有效数据,我会将它们转换为数组,合并它们,然后重新编码为json。

$json1 = '{"title" : [{"some data":"some more data"}]}';
$json2 = '{"title2" : [{"some data2":"some more data2"}]}';

$complete = array_merge(JSON_decode($json1, true), JSON_decode($json2, true));

$newJson = JSON_encode($complete);

heres a fiddle

答案 1 :(得分:1)

比较你的两个文件,这应该是你想要做的(假设你只想运行一个文件来输出JSON响应)。

<?php
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);
}
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);

基于您的评论的第二个解决方案:

<?php
if (empty($_GET['term']) || empty($_GET['term2'])) exit;

$q = strtolower($_GET["term"]);
$q2 = strtolower($_GET["term2"]);

if (get_magic_quotes_gpc()) $q = stripslashes($q);

$files = array();
$files2 = array();

foreach(glob('image/*.jpg*', GLOB_BRACE) as $key=>$file) {
    $files[] = substr($file, 0, -4);
}
foreach(glob('image/*.txt*', GLOB_BRACE) as $key=>$file) {
    $files2[] = substr($file, 0, -4);
}

$files = array_unique($files);
$files = array_combine($files, $files);
$files2 = array_unique($files2);
$files2 = array_combine($files2, $files2);

$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;
}
foreach ($files2 as $key=>$value) {
    if (strpos(strtolower($key), $q2) !== false) {
        array_push($result, array("id"=>$value, "label"=>$key, "value" => strip_tags($key)));
    }
    if (count($result) > 11) break;
}

echo json_encode($result);

答案 2 :(得分:1)

如果您不想更改上面的2个脚本,可以执行以下操作:

<?php

$script1_path = 'http://path/to/your/first/script.php';
$script2_path = 'http://path/to/your/second/script.php';

$json1 = file_get_contents($script1_path.'?term=hello');
$result1 = json_decode($json1);

$json2 = file_get_contents($script2_path.'?term=world');
$result2 = json_decode($json2);

$merged_result = array_merge($result1, $result2);
echo json_encode($merged_result);

?>

警告:根据服务器的配置,可能不允许file_get_content服务器加载http内容。

否则,最好的解决方案可能是更改您的体系结构以在一个php文件中生成2个数组:

$q1 = strtolower($_GET["term1"]);
...
$q2 = strtolower($_GET["term2"]);
...
echo json_encode($result);
相关问题