简体   繁体   English

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

[英]Separate array into comma separate strings

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.这样做的目的是我可以将它传递给 API。 Like this像这样

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

The option UUidds can contain multiple values as long its separated by a comma in the end.选项 UUidds 可以包含多个值,只要最后用逗号分隔即可。

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. Implode 在每一行的末尾添加一个逗号,但它将这个逗号视为一个字符串。 I want to have multiple option UUids so i would need commas that are actually commas and not strings.我想有多个选项 UUids 所以我需要逗号实际上是逗号而不是字符串。

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

You should use你应该使用

'optionUuids' => array_values(
    $options2
),

This will give you all the values but with indices starting from zero这将为您提供所有值,但索引从零开始

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使用 JS 你可以做这样的事情

 // 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但这在 PHP 中没有意义(实际上它有意义,但这在这里对你没有帮助。)你需要的是一种像 JavaScript 中的扩展运算符

Unfortunately the spread operator in PHP doesn't work this way.不幸的是,PHP 中的展开运算符不能以这种方式工作。 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 @见https://secure.php.net/manual/en/functions.arguments.php#functions.variable-arg-list

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM