簡體   English   中英

php在in_array多維中獲取原始鍵值

[英]php Get the original key value in in_array multidimensional

我有一個多維數組,其中包含一些重復的子數組:

 $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 

要查找具有特定值的鍵,可以使用array_search()

因此,要從最終數組中獲取鍵,應在您的else語句中這樣做:

$dupli[$key] = $array;
$originalkey = array_search($array, $final);
echo "Line " . $key . " duplicated row, the original row is: " . $originalkey . "\n";

在文檔中閱讀有關array_search()更多信息

正如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;
    }
}

使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM