php获取in_array multidimensional中的原始键值

时间:2017-06-07 14:54:28

标签: php multidimensional-array duplicates

我有一个包含一些重复子数组的多维数组:

 $data = array(
    2 => array  (
            'User'      => 'cat',
            'File_name' => 'cat.docx',
            'Document'  => 'Document1',
            'Date'      => '2017-03-02',
            'Pages'     => 1
        ),
    3 => array  (
            'User'      => 'dog',
            'File_name' => 'dog.docx',
            'Document'  => 'Document2',
            'Date'      => '2017-03-02',
            'Pages'     => 3
        ),
    4 => array  (
            'User'      => 'shark',
            'File_name' => 'shark.docx',
            'Document'  => 'Document3',
            'Date'      => '2017-03-01',
            'Pages'     => 5
        ),
    5 => array  (
            'User'      => 'dog',
            'File_name' => 'dog.docx',
            'Document'  => 'Document2',
            'Date'      => '2017-03-02',
            'Pages'     => 3
    ),
    6 => array  (
            'User'      => 'dog',
            'File_name' => 'dog.docx',
            'Document'  => 'Document2',
            'Date'      => '2017-03-02',
            'Pages'     => 3
    ),
    //...
);

我可以识别哪些项目是重复的:

$final = $dupli = array();
foreach ($data as $key => $array ) {
    if(!in_array($array, $final)){
        $final[$key] = $array;
    }
    else
    {
        $dupli[$key] = $array;
    }
}
print_r($dupli);

但是我想知道找到的第一个子阵列(关键字)是什么索引,例如:

 $final = $dupli = array();
    foreach ($data as $key => $array ) {
        if(!in_array($array, $final)){
            $final[$key] = $array;
        }
        else
        {
            $dupli[$key] = $array;
            echo "Line " . $key . " duplicated row, the original row is: " . $originalkey;
        }
    }

输出:

Line 5 duplicated row, the original row is: 3 
Line 6 duplicated row, the original row is: 3 

3 个答案:

答案 0 :(得分:2)

要查找具有特定值的密钥,您可以使用<ul> <li draggable="true">List Item 1</li> <li draggable="true">List Item 2</li> <li draggable="true">List Item 3</li> <li draggable="true">List Item 4</li> <li draggable="true">List Item 5</li> <li draggable="true">List Item 6</li> <li draggable="true">List Item 7</li> <li draggable="true">List Item 8</li> <li draggable="true">List Item 9</li> <li draggable="true">List Item 10</li> </ul>

因此,要从最终数组中获取密钥,这应该在array_search()语句中执行:

else

{{3}}

答案 1 :(得分:0)

正如Magnus Eriksson所说,您可以使用array_search,如果您在现有周期中使用它而不是in_array,则每个周期只能执行一次操作:

 $final = $dupli = array();
 foreach ($data as $key => $array ) {
    if(($originalkey = array_search($array, $final)) === false){
        $final[$key] = $array;
    }
    else
    {
        $dupli[$key] = $array;
        echo "Line " . $key . " duplicated row, the original row is: " . $originalkey;
    }
}

答案 2 :(得分:0)

使用array_search()和循环,您可以简化代码,如下所示:

foreach($data as $index => $value)
{
    $key = array_search($value, $data);
    if($key!==false && $key != $index){
        printf("Line %d duplicated row, the original row is: %d\n",$index, $key);
    }
}

在CLI上测试结果

<强>脚本

akshay@db-3325:/tmp$ cat test.php 
<?php
$data = array(
    2 => array  (
            'User'      => 'cat',
            'File_name' => 'cat.docx',
            'Document'  => 'Document1',
            'Date'      => '2017-03-02',
            'Pages'     => 1
        ),
    3 => array  (
            'User'      => 'dog',
            'File_name' => 'dog.docx',
            'Document'  => 'Document2',
            'Date'      => '2017-03-02',
            'Pages'     => 3
        ),
    4 => array  (
            'User'      => 'shark',
            'File_name' => 'shark.docx',
            'Document'  => 'Document3',
            'Date'      => '2017-03-01',
            'Pages'     => 5
        ),
    5 => array  (
            'User'      => 'dog',
            'File_name' => 'dog.docx',
            'Document'  => 'Document2',
            'Date'      => '2017-03-02',
            'Pages'     => 3
    ),
    6 => array  (
            'User'      => 'dog',
            'File_name' => 'dog.docx',
            'Document'  => 'Document2',
            'Date'      => '2017-03-02',
            'Pages'     => 3
    ),
    //...
);


foreach($data as $index => $value)
{
    $key = array_search($value, $data);
    if($key!==false && $key != $index){
        printf("Line %d duplicated row, the original row is: %d\n",$index, $key);
    }
}



?>

<强>输出

akshay@db-3325:/tmp$ php test.php 
Line 5 duplicated row, the original row is: 3
Line 6 duplicated row, the original row is: 3