-1

我有一个具有重复项(zxcas)的数组,但键 [result] 和 [size] 的值不同:

Array
(
    [0] => Array
        (
            [file] => asda.txt
            [path] => /home/user/public_html/asda.txt
            [type] => .txt
            [size] => 13
            [status] => new
        )

    [1] => Array
        (
            [file] => zxcas.txt
            [path] => /home/user/public_html/zxcas.txt
            [type] => .txt
            [size] => 35
            [status] => new
        )

    [2] => Array
        (
            [file] => AS
            [path] => /home/user/public_html/AS.txt
            [type] => .txt
            [size] => 3
            [status] => deleted
        )

    [3] => Array
        (
            [file] => zxcas.txt
            [path] => /home/user/public_html/zxcas.txt
            [type] => .txt
            [size] => 29
            [status] => deleted
        )

)

我将如何编写一个执行以下操作的函数:

  1. 通过键 [path] 的值检测重复数组
  2. 取消设置重复项之一
  3. 将键 [status] 的值更改为“已修改”。

最终数组应如下所示:

Array
(
    [0] => Array
        (
            [file] => asda.txt
            [path] => /home/user/public_html/asda.txt
            [type] => .txt
            [size] => 13
            [status] => new
        )


    [1] => Array
        (
            [file] => AS
            [path] => /home/user/public_html/AS.txt
            [type] => .txt
            [size] => 3
            [status] => deleted
        )

    [2] => Array
        (
            [file] => zxcas
            [path] => /home/user/public_htmlzxcas.txt
            [type] => .txt
            [size] => 29
            [status] => modified
        )

)

到目前为止我的代码:

$newArray = array(); 
$fillerArray = array(); 
foreach($diff AS $key => $value)
{ 
    if(!in_array($value['path'], $fillerArray))
    { 
        $fillerArray[] = $value['path']; 
        $newArray[] = $value;
    } 
} 
return $newArray;

目前它只删除重复的 zxcas,但不重命名值状态。我该怎么做?

4

2 回答 2

0

好的,所以我想出了一个功能,可以满足我的需求。我对 PHP 不太熟悉,因此请告诉我以下方法是否不合逻辑或效率低下。

//Search multi array function. http://us2.php.net/manual/en/function.array-search.php#90577
function array_search_in_level($needle, $haystack, $key, &$result, $searchlevel = 0)
{  
    while(is_array($haystack) && isset($haystack[key($haystack)]))
    { 
        if($searchlevel == 0 && key($haystack) == $key && $haystack[$key] == $needle)
        { 
            $result = $haystack; 
        }
        elseif($searchlevel > 0)
        { 
            array_search_in_level($needle, $haystack[key($haystack)], $key, $result, $searchlevel - 1); 
        } 
            next($haystack); 
    } 
}

//Compare the two sets
function arr_diff($new, $old)
{
    //Compare new with old (shows added/modified)
    foreach($new as $key => $value)
    {

        if(array_search($value, $old) === false)
        {
            unset($result);

            //Checks if the file exists in old
            array_search_in_level($value["path"], $old, 'path', $result, 1);

            $newarray[$key]["file"] = $value["file"];
            $newarray[$key]["path"] = $value["path"];
            $newarray[$key]["type"] = $value["type"];
            $newarray[$key]["size"] = $value["size"];
            $newarray[$key]["md5"] = $value["md5"];

            //if so it's modified, else it's new
            if($result)
            {
                $newarray[$key]["status"] = "modified";     
            }
            else
            {
                $newarray[$key]["status"] = "new";                  
            }
        }       
    }

    //Compare old with new (shows deleted/modified)
    foreach($old as $key => $value)
    {

        if(array_search($value, $new) === false)
        {
            unset($result);

            //Checks if the file exists in new
            array_search_in_level($value["path"], $new, 'path', $result, 1);

            $oldarray[$key]["file"] = $value["file"];
            $oldarray[$key]["path"] = $value["path"];
            $oldarray[$key]["type"] = $value["type"];
            $oldarray[$key]["size"] = $value["size"];
            $oldarray[$key]["md5"] = $value["md5"];

            //if not it's deleted, else it's modified.
            if(!$result)
            {
                $oldarray[$key]["status"] = "deleted";      
            }
            else
            {

                $oldarray[$key]["status"] = "modified";                 
            }
        }
    }


    $diff = array_merge($newarray, $oldarray);

//Filter duplicates
    $uniqueArray = array(); 
    $fillerArray = array(); 

    foreach($diff AS $key => $value)
    { 
        if(!in_array($value["path"], $fillerArray))
        { 
            $fillerArray[] = $value["path"]; 
            $uniqueArray[] = $value;
        } 
    } 

    return $uniqueArray;
}
于 2013-11-16T23:15:53.760 回答
0

试试喜欢

foreach($my_arr as $element) {
    $hash = $element['path'];
    $res[$hash] = $element;
}
print_r($res);
于 2013-11-16T05:59:35.373 回答