PHP:多维数组:按键值删除重复数组,更改唯一数组的键值

时间:2013-11-16 05:42:04

标签: php arrays multidimensional-array unique-values

我有一个重复的数组(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. 按键[路径]
  2. 的值检测重复数组
  3. 取消其中一个副本
  4. 将键[status]的值更改为“modified”。
  5. 最终数组应如下所示:

    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,但不重命名值状态。我该怎么做?

2 个答案:

答案 0 :(得分:0)

尝试

foreach($my_arr as $element) {
    $hash = $element['path'];
    $res[$hash] = $element;
}
print_r($res);

答案 1 :(得分: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;
}