I need to transform one array into another. The first array describes a message. The message has its own ID and the ID of its parent. $firstArray[id]["parent_id"] = parent_id I need to transform this into an array which describes which parent messages have which children: $secondArray(parent_id) = array(id, id, id, id); So, for example: $firstArray[4]["parent_id"] = 2; $firstArray[6]["parent_id"] = 2; $firstArray[12]["parent_id"] = 6; $firstArray[13]["parent_id"] = 12; into $secondArray[12] = array(13); $secondArray[6] = array(12); $secondArray[2] = array(4, 6);