개발.이산이다

in_array

이산이다 2025. 12. 12. 05:30

1. PHP in_array 함수 설명

in_array 함수는 특정 값이 배열 안에 존재하는지 확인하는 기능을 제공합니다.
검색 성공 시 true, 실패 시 false 를 반환하며, 엄격 모드(strict) 를 사용하면 타입까지 비교합니다.

✔️ 함수 원형

 
in_array ( mixed $needle , array $haystack , bool $strict = false ) : bool

✔️ 주요 특징

  • $needle: 찾을 값
  • $haystack: 검색 대상 배열
  • $strict가 true이면 타입까지 일치해야 함
  • 배열 내 존재 확인 시 널리 사용되는 기본 함수

2. PHP in_array 샘플 코드

 
<?php
$fruits = ["apple", "banana", "orange"];

if (in_array("banana", $fruits)) {
    echo "banana is found!";
} else {
    echo "banana is not found!";
}
?>

3. 샘플코드 설명 및 Output

✔️ 동작 방식

  1. $fruits 배열을 생성
  2. "banana" 값이 배열에 존재하는지 in_array로 확인
  3. 존재하면 "banana is found!" 출력

✔️ Output

 
banana is found!

4. VB.NET에서 유사 기능 (Contains 메서드)

VB.NET에서는 List(Of T).Contains() 또는 Array.IndexOf()로 동일 기능 구현이 가능합니다.


🔹 VB.NET 함수 설명

✔️ Contains

특정 값이 리스트에 존재하는지 Boolean 값으로 반환.


🔹 VB.NET 샘플 코드

 
Dim fruits As New List(Of String) From {"apple", "banana", "orange"}

If fruits.Contains("banana") Then
    Console.WriteLine("banana is found!")
Else
    Console.WriteLine("banana is not found!")
End If

🔹 샘플코드 Output

 
banana is found!

5. C#에서 유사 기능 (Contains 메서드)

C#은 VB.NET과 동일하게 List<T>.Contains() 또는 LINQ Contains() 를 사용합니다.


🔹 C# 함수 설명

Contains는 리스트나 배열 안에 특정 요소가 포함되어 있는지를 확인하는 메서드입니다.


🔹 C# 샘플 코드

 
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<string> fruits = new List<string> { "apple", "banana", "orange" };

        if (fruits.Contains("banana"))
        {
            Console.WriteLine("banana is found!");
        }
        else
        {
            Console.WriteLine("banana is not found!");
        }
    }
}

🔹 Output

 
banana is found!

6. Python에서 유사 기능 (in 연산자)

Python은 매우 직관적으로 in 연산자를 사용합니다.


🔹 Python 기능 설명

in을 사용하면 리스트(혹은 튜플, 문자열 등) 내부에 요소가 존재하는지 확인할 수 있으며 Boolean 값을 반환합니다.


🔹 Python 샘플 코드

 
fruits = ["apple", "banana", "orange"]

if "banana" in fruits:
    print("banana is found!")
else:
    print("banana is not found!")

🔹 Output

 
banana is found!

7. JavaScript에서 유사 기능 (includes)

JavaScript는 includes() 메서드를 사용하여 배열에 값이 포함되어 있는지 검사합니다.


🔹 JavaScript 함수 설명

includes(value) 는 배열 안에 value가 존재하면 true, 없으면 false 를 반환합니다.


🔹 JavaScript 샘플 코드

 
const fruits = ["apple", "banana", "orange"];

if (fruits.includes("banana")) {
    console.log("banana is found!");
} else {
    console.log("banana is not found!");
}

🔹 Output

 
banana is found!

'개발.이산이다' 카테고리의 다른 글

natcasesort  (0) 2025.12.14
prev  (0) 2025.12.13
array_fill  (0) 2025.12.11
array_chunk  (0) 2025.12.10
date_diff  (0) 2025.12.10