为什么我不能将我的数组作为json回显?

时间:2016-08-07 06:21:24

标签: php json ajax

我的javascript模型调用了这个函数,使用ajax并希望得到一个json数组

function languages($host, $dbUsername, $dbPassword, $dbName) {
    $mysqli = new mysqli($host, $dbUsername, $dbPassword, $dbName);
    $languages = [];

    $languagesQ = "SELECT id, language FROM languages ORDER BY language";

    if ($stmt = $mysqli->prepare($languagesQ)) {
        $stmt->execute();
        $stmt->store_result();
        $stmt->bind_result($id, $language);

        while ($stmt->fetch()) {
            $languages[] = array(
                'id' => $id,
                'language' => $language
            );
        }

        $stmt->free_result();
        $stmt->close();
    }
    $mysqli->close();

    print_r($languages);

    echo json_encode($languages);
}

问题是行echo json_encode($languages);输出为null,所以我尝试使用print_r($languages);打印数组,它根本不是空的。它看起来像这样:

Array
(
    [0] => Array
        (
            [id] => 1
            [language] => Afrikaans
        )

    [1] => Array
        (
            [id] => 2
            [language] => Albanian
        )

    [2] => Array
        (
            [id] => 3
            [language] => Arabic
        )

    [3] => Array
        (
            [id] => 4
            [language] => Armenian
        )

    [4] => Array
        (
            [id] => 5
            [language] => Basque
        )

    [5] => Array
        (
            [id] => 6
            [language] => Bengali
        )

    [6] => Array
        (
            [id] => 7
            [language] => Bulgarian
        )

    [7] => Array
        (
            [id] => 9
            [language] => Cambodian
        )

    [8] => Array
        (
            [id] => 8
            [language] => Catalan
        )

    [9] => Array
        (
            [id] => 10
            [language] => Chinese (Mandarin)
        )

    [10] => Array
        (
            [id] => 11
            [language] => Croatian
        )

    [11] => Array
        (
            [id] => 12
            [language] => Czech
        )

    [12] => Array
        (
            [id] => 13
            [language] => Danish
        )

    [13] => Array
        (
            [id] => 14
            [language] => Dutch
        )

    [14] => Array
        (
            [id] => 15
            [language] => English
        )

    [15] => Array
        (
            [id] => 16
            [language] => Estonian
        )

    [16] => Array
        (
            [id] => 17
            [language] => Fiji
        )

    [17] => Array
        (
            [id] => 18
            [language] => Finnish
        )

    [18] => Array
        (
            [id] => 19
            [language] => French
        )

    [19] => Array
        (
            [id] => 20
            [language] => Georgian
        )

    [20] => Array
        (
            [id] => 21
            [language] => German
        )

    [21] => Array
        (
            [id] => 22
            [language] => Greek
        )

    [22] => Array
        (
            [id] => 23
            [language] => Gujarati
        )

    [23] => Array
        (
            [id] => 24
            [language] => Hebrew
        )

    [24] => Array
        (
            [id] => 25
            [language] => Hindi
        )

    [25] => Array
        (
            [id] => 26
            [language] => Hungarian
        )

    [26] => Array
        (
            [id] => 27
            [language] => Icelandic
        )

    [27] => Array
        (
            [id] => 28
            [language] => Indonesian
        )

    [28] => Array
        (
            [id] => 29
            [language] => Irish
        )

    [29] => Array
        (
            [id] => 30
            [language] => Italian
        )

    [30] => Array
        (
            [id] => 31
            [language] => Japanese
        )

    [31] => Array
        (
            [id] => 32
            [language] => Javanese
        )

    [32] => Array
        (
            [id] => 33
            [language] => Korean
        )

    [33] => Array
        (
            [id] => 34
            [language] => Latin
        )

    [34] => Array
        (
            [id] => 35
            [language] => Latvian
        )

    [35] => Array
        (
            [id] => 36
            [language] => Lithuanian
        )

    [36] => Array
        (
            [id] => 37
            [language] => Macedonian
        )

    [37] => Array
        (
            [id] => 38
            [language] => Malay
        )

    [38] => Array
        (
            [id] => 39
            [language] => Malayalam
        )

    [39] => Array
        (
            [id] => 40
            [language] => Maltese
        )

    [40] => Array
        (
            [id] => 41
            [language] => Maori
        )

    [41] => Array
        (
            [id] => 42
            [language] => Marathi
        )

    [42] => Array
        (
            [id] => 43
            [language] => Mongolian
        )

    [43] => Array
        (
            [id] => 44
            [language] => Nepali
        )

    [44] => Array
        (
            [id] => 45
            [language] => Norwegian
        )

    [45] => Array
        (
            [id] => 46
            [language] => Persian
        )

    [46] => Array
        (
            [id] => 47
            [language] => Polish
        )

    [47] => Array
        (
            [id] => 48
            [language] => Portuguese
        )

    [48] => Array
        (
            [id] => 49
            [language] => Punjabi
        )

    [49] => Array
        (
            [id] => 50
            [language] => Quechua
        )

    [50] => Array
        (
            [id] => 51
            [language] => Romanian
        )

    [51] => Array
        (
            [id] => 52
            [language] => Russian
        )

    [52] => Array
        (
            [id] => 53
            [language] => Samoan
        )

    [53] => Array
        (
            [id] => 54
            [language] => Serbian
        )

    [54] => Array
        (
            [id] => 55
            [language] => Slovak
        )

    [55] => Array
        (
            [id] => 56
            [language] => Slovenian
        )

    [56] => Array
        (
            [id] => 57
            [language] => Spanish
        )

    [57] => Array
        (
            [id] => 58
            [language] => Swahili
        )

    [58] => Array
        (
            [id] => 59
            [language] => Swedish�
        )

    [59] => Array
        (
            [id] => 60
            [language] => Tamil
        )

    [60] => Array
        (
            [id] => 61
            [language] => Tatar
        )

    [61] => Array
        (
            [id] => 62
            [language] => Telugu
        )

    [62] => Array
        (
            [id] => 63
            [language] => Thai
        )

    [63] => Array
        (
            [id] => 64
            [language] => Tibetan
        )

    [64] => Array
        (
            [id] => 65
            [language] => Tonga
        )

    [65] => Array
        (
            [id] => 66
            [language] => Turkish
        )

    [66] => Array
        (
            [id] => 67
            [language] => Ukrainian
        )

    [67] => Array
        (
            [id] => 68
            [language] => Urdu
        )

    [68] => Array
        (
            [id] => 69
            [language] => Uzbek
        )

    [69] => Array
        (
            [id] => 70
            [language] => Vietnamese
        )

    [70] => Array
        (
            [id] => 71
            [language] => Welsh
        )

    [71] => Array
        (
            [id] => 72
            [language] => Xhosa
        )

)

我的问题是为什么json_encode()根本不起作用?我做错了吗?

你能帮我推吗?

1 个答案:

答案 0 :(得分:2)

查看第58个数组语言的内容。 “瑞典 ”在这里你看到它包含一个特殊的角色。 Json_encode()函数在此阶段终止。 要解决此问题,只需使用'language'=>代码中的utf8_encode($ language)。 我想它会起作用。