Hướng dẫn array to string php

Home » Code

Để chuyển một Mảng sang Chuỗi bằng PHP [array to string PHP], chúng ta có thể sử dụng hai hàm tích hợp sẵn khác nhau có sẵn trong PHP. Hai hàm này chỉ nhận một mảng tại một thời điểm để chuyển nó thành một chuỗi.
– Sử dụng hàm json_encode[]
– Sử dụng hàm implode[]

Cách 1: Sử dụng json_encode[]
Để chuyển đổi một mảng thành một chuỗi, một trong những cách phổ biến để làm điều đó là sử dụng hàm json_encode[] được sử dụng để trả về biểu diễn JSON của một giá trị.
Ví dụ:

Xem thêm.

  QABug chúc các bạn thực hiện thành công.

[PHP 4, PHP 5, PHP 7, PHP 8]

implodeJoin array elements with a string

Description

implode[string $separator, array $array]: string

Alternative signature [not supported with named arguments]:

implode[array $array]: string

Legacy signature [deprecated as of PHP 7.4.0, removed as of PHP 8.0.0]:

implode[array $array, string $separator]: string

Parameters

separator

Optional. Defaults to an empty string.

array

The array of strings to implode.

Return Values

Returns a string containing a string representation of all the array elements in the same order, with the separator string between each element.

Changelog

VersionDescription
8.0.0 Passing the separator after the array is no longer supported.
7.4.0 Passing the separator after the array [i.e. using the legacy signature] has been deprecated.

Examples

Example #1 implode[] example

Notes

Note: This function is binary-safe.

See Also

  • explode[] - Split a string by a string
  • preg_split[] - Split string by a regular expression
  • http_build_query[] - Generate URL-encoded query string

houston_roadrunner at yahoo dot com

13 years ago

it should be noted that an array with one or no elements works fine. for example:

Chủ Đề