Loading JSON with PHP

I've been using PHP for too long, but in some places I'm new to JavaScript integration.

I am trying to find the fastest way to transfer database information to a page where it can be modified and dynamically displayed in JavaScript.

I am currently looking for loading JSON using PHP echo instructions because it is fast and efficient, but I saw that I could use the PHP JSON library (PHP 5.2).

Has anyone tried the new JSON library and is it better than my earlier method?

+5
source share
3 answers

. , 99%, - . , Unicode .

+15

json_encode json_decode . , , JSON.

, UTF-8!

+4

The library did a great job for me. FWIW I needed to do this in a project with an earlier version of PHP without JSON support. The function below worked as the provided risky version of "json_encode" for string arrays.

function my_json_encode($row) {
    $json = "{";
        $keys = array_keys($row);
        $i=1;
        foreach ($keys as $key) {
            if ($i>1) $json .= ',';
            $json .= '"'.addslashes($key).'":"'.addslashes($row[$key]).'"';
            $i++;
        }
    $json .= "}";
    return $json;
}
+2
source

All Articles