라이브러리
[PHP] array_combine - 키에 대한 하나의 배열과 값에 대한 다른 배열을 사용하여 배열을 생성합니다.
PHP Array Combine
PHP의 `array_combine` 함수는 두 개의 배열을 하나의 배열로 결합하는 데 사용됩니다. 이 함수는 첫 번째 배열의 각 키에 두 번째 배열의 해당 인덱스 값을 할당합니다.
Syntax
`array_combine(array $keys, array $values)`
Parameters
* `$keys`: 첫 번째 배열
* `$values`: 두 번째 배열
Return Value
결합된 배열
예제
#hostingforum.kr
php
// 두 개의 배열
$fruits = ['apple', 'banana', 'cherry'];
$colors = ['red', 'yellow', 'pink'];
// array_combine 함수 사용
$fruitColors = array_combine($fruits, $colors);
// 결과 출력
print_r($fruitColors);
위의 예제에서 `$fruitColors` 배열의 키는 `$fruits` 배열의 키가 되고, 값은 `$colors` 배열의 인덱스 값이 됩니다.
#hostingforum.kr
php
Array
(
[apple] => red
[banana] => yellow
[cherry] => pink
)
예제 2: 중복 키 처리
#hostingforum.kr
php
// 두 개의 배열
$fruits = ['apple', 'banana', 'apple'];
$colors = ['red', 'yellow', 'green'];
// array_combine 함수 사용
$fruitColors = array_combine($fruits, $colors);
// 결과 출력
print_r($fruitColors);
위의 예제에서 `$fruitColors` 배열의 키는 `$fruits` 배열의 키가 되지만, 중복 키가 있는 경우 마지막 키의 값만 유지됩니다.
#hostingforum.kr
php
Array
(
[apple] => green
[banana] => yellow
)
예제 3: NULL 키 처리
#hostingforum.kr
php
// 두 개의 배열
$fruits = ['apple', 'banana', NULL];
$colors = ['red', 'yellow', 'green'];
// array_combine 함수 사용
$fruitColors = array_combine($fruits, $colors);
// 결과 출력
print_r($fruitColors);
위의 예제에서 `$fruitColors` 배열의 키는 `$fruits` 배열의 키가 되지만, NULL 키는 제거됩니다.
#hostingforum.kr
php
Array
(
[apple] => red
[banana] => yellow
)
결론
PHP의 `array_combine` 함수는 두 개의 배열을 하나의 배열로 결합하는 데 사용됩니다. 이 함수는 첫 번째 배열의 각 키에 두 번째 배열의 해당 인덱스 값을 할당합니다. 중복 키는 마지막 키의 값만 유지되고, NULL 키는 제거됩니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.