How to convert an array to a string in PHP?

前端 未结 8 911
[愿得一人]
[愿得一人] 2021-02-01 14:42

For an array like the one below; what would be the best way to get the array values and store them as a comma-separated string?

Array ( [0] => 33160,
                 


        
相关标签:
8条回答
  • Using implode(), you can turn the array into a string.

    $str = implode(',', $array); // 33160,33280,33180,...
    
    0 讨论(0)
  • 2021-02-01 14:48

    I would turn it into a json object, with the added benefit of keeping the keys if you are using an associative array:

     $stringRepresentation= json_encode($arr);
    
    0 讨论(0)
  • 2021-02-01 14:48

    PHP's implode function can be used to convert an array into a string --- it is similar to join in other languages.

    You can use it like so:

    $string_product = implode(',', $array);
    

    With an array like [1, 2, 3], this would result in a string like "1,2,3".

    0 讨论(0)
  • 2021-02-01 14:48

    implode?

    0 讨论(0)
  • 2021-02-01 14:53

    I would turn it into CSV form, like so:

    $string_version = implode(',', $original_array)
    

    You can turn it back by doing:

    $destination_array = explode(',', $string_version)
    
    0 讨论(0)
  • 2021-02-01 15:02

    serialize() and unserialize() convert between php objects and a string representation.

    0 讨论(0)
提交回复
热议问题