force json_encode to create strings

后端 未结 4 1819
感动是毒
感动是毒 2020-12-21 14:51

I have to json_encode a PHP array to a JavaScript array. Unfortunately the jQuery library I am using will not properly process that array if it contains ints instead of stri

4条回答
  •  盖世英雄少女心
    2020-12-21 15:16

    Because there isn't an opposite flag for JSON_NUMERIC_CHECK, I've created a function for that purpose.
    It accepts one and multi dimensional arrays and there can be added for conditions to validate each element of te array.

    function JSON_NUMERIC_STRING($array){
        foreach($array as $key => &$value){
            if(is_array($value)){
                $value = iterateMA($value);
            }elseif(is_numeric($value)){
                $value = strval($value);
            }
            // add more conditions if needed...
        }
        return $array;
    }
    
    $array = JSON_NUMERIC_STRING($array);
    

提交回复
热议问题