728x90
반응형

implode 함수

implode 함수는 각 배열 요소들을 문자열로 만들어주는 함수다.
다음과 같이 사용할 수 있다.

implode(string $separator, array $array): string

implode(array $array): string

implode(array $array, string $separator): string

- php manual
https://www.php.net/manual/en/function.implode.php

 

PHP: implode - Manual

Sometimes it's necessary to add a string not just between the items, but before or after too, and proper handling of zero items is also needed.In this case, simply prepending/appending the separator next to implode() is not enough, so I made this little he

www.php.net

 

직접 사용해보면서 어떻게 결과가 나오는지 확인해보자.

1. 배열에 있는 값을 하나의 문자열로 합치기

다음와 같이 사용하게 되면 배열에 있는 모든 문자가 합쳐진다.

<?php          
    $test = implode(['a', 'b', 'c']);
	
	print $test;

 

2. 배열에 있는 값들을 구분해서 가져오기

배열에 있는 값들을 구분자를 통해서 가져올 때 사용하는데
각 배열의 값들을 콤마(,) 를 통해서 구분해서 하나의 문자열로 만들어보자.

<?php
    $array = ['id', 'email', 'phone', 'passwd', 'createdAt'];
    $test = implode(", ", $array);

    print $test;

결과를 확인해보면 콤마(,) 로 연결되어 출력되는 것을 확인할 수 있다.

 

- 테스트 사이트

https://ideone.com/

 

Ideone.com

Ideone is something more than a pastebin; it's an online compiler and debugging tool which allows to compile and run code online in more than 40 programming languages.

ideone.com

 

728x90
반응형

'프로그래밍 언어 > PHP' 카테고리의 다른 글

[php] print_r(), var_export() 함수  (0) 2022.07.11
[php] foreach  (0) 2022.07.11
복사했습니다!