Get the keys for duplicate values in an array

前端 未结 9 1568
温柔的废话
温柔的废话 2020-11-28 11:11

I have the following array:

$myarray = Array(\"2011-06-21\", \"2011-06-22\", \"2011-06-22\", \"2011-06-23\", \"2011-06-23\", \"2011-06-24\", \"2011-06-24\",         


        
9条回答
  •  孤独总比滥情好
    2020-11-28 11:32

    I'll answer the second question first. You want to use array_keys with the "search_value" specified.

    $keys = array_keys($array, "2011-06-29")
    

    In the example below, $duplicates will contain the duplication values while $result will contain ones that are not duplicates. To get the keys, simply use array_keys.

    Result:

    // $duplicates
    Array
    (
        [1] => a
    )
    
    // $result
    Array
    (
        [2] => b
        [3] => c
        [4] => d
    )
    
    // $unique_keys
    Array
    (
        [0] => 2
        [1] => 3
        [2] => 4
    )
    
    // $duplicate_keys
    Array
    (
        [0] => 0
        [1] => 1
    )
    

提交回复
热议问题