Có thể trùng lặp: Làm thế nào để tạo danh sách phân tách dấu phẩy từ mảng trong PHP?
How to create comma separated list from array in PHP?
Mảng của tôi trông như thế này:
Array
[
[0] => lorem
[1] => ipsum
[2] => dolor
[3] => sit
[4] => amet
]
Làm thế nào để chuyển đổi điều này thành một chuỗi như thế này với PHP?
$string = 'lorem, ipsum, dolor, sit, amet';
Đã hỏi ngày 27 tháng 3 năm 2012 lúc 18:33Mar 27, 2012 at 18:33
0
$arr = array [ 0 => "lorem", 1 => "ipsum", 2 => "dolor"];
$str = implode [", ", $arr];
Josh
8.0245 Huy hiệu vàng42 Huy hiệu bạc40 Huy hiệu đồng5 gold badges42 silver badges40 bronze badges
Đã trả lời ngày 27 tháng 3 năm 2012 lúc 18:35Mar 27, 2012 at 18:35
Omabenaomabenaomabena
Phù hiệu vàng 3.4811 Huy hiệu bạc13 Huy hiệu đồng1 gold badge16 silver badges13 bronze badges
2
Trực tiếp từ các tài liệu:
$comma_separated = implode[",", $array];
Đã trả lời ngày 27 tháng 3 năm 2012 lúc 18:35Mar 27, 2012 at 18:35
OmabenaomabenaNobita
Phù hiệu vàng 3.4811 Huy hiệu bạc13 Huy hiệu đồng10 gold badges56 silver badges85 bronze badges
2
Trực tiếp từ các tài liệu:
$array = array['lastname', 'email', 'phone'];
$comma_separated = implode[",", $array];
echo $comma_separated; // lastname,email,phone
//php.net/manual/en/function.implode.php
Josh
8.0245 Huy hiệu vàng42 Huy hiệu bạc40 Huy hiệu đồng5 gold badges42 silver badges40 bronze badges
Đã trả lời ngày 27 tháng 3 năm 2012 lúc 18:35Mar 27, 2012 at 18:34
OmabenaomabenaAdam
Phù hiệu vàng 3.4811 Huy hiệu bạc13 Huy hiệu đồng6 gold badges30 silver badges51 bronze badges
Trực tiếp từ các tài liệu:
$string = implode[",", $array];
Đã trả lời ngày 27 tháng 3 năm 2012 lúc 18:35Mar 27, 2012 at 18:34
OmabenaomabenaJKirchartz
Phù hiệu vàng 3.4811 Huy hiệu bạc13 Huy hiệu đồng7 gold badges59 silver badges87 bronze badges
Cải thiện bài viết
Lưu bài viết
Danh sách phân tách bằng dấu phẩy có thể được tạo bằng cách sử dụng hàm Implode []. Ilifrode [] là một hàm tích hợp trong PHP và được sử dụng để tham gia các phần tử của một mảng. Imptrode [] là bí danh cho PHP | Chức năng tham gia [] và hoạt động giống hệt như hàm tham gia []. Nếu chúng ta có một mảng các phần tử, chúng ta có thể sử dụng hàm augmode [] để tham gia tất cả chúng để tạo một chuỗi. Về cơ bản, chúng tôi tham gia các phần tử mảng với một chuỗi. Giống như hàm nối [], hàm giả định [] cũng trả về một chuỗi được hình thành từ các phần tử của một mảng.implode[] function. The implode[] is a builtin function in PHP and is used to join the elements of an array. implode[] is an alias for PHP | join[] function and works exactly same as that of join[] function.
If we have an array of
elements, we can use the implode[] function to join them all to form one string. We basically join array elements with a string. Just like join[] function , implode[] function also returns a string formed from the elements of an array.
Syntax:
string implode[ separator, array ]
Loại trả về: Loại trả về của hàm Implofrode [] là chuỗi. Nó sẽ trả về chuỗi được nối được hình thành từ các yếu tố của mảng. The return type of implode[] function is string. It will return the joined string formed from the elements of array.
Ví dụ 1: Ví dụ này thêm dấu phân cách dấu phẩy vào các phần tử mảng. This example adds comma separator to the array elements.
$string = 'lorem, ipsum, dolor, sit, amet';
0Is
$arr = array [ 0 => "lorem", 1 => "ipsum", 2 => "dolor"];
$str = implode [", ", $arr];
1$string = 'lorem, ipsum, dolor, sit, amet';
1$arr = array [ 0 => "lorem", 1 => "ipsum", 2 => "dolor"];
$str = implode [", ", $arr];
0$arr = array [ 0 => "lorem", 1 => "ipsum", 2 => "dolor"];
$str = implode [", ", $arr];
4 $arr = array [ 0 => "lorem", 1 => "ipsum", 2 => "dolor"];
$str = implode [", ", $arr];
5$arr = array [ 0 => "lorem", 1 => "ipsum", 2 => "dolor"];
$str = implode [", ", $arr];
6$string = 'lorem, ipsum, dolor, sit, amet';
6$string = 'lorem, ipsum, dolor, sit, amet';
1$arr = array [ 0 => "lorem", 1 => "ipsum", 2 => "dolor"];
$str = implode [", ", $arr];
1$arr = array [ 0 => "lorem", 1 => "ipsum", 2 => "dolor"];
$str = implode [", ", $arr];
4$arr = array [ 0 => "lorem", 1 => "ipsum", 2 => "dolor"];
$str = implode [", ", $arr];
0$comma_separated = implode[",", $array];
3Output:
Array [ [0] => GFG1 [1] => GFG2 [2] => GFG3 ] GFG1, GFG2, GFG3
Ví dụ 2:
$string = 'lorem, ipsum, dolor, sit, amet';
0$string = 'lorem, ipsum, dolor, sit, amet';
1 $string = 'lorem, ipsum, dolor, sit, amet';
2$string = 'lorem, ipsum, dolor, sit, amet';
3$comma_separated = implode[",", $array];
8$arr = array [ 0 => "lorem", 1 => "ipsum", 2 => "dolor"];
$str = implode [", ", $arr];
1$string = 'lorem, ipsum, dolor, sit, amet';
1$arr = array [ 0 => "lorem", 1 => "ipsum", 2 => "dolor"];
$str = implode [", ", $arr];
0$arr = array [ 0 => "lorem", 1 => "ipsum", 2 => "dolor"];
$str = implode [", ", $arr];
4 $arr = array [ 0 => "lorem", 1 => "ipsum", 2 => "dolor"];
$str = implode [", ", $arr];
5$arr = array [ 0 => "lorem", 1 => "ipsum", 2 => "dolor"];
$str = implode [", ", $arr];
6$string = 'lorem, ipsum, dolor, sit, amet';
6$string = 'lorem, ipsum, dolor, sit, amet';
1$arr = array [ 0 => "lorem", 1 => "ipsum", 2 => "dolor"];
$str = implode [", ", $arr];
1$arr = array [ 0 => "lorem", 1 => "ipsum", 2 => "dolor"];
$str = implode [", ", $arr];
4$arr = array [ 0 => "lorem", 1 => "ipsum", 2 => "dolor"];
$str = implode [", ", $arr];
0$comma_separated = implode[",", $array];
3Output:
Array [ [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 ] 0, 1, 2, 3, 4, 5, 6, 7
Ví dụ 2: