Php in array
From w3cyberlearnings
Contents |
PHP function in_array
This function checks a value within an array. If the value exists in the array, it returns TRUE else returns FALSE.
Syntax in_array
- value: value to search within the array
- array: Array input
- strict (optional): when set to TRUE the functions check string and numeric differently (i.e '30' != 30)
in_array(value, array, strict);
Example 1
<?php $name = array('Ella', 'Amelia', 'Liam', 'Melanie'); if(in_array('Ella',$name)){ echo 'Ella <br/>'; } else { echo 'No Ella <br/>'; } ?>
Output
Ella
Example 2: Set the strict to TRUE
<?php $name = array('30', 'Ella', 'Amelia', 'Liam', 'Melanie'); if (in_array(30, $name, 1)) { echo '30 number Exist <br/>'; } else { echo 'No Ella <br/>'; } ?>
Output
No Ella
Example 3
<?php $name = array('30', 'Ella', 'Amelia', 'Liam', 'Melanie'); if (in_array(30, $name)) { echo '30 number Exist <br/>'; } else { echo 'No Ella <br/>'; } ?>
Output
30 number Exist
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