You don't need all those arrays. Just associativeArray
is enough.
You can do it like this:
$newArray = array();
// This will loop in the array so that, in each step:
// - $k is the current key
// - $v is the current value
foreach($associativeArray as $k => $v) {
// If newArray doesn't already have $v as a key
if (!array_key_exists($v, $newArray)) {
// Define it as a new array with only one element
$newArray[$v] = array($k);
} else {
// If it already exists, just push $k to it's elements
$newArray[$v][] = $k; // This is the same as array_push($newArray[$v], $k)
}
}