jjzjj

force_encoding

全部标签

php - 强制 PHP json_encode() 将索引编码为字符串

我有一个数组设置如下:$myArray=array();$myArray[]="Newarrayitem1";$myArray[]="Newarrayitem2";$myArray[]="Newarrayitem3";当我在其上运行json_encode()时,它会输出以下内容:["Newarrayitem1","Newarrayitem2","Newarrayitem3"]我想要的是将索引编码为字符串的函数:{"0":"Newarrayitem1","1":"Newarrayitem2","2":"Newarrayitem3"}以便稍后我可以删除第一项而不影响第二项的索引。有没有简单

php - 格式 echo json_encode

这个问题在这里已经有了答案:Pretty-PrintingJSONwithPHP(27个答案)关闭9年前。我想格式化echojson_encode,目前输出是{"results":{"course":"CC140","books":{"book":[[{"id":"300862","title":"Buildingobject-orientedsoftware","isbn":"0070431965","borrowedcount":"6"}]]}}}而我想这样输出:{"results":{"course":"CC140","books":{"book":[[{"id":"300862

php - 如何在 php 上使用 base64_encode 字节数组?

如何在php上对字节数组进行base64_encode?在Java上newBASE64Encoder().encode(byte[]bytes); 最佳答案 使用base64_encode($stringToEncode)和base64_decode($stringToDecode)例子:$string='TestString';$coded=base64_encode($string);echo$coded;//TyByYXRvIHJldSBhIHJvcGEgZG8gcmVpIGRlIFJvbWE=$original=base64

php - 何时使用 PHP 的 json_encode 函数的第二个参数(位掩码)

PHP的json_encode函数作为第二个可选参数(位掩码)。有人可以向我解释它们的用途、我应该在什么时候使用它们以及为什么要使用它们吗?谢谢 最佳答案 目的是去除JSON输出中的特殊字符。在某些情况下,特殊字符可能有其他含义,您只想传递JSON而无需使用任何这些。假设您要在一些XML中发送json。您不希望其中的任何 关于php-何时使用PHP的json_encode函数的第二个参数(位掩码),我们在StackOverflow上找到一个类似的问题: htt

php - mb_convert_encoding() 不适用于 phpunit

出于某种原因,在phpunit中运行mb_convert_encoding时,我得到了意想不到的结果。例如执行以下操作:var_dump(mb_convert_encoding(utf8_decode('ö'),'UTF-8')==='ö')上面在PHP-FPM和PHP-CLI下返回bool(true),但是在PHPunit下返回false,mb_convert_encoding()正在做一些事情,它只是编码到一个乱七八糟的字符串。 最佳答案 我猜你正在使用一组不同的mbstringini设置。这是解决该问题的一种方法。首先,您可以

php - 为什么将空白字符串的 json_encode 添加到数组中?

我在PHP5.6上遇到过这种行为(在PHP5.4到7.0中也是相同的)。$note=newSimpleXMLElement('');$note->addChild("string0",'justastring');$note->addChild("string1","abc\n\n\n");$note->addChild("string2","\tdef");$note->addChild("string3","\n\n\n");$note->addChild("string4","\t\n");$json=json_encode($note,JSON_PRETTY_PRINT);pr

php - Zend 框架 : Apache decoding encoded URL instead of passing encoded URL?

我正在使用Selenium和PHPUnit测试我的ZendFramework应用程序。我有一个测试需要打开一个包含编码URL的URL。$redirectToLocation=urlencode('/myothercontroller/action');//%2Fmyothercontroller%2Faction$this->openAndWait('/controller/action/thenRedirectTo/'.$redirectToLocation);但是当我运行测试时,浏览器尝试打开解码后的URL:/controller/action/thenRedirectTo//my

php - json_encode 返回未定义

我的脚本从我的json_encodephp返回未定义的值索引.php示例.htmlfunctionclickHere(){$.get("index.php",function(data){alert(data.user);});}我该如何解决这个问题? 最佳答案 使用jQuery.getJSON方法而不是.get,如果你希望你的JSON被解析。此外,请确保正确加载了jQuery库。functionclickHere(){$.getJSON("index.php",function(data){alert(data.user);});

php - json_encode 添加大量十进制数字

为什么会这样?我可以防止这种情况吗?(除了将它们作为字符串传递)var_dump(json_encode([1002.31,2002.42]));输出:string(39)"[1002.3099999999999,2002.4200000000001]" 最佳答案 您应该配置“precision”'和'serialize_precision'参数。precision=14serialize_precision=-1测试用例:php-r'var_dump(json_encode([1002.31,2002.42]));'string(

php - 将 json_encode 应用于类时如何忽略特定值

有没有办法在编码为json时忽略php中类的特定类属性。例如,在带有jackson库的java中,我可以使用@JsonIgnore注释全局变量来实现这一点。在php中有什么可比的(最好是native的)吗?? 最佳答案 一种方法是利用JsonSerializableinterface.这使您可以创建一个函数,当在您的类上调用json_encode()时调用该函数。例如:classMyClassimplementsJsonSerializable{public$var1,$var2;function__construct($a1,$a