Jump to: navigation, search

Php array search

From w3cyberlearnings

Contents

PHP function array_search

This function searches array value and returns the array key.

Syntax array_search

  • value: value to search
  • array: array input
  • strict: true (treat numeric and string are not the same)
array_search(value, array, strict);

Example 1

<?php

$person = array('Melanie' => 31, 'Ella' => 32, 'Ada' => 40);

$result = array_search(32, $person);
echo $result;
?>

Output

Ella

Example 2

<?php

$person = array('Melanie' => "31", 'Ella' => 31, 'Ada' => "31");

$result = array_search(31, $person);
echo $result;
?>

Output

Melanie

Example 3

<?php

$person = array('Melanie' => "31", 'Ella' => 31, 'Ada' => "31");

$result = array_search(31, $person,true);
echo $result;
?>

Output

Ella

Example 4

<?php

$groups = array('Group 1'=>'china','Group 2'=>'vietnam','Group 3'=>'USA');

echo array_search('china', $groups);

?>

Output

Group 1

Related Links


Navigation
Web
SQL
MISC
References