我需要将多调整PHP数组的特定元素与同一数组中的某些其他元素进行比较

I have an array that contains certain words from a MySQL database, which are matched with words in a line of text. The text is, say, 20 lines long. The array also stores the position of each word in the database, and some of these words are "related" to other words in the database, which are also stored in the array. It looks a bit like this:

$words = array(
    ["Line0"] => array (
        ["Word0"] => array( 
            ["Name"] => "RandomWord",
            ["DatabasePos"] => 15,
            ["RelationsInDatabase"] => array (
                89, //So this is clearly the same database pos as Word1 on Line0.
                27, //let's assume there's a word on line15 that has this database pos
                )
            ),
        ["Word1"] => array (
            ["Name"] => "SomeOtherRandomWord",
            ["DatabasePos"] => 89,
            ["RelationsInDatabase"] => array (
                NULL
                )
            )
        ),
    ["Line1"] => array (
        ["Word0"] => .... ,
        ..
        ...
        ...
    )
);

etc.

I want to iterate through this array, and find the words that are related to by other words. Then, append to the same array which lines and which words they are related to. For example:

$words = array(
    ["Line0"] => array (
        ["Word0"] => array( 
            ["Name"] => "RandomWord",
            ["DatabasePos"] => 15,
            ["RelationsInDatabase"] => array (
                89, //So this is clearly the same database pos as Word1 on Line0.
                27, //let's assume there's a word on line15 that has this database pos
                ),
            ["RelationsInLinesOfText"] => array ( //I want to loop through and add this element to the array.
                [0] => array("Line0", "Word1"),
                [1] => array("Line15", "Word3")
                )
            ),
        ["Word1"] => array (
            ["Name"] => "SomeOtherRandomWord",
            ["DatabasePos"] => 89,
            ["RelationsInDatabase"] => array (
                NULL
                )
            )
        ),
    ["Line1"] => array (
        ["Word0"] => .... ,
        ..
        ...
        ...
    )
);

My problem is that I end up with a very messy, 4/5 level deep "foreach" loop and end up making a bunch of mistakes that are hard to debug due to the messy code. Is there a clean way of doing this, maybe using something like a RecursiveArrayIterator? I don't have much experience with PHP iterator objects.

Thanks for the help

It is an ugly solution, but I think in any case you'll need to iterate through entire array twice-nested:

function searchLink($iLink, &$rgData)
{
   $rgResult = [];
   foreach($rgData as $sLine=>$rgLines)
   {
      foreach($rgLines as $sWord=>$rgWord)
      {
         if($rgWord['DatabasePos']==$iLink)
         {
            $rgResult[]=['line'=>$sLine, 'word'=>$sWord];
         }
      }
   }
   return $rgResult;
}

//$rgData is a data array
foreach($rgData as $sLine => $rgLines)
{
   foreach($rgLines as $sWord=>$rgWord)
   {
      foreach($rgWord['RelationsInDatabase'] as $iPosition)
      {
         $rgData[$sLine][$sWord]['RelationsInLinesOfText'][]=searchLink($iPosition, $rgData);
      }
   }
}

also, since you've not mentioned if position is unique, an array ['line'=>$sLine, 'word'=>$sWord] will be written to each entry.