Get the index value of an array in PHP

后端 未结 12 1982
迷失自我
迷失自我 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:24

    Could you be a little more specific?

    $key = array_search('string2',$list)
    

    works fine for me. Are you trying to accomplish something more complex?

    0 讨论(0)
  • 2020-12-04 23:25

    Other folks have suggested array_search() which gives the key of the array element where the value is found. You can ensure that the array keys are contiguous integers by using array_values():

    $list = array(0=>'string1', 'foo'=>'string2', 42=>'string3');
    $index = array_search('string2', array_values($list));
    print "$index\n";
    
    // result: 1
    

    You said in your question that array_search() was no use. Can you explain why? What did you try and how did it not meet your needs?

    0 讨论(0)
  • 2020-12-04 23:30

    // or considering your array structure:

    $array = array(
      'string1' => array('a' => '', 'b' => '', 'c' => ''),
      'string2' => array('a' => '', 'b' => '', 'c' => ''),
      'string3' => array('a' => '', 'b' => '', 'c' => ''),
    );
    

    // you could just

    function findIndexOfKey($key_to_index,$array){
      return array_search($key_to_index,array_keys($array));
    }
    

    // executed

    print "\r\n//-- Method 1 --//\r\n";
    print '#index of: string1 = '.findIndexofKey('string1',$array)."\r\n";
    print '#index of: string2 = '.findIndexofKey('string2',$array)."\r\n";
    print '#index of: string3 = '.findIndexofKey('string3',$array)."\r\n";
    

    // alternatively

    print "\r\n//-- Method 2 --//\r\n";
    print '#index of: string1 = '.array_search('string1',array_keys($array))."\r\n";
    print '#index of: string2 = '.array_search('string2',array_keys($array))."\r\n";
    print '#index of: string3 = '.array_search('string3',array_keys($array))."\r\n";
    

    // recursersively

    print "\r\n//-- Method 3 --//\r\n";
    foreach(array_keys($array) as $key => $value){
      print '#index of: '.$value.' = '.$key."\r\n";
    }
    

    // outputs

    //-- Method 1 --//
    #index of: string1 = 0
    #index of: string2 = 1
    #index of: string3 = 2
    
    //-- Method 2 --//
    #index of: string1 = 0
    #index of: string2 = 1
    #index of: string3 = 2
    
    //-- Method 3 --//
    #index of: string1 = 0
    #index of: string2 = 1
    #index of: string3 = 2
    
    0 讨论(0)
  • 2020-12-04 23:37

    array_search is the way to do it.

    array_search ( mixed $needle , array $haystack [, bool $strict = FALSE ] ) : mixed

    From the docs:

    $array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
    
    $key = array_search('green', $array); // $key = 2;
    $key = array_search('red', $array);   // $key = 1;
    

    You could loop over the array manually and find the index but why do it when there's a function for that. This function always returns a key and it will work well with associative and normal arrays.

    0 讨论(0)
  • 2020-12-04 23:38

    Here is a function that will work for numeric or string indices. Pass the array as first parameter, then the index to the element that needs to be moved and finally set the direction to -1 to move the element up and to 1 to move it down. Example: Move(['first'=>'Peter','second'=>'Paul','third'=>'Kate'],'second',-1) will move Paul up and Peter down.

    function Move($a,$element,$direction)
    {
    
    $temp = [];
    $index = 0;
    
    foreach($a as $key=>$value)
    {
        $temp[$index++] = $key;
    }
    
    $index = array_search($element,$temp);
    
    $newpos = $index+$direction;
    
    if(isset($temp[$newpos]))
    {
            $value2 = $temp[$newpos];
            $temp[$newpos]=$element;
            $temp[$index]=$value2;
    }
    else
    {
        # move is not possible
        return $a; # returns the array unchanged
    }   
    
    $final = [];
    
    foreach($temp as $next)
    {
        $final[$next]=$a[$next];
    }
    
    return $final;
    

    }

    0 讨论(0)
  • 2020-12-04 23:41

    If you're only doing a few of them (and/or the array size is large), then you were on the right track with array_search:

    $list = array('string1', 'string2', 'string3');
    $k = array_search('string2', $list); //$k = 1;
    

    If you want all (or a lot of them), a loop will prob do you better:

    foreach ($list as $key => $value) {
        echo $value . " in " . $key . ", ";
    }
    // Prints "string1 in 0, string2 in 1, string3 in 2, "
    
    0 讨论(0)
提交回复
热议问题