PHP in_array() Fungsi
Contoh
Cari nilai "Glenn" dalam array dan keluarkan beberapa teks:
<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
if (in_array("Glenn", $people))
{
echo "Match found";
}
else
{
echo "Match not found";
}
?>
Definisi dan Penggunaan
Fungsi in_array() mencari array untuk nilai tertentu.
Catatan: Jika parameter pencarian adalah string dan parameter tipe diatur ke TRUE, pencarian peka huruf besar/kecil.
Sintaksis
in_array(search, array, type)
Nilai Parameter
Parameter | Description |
---|---|
search | Required. Specifies the what to search for |
array | Required. Specifies the array to search |
type | Optional. If this parameter is set to TRUE, the in_array() function searches for the search-string and specific type in the array. |
Detail Teknis
Nilai Kembali: | Mengembalikan TRUE jika nilai ditemukan dalam array, atau FALSE sebaliknya |
---|---|
Versi PHP: | 4+ |
Log Perubahan PHP: | PHP 4.2: Parameter pencarian sekarang dapat berupa array |
Lebih Banyak Contoh
Contoh
Menggunakan semua parameter:
<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland", 23);
if (in_array("23", $people, TRUE))
{
echo "Match found<br>";
}
else
{
echo "Match not found<br>";
}
if (in_array("Glenn",$people, TRUE))
{
echo "Match found<br>";
}
else
{
echo "Match not found<br>";
}
if (in_array(23,$people, TRUE))
{
echo "Match found<br>";
}
else
{
echo "Match not found<br>";
}
?>
Referensi Array PHP