Php array keys
From w3cyberlearnings
Contents |
PHP function array_keys
This function returns all array keys from the array.
Syntax array_keys
- array: array input
array_keys(array);
Example 1
<?php $student = array('Abbey' => 89, 'Abbie' => 88, 'Abbigail' => 83, 'Aiyana' => 93); $student_name = array_keys($student); foreach ($student_name as $name) { echo $name . '<br/>'; } ?>
Output
Abbey Abbie Abbigail Aiyana
Example 2
<?php $student = array( 'A300' => array('name' => 'J4', 'score' => 80), 'A301' => array('name' => 'J3', 'score' => 10), 'A302' => array('name' => 'J2', 'score' => 50), 'A303' => array('name' => 'J1', 'score' => 30), ); $key_code = array_keys($student); foreach ($key_code as $key) { echo $key . '<br/>'; } ?>
Output
A300 A301 A302 A303
Example 3
<?php $fruit = array('apple', 'banana', 'coconut'); $akeys = array_keys($fruit); print_r($akeys); ?>
Output
Array ( [0] => 0 [1] => 1 [2] => 2 )
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