return key array php code example

Example 1: get array key php

array_keys ($array);
// It returns an array
// more informations at https://www.php.net/manual/fr/function.array-keys.php (fr)

Example 2: how to get array key in php

<?php
$array = array(
    'fruit1' => 'apple',
    'fruit2' => 'orange',
    'fruit3' => 'grape',
    'fruit4' => 'apple',
    'fruit5' => 'apple');

// this cycle echoes all associative array
// key where value equals "apple"
for($i = 0; $i< sizeof($array);$i++){
if (key($array[$i]) == 'apple') {
        echo key($array).'<br />';
    }
    //next($array);
}
?>

Tags:

Php Example