๊ฐœ๋ฐœ๊ด€๋ จ/PHP

[PHP] ๋ฐฐ์—ด์•ˆ์— ๊ฐ’์„ ํ™•์ธํ•ด์ฃผ๋Š” ํ•จ์ˆ˜ in_array() ์‚ฌ์šฉ๋ฒ• ๋ฐ ์˜ˆ์ œ

๐Ÿป๊ณฐ์ด๐Ÿป 2022. 8. 3.
728x90

์•ˆ๋…•ํ•˜์„ธ์š” ๊ณฐ์ด์—์š”:)

PHP ๊ฐœ๋ฐœ์ค‘์— ๋ฐฐ์—ด ์ •๋ง ๋งŽ์ด์“ฐ์ด๋Š”๋ฐ์š” 

 

๋ฐฐ์—ด์•ˆ์— ๊ฐ’์ด ์žˆ๋Š”์ง€ ํ™•์ธํ• ์ˆ˜์žˆ๋Š” ํ•จ์ˆ˜ ์†Œ๊ฐœํ•ด๋“œ๋ฆฌ๊ณ  ์‚ฌ์šฉ๋ฒ•๊ณผ ์˜ˆ์ œ ์•Œ์•„๋ณด๋ ค๊ณ ํ•ด์š”!

 

in_array ๋ผ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

 

์„ค๋ช…

๋ฐฐ์—ด์— ๊ฐ’์ด ์žˆ๋Š”์ง€ ํ™•์ธ

 

in_array(์ฐพ์„๊ฐ’, ๋ฐฐ์—ด)

 

๊ฐ’์ด ์žˆ์œผ๋ฉด : true

๊ฐ’์ด ์—†์œผ๋ฉด : false

 

๊ฐ„๋‹จํ•œ ์‚ฌ์šฉ ์˜ˆ์ œ

<?php
	$array = array("apple", "banana");

	$search_text = "apple";

	if(in_array($search_text, $array))
	{
		echo "์‚ฌ๊ณผ ์žˆ์–ด์š”";
	}
	else
	{
		echo "์‚ฌ๊ณผ ์—†์–ด์š”";
	}
?>

์ถœ๋ ฅ ๊ฒฐ๊ณผ

์‚ฌ๊ณผ ์žˆ์–ด์š”

 

์–ด๋–ป๊ฒŒ ํ™œ์šฉํ• ๊นŒ?

<?php
	$search_text = "apple";

	if($search_text == "apple" || $search_text == "banana")
	{
		echo "์ฐพ์•˜๋‹ค!";
	}
 ?>

์ด๋ ‡๊ฒŒ || << OR ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ค„์ด ๊ธธ๊ฒŒ ๋Š˜์–ด์„ ๊ฒƒ๋ณด๋‹จ ๋”์šฑ๋” ๊น”๋”ํ•˜๊ฒŒ ์‚ฌ์šฉํ• ์ˆ˜์žˆ์„๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

<?php
	$search_text = "apple";

	if(in_array($search_text, array("apple", "banana")))
	{
		echo "์ฐพ์•˜๋‹ค!";
	}
?>

 

์ด๋ ‡๊ฒŒ ๊ฐ„๋‹จํ•˜๊ฒŒ ์‚ฌ์šฉํ• ์ˆ˜์žˆ์Šต๋‹ˆ๋‹ค.

 

ํ˜ธํ™˜ ๊ฐ€๋Šฅํ•œ ๋ฒ„์ „

PHP 4, PHP 5, PHP 7, PHP 8 ๋ฒ„์ „์—์„œ ์‚ฌ์šฉํ•˜์‹ค์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€