✅ 1. PHP array_fill 함수 설명
array_fill 함수는 특정 인덱스부터 일정 개수만큼 동일한 값을 가진 배열을 생성하는 함수입니다.
✔️ 문법
array_fill(int $start_index, int $count, mixed $value): array
✔️ 파라미터
- $start_index : 배열의 시작 인덱스
- $count : 생성할 요소 개수
- $value : 채울 값
✔️ 반환값
- 동일한 값으로 채워진 새 배열을 반환합니다.
✅ 2. PHP array_fill 샘플 코드 생성
<?php
$arr = array_fill(2, 4, "Hello");
print_r($arr);
?>
✅ 3. 샘플 코드 이용 방법 및 Output
✔️ 설명
- 인덱스 2부터 시작하여 4개의 요소를 "Hello"로 채운 배열을 생성합니다.
✔️ Output
Array ( [2] => Hello [3] => Hello [4] => Hello [5] => Hello )
✅ 4. VB.NET에서 동일/유사 기능 함수 생성 + 설명/샘플/출력
VB.NET에는 array_fill과 동일한 내장 함수는 없으므로 사용자 정의 함수로 구현합니다.
✔️ (1) VB.NET 함수 설명
ArrayFill 함수는 특정 개수만큼 동일한 값으로 채워진 배열을 생성합니다.
✔️ (2) VB.NET 샘플 코드
Module Module1
Function ArrayFill(startIndex As Integer, count As Integer, value As Object) As Dictionary(Of Integer, Object)
Dim result As New Dictionary(Of Integer, Object)
For i As Integer = 0 To count - 1
result(startIndex + i) = value
Next
Return result
End Function
Sub Main()
Dim arr = ArrayFill(2, 4, "Hello")
For Each item In arr
Console.WriteLine($"{item.Key} => {item.Value}")
Next
End Sub
End Module
✔️ (3) VB.NET Output
2 => Hello 3 => Hello 4 => Hello 5 => Hello
✅ 5. C#에서 동일/유사 기능 함수 생성 + 설명/샘플/출력
C# 역시 PHP의 array_fill과 동일한 내장 함수가 없어 커스텀 함수로 구현합니다.
✔️ (1) C# 함수 설명
ArrayFill 메서드는 특정 인덱스부터 지정된 개수만큼 동일한 값으로 채우는 컬렉션을 생성합니다.
✔️ (2) C# 샘플 코드
using System;
using System.Collections.Generic;
class Program
{
static Dictionary<int, object> ArrayFill(int startIndex, int count, object value)
{
var result = new Dictionary<int, object>();
for (int i = 0; i < count; i++)
{
result[startIndex + i] = value;
}
return result;
}
static void Main()
{
var arr = ArrayFill(2, 4, "Hello");
foreach (var item in arr)
{
Console.WriteLine($"{item.Key} => {item.Value}");
}
}
}
✔️ (3) C# Output
2 => Hello 3 => Hello 4 => Hello 5 => Hello
✅ 6. Python에서 동일/유사 기능 함수 + 설명/샘플/출력
Python에서는 리스트 곱셈 연산으로 유사 기능을 쉽게 구현할 수 있습니다.
✔️ (1) Python 함수 설명
Python 리스트는 "Hello" * n이 문자열 반복이 아닌,
[value] * n을 통해 n개의 동일한 요소로 구성된 리스트를 생성할 수 있습니다.
하지만 PHP처럼 시작 인덱스를 포함한 구조는 dict로 구현합니다.
✔️ (2) Python 샘플 코드
def array_fill(start_index, count, value):
return {start_index + i: value for i in range(count)}
arr = array_fill(2, 4, "Hello")
for k, v in arr.items():
print(f"{k} => {v}")
✔️ (3) Python Output
2 => Hello 3 => Hello 4 => Hello 5 => Hello
✅ 7. JavaScript에서 동일/유사 기능 함수 + 설명/샘플/출력
JavaScript에는 Array.fill()이 존재하며 PHP의 array_fill과 가장 유사합니다.
✔️ (1) JavaScript 함수 설명
JS 배열에서는 다음처럼 사용합니다:
Array(count).fill(value)
PHP처럼 시작 인덱스를 지정하려면 객체(Object)로 구현합니다.
✔️ (2) JavaScript 샘플 코드
function arrayFill(startIndex, count, value) {
const result = {};
for (let i = 0; i < count; i++) {
result[startIndex + i] = value;
}
return result;
}
const arr = arrayFill(2, 4, "Hello");
for (const key in arr) {
console.log(`${key} => ${arr[key]}`);
}
✔️ (3) JavaScript Output
2 => Hello 3 => Hello 4 => Hello 5 => Hello
'개발.이산이다' 카테고리의 다른 글
| prev (0) | 2025.12.13 |
|---|---|
| in_array (0) | 2025.12.12 |
| array_chunk (0) | 2025.12.10 |
| date_diff (0) | 2025.12.10 |
| array_diff_ukey (0) | 2025.12.09 |