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
- array_change_key_case
- array_chunk
- array_combine
- array_count_values
- array_diff_assoc
- array_diff_key
- array_diff_uassoc
- array_diff_ukey
- array_diff
- array_fill_keys
- array_fill
- array_filter
- array_flip
- array_intersect_assoc
- array_intersect_key
- array_intersect_uassoc
- array_intersect_ukey
- array_intersect
- array_key_ exists
- array_keys