Get the index value of an array in PHP

后端 未结 12 1983
迷失自我
迷失自我 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:43
    $find="Topsite";
    $list=array("Tope","Ajayi","Topsite","Infotech");
    $list_count=count($list);
    sort($list);
    for($i=0;$i<$list_count;$i++)
    {
        if($list[$i]==$find){
             $position=$i;
        }
    
    }
    echo $position;
    
    0 讨论(0)
  • 2020-12-04 23:44

    You'll have to create a function for this. I don't think there is any built-in function for that purpose. All PHP arrays are associative by default. So, if you are unsure about their keys, here is the code:

    <?php
    
    $given_array = array('Monday' => 'boring',
    'Friday' => 'yay',
    'boring',
    'Sunday' => 'fun',
    7 => 'boring',
    'Saturday' => 'yay fun',
    'Wednesday' => 'boring',
    'my life' => 'boring');
    
    $repeating_value = "boring";
    
    function array_value_positions($array, $value){
        $index = 0;
        $value_array = array();
            foreach($array as $v){
                if($value == $v){
                    $value_array[$index] = $value;
                }
            $index++;
            }
        return $value_array;
    }
    
    $value_array = array_value_positions($given_array, $repeating_value);
    
    $result = "The value '$value_array[0]' was found at these indices in the given array: ";
    
    $key_string = implode(', ',array_keys($value_array));
    
    echo $result . $key_string . "\n";//Output: The value 'boring' was found at these indices in the given array: 0, 2, 4, 6, 7
    
    
    0 讨论(0)
  • 2020-12-04 23:45

    This code should do the same as your new routine, working with the correct multi-dimensional array..

     $arr = array(
      'string1' => array('a' => '', 'b' => '', 'c' => ''),
      'string2' => array('a' => '', 'b' => '', 'c' => ''),
      'string3' => array('a' => '', 'b' => '', 'c' => '')
     );
    
     echo 'Index of "string2" = '. array_search('string2', array_keys($arr));
    
    0 讨论(0)
  • 2020-12-04 23:45

    array_search should work fine, just tested this and it returns the keys as expected:

    $list = array('string1', 'string2', 'string3');
    echo "Key = ".array_search('string1', $list);
    echo " Key = ".array_search('string2', $list);
    echo " Key = ".array_search('string3', $list);
    

    Or for the index, you could use

    $list = array('string1', 'string2', 'string3');
    echo "Index = ".array_search('string1', array_merge($list));
    echo " Index = ".array_search('string2', array_merge($list));
    echo " Index = ".array_search('string3', array_merge($list));
    
    0 讨论(0)
  • 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));
    
    0 讨论(0)
  • 2020-12-04 23:50

    Try the array_keys PHP function.

    $key_string1 = array_keys($list, 'string1');
    
    0 讨论(0)
提交回复
热议问题