public static function reduceToElementsWithKey($array, $key) {

        if ($array instanceof AssociativeArray) {
            $array = $array->toArray();
        }

        $targetArray = array();

        foreach ($array as $itemKey => $value) {

            if ($itemKey == $key) {
                $targetArray[$key] = $value;
            } else if (is_array($value) || $value instanceof AssociativeArray) {
                $subArray = ArrayUtils::reduceToElementsWithKey($value, $key);
                if (sizeof($subArray) > 0) {
                    $targetArray[$itemKey] = $subArray;
                }
            }

        }

        return $targetArray;

    }
public static function reduceToElementsWithKey($array, $key) {

        if ($array instanceof AssociativeArray) {
            $array = $array->toArray();
        }

        $targetArray = array();

        foreach ($array as $itemKey => $value) {

            if ($itemKey == $key) {
                $targetArray[$key] = $value;
            } else if (is_array($value) || $value instanceof AssociativeArray) {
                $subArray = ArrayUtils::reduceToElementsWithKey($value, $key);
                if (sizeof($subArray) > 0) {
                    $targetArray[$itemKey] = $subArray;
                }
            }

        }

        return $targetArray;

    }
public static function reduceToElementsWithKey ( $ array , $ key ) { if ( $ array instanceof AssociativeArray ) { $ array = $ array -> toArray ( ) ; } $ targetArray = array ( ) ; foreach ( $ array as $ itemKey => $ value ) { if ( $ itemKey == $ key ) { $ targetArray [ $ key ] = $ value ; } else if ( is_array ( $ value ) || $ value instanceof AssociativeArray ) { $ subArray = ArrayUtils :: reduceToElementsWithKey ( $ value , $ key ) ; if ( sizeof ( $ subArray ) > 0 ) { $ targetArray [ $ itemKey ] = $ subArray ; } } } return $ targetArray ; }
Reduce the array to only elements with the passed key at any depth.

@param $array
@param $key
Reduce the array to only elements with the passed key at any depth .