/** * Create Multidimensional Array ( Menu ) * * @version 1 * @author Rick de Man <*****@*****.**> * * @param array $Menu * The Array with NO sub arrays * @param unknown $KeyUnique * The unique key of array keys * @param unknown $KeyParent * The parentkey of array keys * @return false if operation failed, MultiDimensional array if succeded */ function createmultiarray($Menu, $KeyUnique, $KeyParent) { // Create Result $Result = array(); // Loop through the items for the Main array foreach ($Menu as $K => $V) { // make sure the provided keys exists if (isset($V[$KeyUnique]) && isset($V[$KeyParent])) { // if the 'Parent' key is empty if (in_array($V[$KeyParent], array('', 0))) { // Add the key to the result $Result[$V[$KeyUnique]] = $V; // Remove from provided Menu unset($Menu[$K]); } } } // Check if Main items have been found if (count($Result) == 0) { // return failed return false; } // Loop through the items for the Children foreach ($Menu as $K => $V) { // Check if the parentvalue is found if (ArrayKeyLocate($Result, $V[$KeyParent]) !== false) { // Add the array on the location $Result = ArrayKeyAdd($Result, $V[$KeyParent], $V, $KeyUnique); // Remove Menu item unset($Menu[$K]); } } // Return Array return $Result; }
/** * Find a key in the given array, add the Array with Key on that place * * @version 1 * @author Rick de Man <*****@*****.**> * * @param array $Array * The array where a key needs to be added * @param string $FindKey * What keys needs the be found in the array * @param array $Add * The array to be added on 'FindKey' * @param array $Key * The Key name for in the FindKey * * @return array */ function arraykeyadd($Array, $FindKey, $Add, $Key) { // Check if an array has been given if (is_array($Array)) { // Loop through the array, Find the Key foreach ($Array as $K => $V) { // If the current Key equals the FindKey if ($K == $FindKey) { // The Array -> K -> $Add(KEY) = ArrayAdd $Array[$K][$Add[$Key]] = $Add; return $Array; } } // Loop through the array, go deeper in the array foreach ($Array as $K => $V) { // go óne level deeper in the array $Array[$K] = ArrayKeyAdd($V, $FindKey, $Add, $Key); } } // Return the processed Array return $Array; }