PHP - Get key name of array value

前端 未结 9 1553
暗喜
暗喜 2020-11-29 16:53

I have an array as the following:

function example() {
    /* some stuff here that pushes items with
        dynamically created key strings into an array */         


        
9条回答
  •  迷失自我
    2020-11-29 17:40

    you can use key function of php to get the key name:

     '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).'
    '; } next($array); } ?>

    like here : PHP:key - Manual

提交回复
热议问题