Jump to: navigation, search

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


Navigation
Web
SQL
MISC
References