[Solved] How to mearge two json files? [closed]


Comparing your two files, this should be what you looking to do (providing you are wanting to run just the one file to output the JSON response).

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

Second solution based on your comment:

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

5

solved How to mearge two json files? [closed]