doude4924
2011-07-18 20:16 阅读 43

str_replace如何使用数组? [关闭]

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"]
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • 已采纳
    dongzhi5386 dongzhi5386 2011-07-18 20:20

    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

    点赞 评论 复制链接分享
  • douyi6290 douyi6290 2011-07-18 20:21

    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
    
    点赞 评论 复制链接分享
  • dongping8572 dongping8572 2011-07-18 20:23

    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

    点赞 评论 复制链接分享
  • doutan5798 doutan5798 2011-07-18 20:23

    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.

    点赞 评论 复制链接分享

相关推荐