如何使用 str_replace 来替换数组?
为什么 str_replace 不起作用?我该怎么办?
$date = $this->convert_date->JalaliToGregorian('1390','04','20'); ->> this output with json_encode -> [2011,7,11]
$da = str_replace(",","/",$date);
echo json_encode ($da) ->> output Array ["2011","7","11"]
why not worked str_replace? what do i do?
$date = $this->convert_date->JalaliToGregorian('1390','04','20'); ->> this output with json_encode -> [2011,7,11]
$da = str_replace(",","/",$date);
echo json_encode ($da) ->> output Array ["2011","7","11"]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
逗号不在数组中。这是由 json_encode 添加的。尝试
implode("/", $date);
这将使用/
作为粘合剂组合三个数组元素。内爆文档
The commas are not in the array. That's being added by json_encode. Try
implode("/", $date);
That will combine the three array elements using/
as glue.Implode Documentation
json_encode 返回一个表示对象的 JSON 表示形式的字符串。对于数组来说,这是一个用逗号包围的逗号分隔列表。如果你想让数组由其他东西来描述,那么你应该使用 <代码>内爆($glue,$pieces)。
作为一个陷阱 - implode 将根据键插入顺序工作,因此您可能需要首先使用 ksort:
json_encode returns a string which represents the JSON representation of an object. In the case of Arrays, that is a comma delineated list surrounded by commas. If you want to have the array be delineated by something else, then you should use
implode($glue,$pieces)
.As a bit of a gotcha -- implode will work based on key insertion order so you may want to use ksort first:
我不完全确定您期望结果是什么。
如果您希望脚本输出“2011/7/11”,那么您应该使用 implode() 而不是 str_replace (因为 $date 不是字符串,而是数组)。
所以
$da = implode('/', $date);
会给你那个结果
I'am not entirely sure what do you expect as a result.
If you want your script to output '2011/7/11', then you shoul use implode() instead of str_replace (since $date is not a string, but an array).
So
$da = implode('/', $date);
would give you that result
我不确定我是否理解正确,但这可能是一个解决方案:
这会将
$date
数组的元素与/
粘合到此字符串中:请参阅 CodePad.org snippet 获取证明。
I am not sure, whether I understand you correctly, but this may be a solution:
This will glue the elements of
$date
array with/
into this string:Please see CodePad.org snippet for a proof.