Get the index value of an array in PHP

后端 未结 12 2033
迷失自我
迷失自我 2020-12-04 23:05

I have an array:

$list = array(\'string1\', \'string2\', \'string3\');

I want to get the index for a given value (i.e. 1 for <

12条回答
  •  不知归路
    2020-12-04 23:49

    The problem is that you don't have a numerical index on your array.
    Using array_values() will create a zero indexed array that you can then search using array_search() bypassing the need to use a for loop.

    $list = array('string1', 'string2', 'string3');
    $index = array_search('string2',array_values($list));
    

提交回复
热议问题