함수 in_array()

  다른 뜻에 대해서는 SQL IN 문서를 참조하십시오.

1 개요[ | ]

in_array
inArray
contains
include
in
  • Checks if a value exists in an array (값이 배열안에 존재 하는지 확인)

2 Bash[ | ]

function in_array() {
  NEEDLE="$1"; shift; ARRAY=("$@")
  for VALUE in ${ARRAY[@]}; do [ "$VALUE" == "$NEEDLE" ] && echo 1 && return; done
  echo 0
}
FRUITS=("apple" "banana" "orange")
echo `in_array "banana" "${FRUITS[@]}"` # 1
echo `in_array "melon" "${FRUITS[@]}"` # 0

3 C#[ | ]

string[] fruits = { "banana", "orange", "apple" };
MessageBox.Show(fruits.Contains("apple").ToString()); // True
MessageBox.Show(fruits.Contains("mango").ToString()); // False
ArrayList fruits = new ArrayList();
fruits.Add("banana");
fruits.Add("orange");
fruits.Add("apple");
MessageBox.Show(fruits.Contains("apple").ToString()); // True
MessageBox.Show(fruits.Contains("mango").ToString()); // False

4 Go[ | ]

package main

import "fmt"

func InArray(a []string, x string) bool {
    for _, n := range a {
        if x == n {
            return true
        }
    }
    return false
}

func main() {
    fruits := []string{"apple","banana","mango"}
    fmt.Println( InArray(fruits, "apple") ) // true
    fmt.Println( InArray(fruits, "melon") ) // false
}

5 Java[ | ]

String[] fruits = {"apple", "banana", "orange"};
System.out.println(Arrays.asList(fruits).contains("apple")); // true
System.out.println(Arrays.asList(fruits).contains("melon")); // false

6 JavaScript[ | ]

var fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log( fruits.includes('Banana') ); // true
console.log( fruits.includes('Melon') ); // false
function inArray(needle, haystack) {
  var len = haystack.length;
  for(var i=0; i<len; i++) if(haystack[i] == needle) return true;
  return false;
}
var fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log(inArray("Banana", fruits)); // true
console.log(inArray("Melon", fruits)); // false

7 jQuery[ | ]

var fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log($.inArray("Banana", fruits)); // 0
console.log($.inArray("Orange", fruits)); // 1
console.log($.inArray("Apple", fruits)); // 2
console.log($.inArray("Mango", fruits)); // 3
console.log($.inArray("Melon", fruits)); // -1
var fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log( $.inArray("Banana", fruits) >=0 ); // true
console.log( $.inArray("Orange", fruits) >=0 ); // true
console.log( $.inArray("Melon", fruits) >=0 ); // false

8 Kotlin[ | ]

fun main(args: Array<String>) {
    val fruits = arrayOf("Apple", "Banana", "Orange")
    println( "Apple" in fruits ) // true
    println( "Melon" in fruits ) // false
}

9 Objective-C[ | ]

NSArray *fruits = [NSArray arrayWithObjects:@"apple", @"banana", @"orange", nil];
NSLog(@"%d", [fruits containsObject:@"apple"]); // 1
NSLog(@"%d", [fruits containsObject:@"melon"]); // 0

10 PHP[ | ]

$fruits = ["apple", "banana", "orange"];
var_dump( in_array("apple", $fruits) ); # bool(true)
var_dump( in_array("melon", $fruits) ); # bool(false)

11 Python[ | ]

fruits = ['Apple', 'Banana', 'Orange']
print('Apple' in fruits) # True
print('Melon' in fruits) # False

12 Perl[ | ]

my @fruits = ("Apple", "Banana", "Orange");
grep ( /^Apple$/, @fruits ); # returns true
grep ( /^Melon$/, @fruits ); # returns false

13 R[ | ]

fruits = c("Apple", "Banana", "Orange")
print("Apple" %in% fruits) ## [1] TRUE
print("Melon" %in% fruits) ## [1] FALSE

14 Ruby[ | ]

fruits = ["Apple", "Banana", "Orange"]
puts fruits.include?("Apple") # true
puts fruits.include?("Melon") # false

15 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}