how is work with str_replace to array? [closed]
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.
精彩评论