douying4203
2019-02-14 13:30
浏览 83
已采纳

将数组分隔为逗号分隔的字符串

I want to separate my array into separate strings that all end with a comma.

Array ( [0] => 233d3f9b-3e8e-4e16-ade2-6c165a0324c6 
        [2] => a6c736b0-f3d2-4907-9d36-6b31adeec2d1 
        [3] => 1e693cba-d0ce-4a24-bd75-b834e3f44272 
    )

The purpose of this is so i can pass it to an API. Like this

    'optionUuids' => array(
        $options2,
    ),

The option UUidds can contain multiple values as long its separated by a comma in the end.

I tried solving my problem using implode. Implode adds a comma at the end of each line, but it treats this comma as a string. I want to have multiple option UUids so i would need commas that are actually commas and not strings.

Im having trouble explaining this. This is what i expect:

    'optionUuids' => array(
      233d3f9b-3e8e-4e16-ade2-6c165a0324c6,
      a6c736b0-f3d2-4907-9d36-6b31adeec2d1,
      1e693cba-d0ce-4a24-bd75-b834e3f44272,
    ),
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doufu9836 2019-02-14 14:09
    已采纳

    You should use

    'optionUuids' => array_values(
        $options2
    ),
    

    This will give you all the values but with indices starting from zero

    点赞 评论
  • doujubeng2942 2019-02-14 13:54

    I think, what you want to achieve is not possible

    You want to change something like this
    ["A","B","C"]
    into something like this
    "A","B","C"

    With JS you could do things like this

    // JavaScript
    
    arr = [1,2,3]
    opt = Array(...arr)
    console.log(opt)

    But this has no meaning in PHP (in fact it has a meaning, but this would not help you here.) What you would need is a sort of a spread operator like in JavaScript

    Unfortunately the spread operator in PHP doesn't work this way. It knows the ... operator, but this is only used in function parameter declarations and function calls. @See https://secure.php.net/manual/en/functions.arguments.php#functions.variable-arg-list

    </div>
    
    点赞 评论

相关推荐 更多相似问题