0

i have two arrays in php, one is a list of files and the other has uniqueid() => ("file", uid, meetings)

array 1:

$madeFromFilesArray = array(
    "index",
    "contact",
    "reportA",
    "reportB",
);

array 2:

$LoadedArray = array(
    5156e1b122c2b => array("index", uid, meetings),
    5156e1b122c2c => array("about", uid, meetings),
    5156e1b122c2d => array("contact", uid, meetings),
    5156e1b122c2e => array("reportB", uid, meetings),
);

array 1 is the updated one. i need to sync array 2 with 1. for example delete "about" from array 2 because it is not in 1 and add reportA to array 2 because it is in 1 and not in 2. so the final array is:

result array3:

$LoadedArray = array(
    5156e1b122c2b => array("index", uid, meetings),
    5156e1b122c2d => array("contact", uid, meetings),
    5156e1b122c2e => array("reportB", uid, meetings),
    5156e1b122c2f => array("reportA", uid, meetings),
);
4

1 回答 1

2

Here's a solution, but I have no idea where uid and meetings come from or what they represent, so I used strings

$madeFromFilesArray = array(
    "index",
    "contact",
    "reportA",
    "reportB",
);

$LoadedArray = array(
    '5156e1b122c2b' => array("index", 'uid', 'meetings'),
    '5156e1b122c2c' => array("about", 'uid', 'meetings'),
    '5156e1b122c2d' => array("contact", 'uid', 'meetings'),
    '5156e1b122c2e' => array("reportB", 'uid', 'meetings'),
);

$seenFiles = array();
foreach ($LoadedArray as $key => $values) {
    if (!in_array($values[0], $madeFromFilesArray)) {
        unset($LoadedArray[$key]);
    } else {
        $seenFiles[] = $values[0];
    }
}

$missingFiles = array_diff($madeFromFilesArray, $seenFiles);
foreach ($missingFiles as $value) {
    $LoadedArray[uniqid()] = array($value, 'uid', 'meetings');
}
于 2013-03-30T13:54:08.047 回答