php function id to key code example

Example 1: php array all keys empty

array('key1' => null, 'key2' => null, 'key3' => null, 'key4' => null)
if (!array_filter($array)) {
    // all values are empty (where "empty" means == false)
}

Example 2: php get array key

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

// this cycle echoes all associative array
// key where value equals "apple"
while ($fruit_name = current($array)) {
    if ($fruit_name == 'apple') {
        echo key($array).'<br />';
    }
    next($array);
}
?>

Tags:

Php Example