PHP array_search() Function Example

PHP Script:


<?php
$array = array(0 => 'book', 1 => 'cup', 'index'=>'text', 2 => 'spoon',3 => 'fork');
$key = array_search('text', $array);
echo "$key\n";
$key = array_search('cup', $array);
echo "$key\n";
?>

Output:


index
1

Description:

PHP array_search() function is used for searching an array for a given value and returns the corresponding array key if the given value is found.

PHP array_search() function parameters:

1- mixed: (required): The value to be searched.
2- Array (required): The array in which value is searched.
3- Bool(optional), Default False: This Boolean flag when set to true, then this function tires to check the type of the value begin searched and checks if both items references to the same  object.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s