-1

I have an array that has a duplicate (zxcas), but with a different value for the key [result] and [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
        )

)

How would I go about writing a function that does the following:

  1. Detects duplicates arrays by the value of the key [path]
  2. Unset one of the duplicates
  3. Change the value of key [status] to "modified".

The final array should look like:

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
        )

)

My code so far:

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

At the moment it only removes the duplicate zxcas, but does not rename the value status. How would I do that?

2
  • Kindly show us, what you've tried? Commented Nov 16, 2013 at 5:48
  • @KamranAhmed I have added the code I have so far at the bottom of the question. So far it only removes the duplicate. Thanks! Commented Nov 16, 2013 at 14:55

2 Answers 2

0

Try like

foreach($my_arr as $element) {
    $hash = $element['path'];
    $res[$hash] = $element;
}
print_r($res);
Sign up to request clarification or add additional context in comments.

1 Comment

How would I change the value of [status]?
0

Alright, so I have come up with a function that does what I was looking for. I'm not too experienced with PHP, thus please let me know if the following method is illogical or inefficient.

//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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.