3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $configArray = array( 'grand_total' => array( 'before' => array(), '_code' => 'grand_total', ), 'discount' => array( 'before' => array('grand_total'), '_code' => 'discount', ), ); reset($configArray); $element = current($configArray); foreach ($configArray as $code => $data) { foreach ($data['before'] as $beforeCode) { if (!isset($configArray[$beforeCode])) { continue; } $configArray[$code]['before'] = array_unique( array_merge( $configArray[$code]['before'], $configArray[$beforeCode]['before'] ) ); } } print_r($configArray);
based on 5KbtG
Output for 4.3.0 - 7.1.0
Array ( [grand_total] => Array ( [before] => Array ( ) [_code] => grand_total ) [discount] => Array ( [before] => Array ( [0] => grand_total ) [_code] => discount ) )