Example #1
0
/**
 * 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;
}
Example #2
0
/**
 * Find a key in the given array return exploded position, false if key isn't found
 *
 * @version 1
 * @author Rick de Man <*****@*****.**>
 *        
 * @param array $Array
 *        	The array to look in for
 * @param string $Key
 *        	The Key to find in the provided array
 * @param string $Glue
 *        	The 'glue' for key location string
 * @param string $Result
 *        	Do not Use, will be over ruled
 * @return false if not found, string as location of the key
 */
function arraykeylocate($Array, $Key, $Glue = ',', $Result = '')
{
    // Overrule the Result only when function depth is 1
    if (GetFunctionDepth(2) == 1) {
        // Reset Result value
        $Result = '';
    }
    // Check if array has been given
    if (is_array($Array)) {
        // Loop through the array, Find the Key
        foreach ($Array as $K => $V) {
            // Check if the current Key is which we are looking for
            if ($K == $Key) {
                // Check if result is empty
                if ($Result == '') {
                    // Return the Current KeyName
                    return $K;
                } else {
                    // Return the result + KeyName
                    return $Result . $Glue . $K;
                }
            }
        }
        // Loop through the array, go deeper in the array
        foreach ($Array as $K => $V) {
            // Only allow Arrays
            if (is_array($V)) {
                // Check for Results for each level we can
                $Result2 = ArrayKeyLocate($Array[$K], $Key, $Glue, $Result == '' ? $K : $Result . ',' . $K);
                // If the String ends with the Search Key Start returning value(s)
                if (EndsWith($Glue . $Key, $Result2) !== false) {
                    // Return The position
                    return $Result2;
                }
            }
        }
    }
    // Return the result
    return $Result == '' ? false : $Result;
}