How to use key in PHP

In this article, I will explain how the key() function can be used in PHP.
  • 1504

key() function in PHP

  • The key() function is used to return the index key from the current internal array position.

  • The key() function returns FALSE on error.

Syntax

key(array)

Parameter

  • array array is required parameter. it is the input array to use.

Example

The following example show to how the key() function can be used in PHP.

<html>

<body>

<h3 style="color: blueviolet;">key() function example in PHP</h3>

    <?php

    $a = array(

    'Subject1' => 'Math',

    'Subject2' => 'Science',

    'Subject3' => 'math',

    'Subject4' => 'Math',

    'Subject5' => 'Computer');

    echo("Current Element Position : <br>");

    while ($subject_name = current($a)) {

    if ($subject_name == 'Math') {

        echo key($a).'<br />';

    }

    next($a);

    }

    ?>

</body>

</html>

 

Output

keyphp.jpg
You may also want to read these related articles here
 
Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.