Conversion of jq array to string

function encode($array)
{
        if(version_compare(PHP_VERSION,"5.4.0","<")){
        $str = json_encode($array);
        $str = preg_replace_callback("-sharp\\\u([0-9a-f]{4})-sharpi",function($matchs){
           return iconv("UCS-2BE", "UTF-8", pack("H4", $matchs[1]));
        },$str);
        return $str;
        }else{
        return json_encode($array, JSON_UNESCAPED_UNICODE);
        }
}

this is a function for converting Chinese;
clipboard.png
I check out the data from the database and convert it to json format.
this is js; in html

 function test(){
        var id = $(".father option:selected").val();
        $.ajax({
            url: "regulation_type.php",
            type: "post",
            data: {id:id , act:"ajax"},
            success:function(msg){
                //var str = "{"id":"2","title":"","parent_id":"1","chapter":""}";
                //obj = JSON.parse(str);
                console.log(msg);
                str = msg.replace("[{","{");
                str_1 = msg.replace("}]","}");
                console.log(str_1);

output msg, directly to get [{"id": "2", "title": "Criminal Law Task", "parent_id": "1", "chapter": "Chapter I"}] , with a [] , I thought it was an array. Using typeof , I looked at the string, obj = JSON.parse (json);. output obj.id has no value, and then write {"id": "2", "title": "Criminal Law Task", "parent_id": "1", "chapter": "Chapter I"} ; Use obj = JSON.parse (json); to output obj.id , and then want to get rid of the outside [] , but you can"t get rid of it. str = msg.replace ("[{","{"); str_1 = msg.replace ("}],"}"); the one on the right can be removed, but the one on the left can"t be removed, is there any big god who knows the reason, solve it, online, etc.!

Apr.07,2021

then you can just take the returned array and return multiple items to delete [] then you will not be able to parse correctly


because you use getAll () to get multiple pieces of data, obj is a 2-dimensional array, so use a loop

.
obj = JSON.parse(str);
for(var o in obj)
{
    console.log(obj[o].id);
}

if you only want the first piece of data:
method 1:
do not use getAll () method
method 2:

echo encode($res[0]);

method 3:

obj = JSON.parse(str);
obj = obj[0];
console.log(obj.id);

method 1 is better

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e49ad2-44d2f.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e49ad2-44d2f.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?