获取AJAX中foreach数组的第二个变量


Get second variable of a foreach array in AJAX

为了澄清这个问题,我提出了一个新的话题。

我使用"http://www.devbridge.com/sourcery/components/jquery-autocomplete/"的自动补全,但在以前的版本中,因为它的轻量级和速度。我使用的版本可以在这里找到:https://code.google.com/p/jquery-autocomplete/source/browse/

在其中,我尝试使用

获得结果
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
$(function() {
$("#ac1").autocomplete('search.php', {
selectFirst: true
});
$("#flush").click(function() {
var ac = $("#ac1").data('autocompleter');
if (ac && $.isFunction(ac.cacheFlush)) {
    ac.cacheFlush();
} else {
    alert('Error flushing cache');
}
});

data.php结构简单:

$data = array(
"Berlin" => "10178",
"Hamburg" => "20038",
"München" => "80331",

和search.php文件包含以下内容:

<?php
include 'data.php';
function autocomplete_format($results) {
foreach ($results as $result) {
echo $result[0] . '|' . $result[1] . "'n";
}
}

if (isset($_GET['q'])) {
$q = strtolower($_GET['q']);
if ($q) {
foreach ($data as $key => $value) {
    if (strpos(strtolower($key), $q) !== false) {
        $results[] = array($key, $value);
    }
}
}
}
$output = 'autocomplete';
if (isset($_GET['output'])) {
$output = strtolower($_GET['output']);
}
if ($output === 'json') {
echo json_encode($results);
} else {
echo autocomplete_format($results);
}

现在我试着,当我从下拉菜单中选择了正确的城市后,在一个新的标签中,城市旁边的数字(在data.php中)。

试试这个:

echo $data['Berlin'];