Php 自动完成jQueryUI的MYSQL到JSON

Php 自动完成jQueryUI的MYSQL到JSON,php,javascript,jquery,jquery-ui,autocomplete,Php,Javascript,Jquery,Jquery Ui,Autocomplete,下面的代码显示了我在做什么。基本上,其目的是从数据库中检索数据,并将其转换为jQueryUI的JSON格式。使用下面的方法和一个不使用array-to-json函数的方法,我能够得到返回的json数据。如。: [{“姓名”:“库尔特·施耐德”},{“姓名”:“萨姆·崔”},{“姓名”:“克里斯蒂娜·格里米”}] 但问题是它仍然不能与自动完成一起工作。我用示例中提供的search.php替换了下面的代码,因此这显然是代码的问题 我从以下代码开始: <?php include 'con

下面的代码显示了我在做什么。基本上,其目的是从数据库中检索数据,并将其转换为jQueryUI的JSON格式。使用下面的方法和一个不使用array-to-json函数的方法,我能够得到返回的json数据。如。: [{“姓名”:“库尔特·施耐德”},{“姓名”:“萨姆·崔”},{“姓名”:“克里斯蒂娜·格里米”}]

但问题是它仍然不能与自动完成一起工作。我用示例中提供的search.php替换了下面的代码,因此这显然是代码的问题

我从以下代码开始:

<?php
    include 'connect.php';
    mysql_select_db("database", $con);
    $search = mysql_query("SELECT name FROM artist");
    $rows = array();
    while($row = mysql_fetch_assoc($search)) {
        $result[] = $row;
    }
print json_encode($result);
?>

其实际效果与以下代码相同(除了间距上的一些差异): (array_to_json函数直接从示例中复制)


您可以显示第一个代码的示例结果吗?让您的PHP返回一个简单的字符串数组:
[“Kurt Schneider”、“Sam Tsui”、“Christina Grimmie”]
以下是第一个代码的示例结果:[{“name”:“Kurt Schneider”},{“name”:“Sam Tsui”},{“name”:“Christina Grimmie”}]就字符串数组而言,我们是在谈论数组(0)=“dfgfdsg”等等,或者是你上面提到的形式?我不确定它会如何翻译成PHP…完全解决了。等我能发布答案后再说。谢谢:)
<?php
    include 'connect.php';
    mysql_select_db("database", $con);
    $search = mysql_query("SELECT name FROM artist");
    $rows = array();
    while($row = mysql_fetch_assoc($search)) {
        $result[] = $row;
    }

    function array_to_json( $array ){

    if( !is_array( $array ) ){
        return false;
    }

    $associative = count( array_diff( array_keys($array), array_keys( array_keys( $array )) ));
    if( $associative ){

        $construct = array();
        foreach( $array as $key => $value ){

            // We first copy each key/value pair into a staging array,
            // formatting each key and value properly as we go.

            // Format the key:
            if( is_numeric($key) ){
                $key = "key_$key";
            }
            $key = "\"".addslashes($key)."\"";

            // Format the value:
            if( is_array( $value )){
                $value = array_to_json( $value );
            } else if( !is_numeric( $value ) || is_string( $value ) ){
                $value = "\"".addslashes($value)."\"";
            }

            // Add to staging array:
            $construct[] = "$key: $value";
        }

        // Then we collapse the staging array into the JSON form:
        $result = "{ " . implode( ", ", $construct ) . " }";

    } else { // If the array is a vector (not associative):

        $construct = array();
        foreach( $array as $value ){

            // Format the value:
            if( is_array( $value )){
                $value = array_to_json( $value );
            } else if( !is_numeric( $value ) || is_string( $value ) ){
                $value = "'".addslashes($value)."'";
            }

            // Add to staging array:
            $construct[] = $value;
        }

        // Then we collapse the staging array into the JSON form:
        $result = "[ " . implode( ", ", $construct ) . " ]";
    }

    return $result;
}

    echo array_to_json($result);
?>
<?php
    include 'connect.php';
    mysql_select_db("database", $con);
    $search = mysql_query("SELECT name FROM artist");
    $rows = array();
    while($row = mysql_fetch_assoc($search)) {
        //don't add the array, just the name.
        $result[] = $row["name"];
    }
print json_encode($result);
?>
["Kurt Schneider", "Sam Tsui", "Christina Grimmie"]