开发者

how is work with str_replace to array? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. 开发者_如何转开发 Closed 11 years ago.

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"]


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 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).

implode("/", $date);

As a bit of a gotcha -- implode will work based on key insertion order so you may want to use ksort first:

$a = array(1=>1, 0=>0); 
echo implode(",", $a); // outputs 1,0
ksort( $a );
echo implode(",", $a); // outputs 0,1


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


I am not sure, whether I understand you correctly, but this may be a solution:

echo implode('/', $date);

This will glue the elements of $date array with / into this string:

2011/7/11

Please see CodePad.org snippet for a proof.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜