简体   繁体   中英

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

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

 // 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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