Joomla 3.9 - 主页消失当我发布两次由我开发的自定义模块时

I developed a joomla module, it was working fine. When it was published once but when i published it again on same page, then home page gone and i got 500 error, and if I tried to unpublished one module both got unpublished.

How to resolve that issue. As a guess i think i should create a dynamic id with every module. but i dont know how to do that in joomla.

This code is making problems.

function group_by_key($array) {
    $result = array();

    foreach ($array as $sub) {
        foreach ($sub as $k => $v) {
            $result[$k][] = $v;
        }
    }
    return $result;
}

$features_list = array(
    $features_list1 = group_by_key($features[0]),
    $features_list2 = group_by_key($features[1]),
    $features_list3 = group_by_key($features[2]),
    $features_list4 = group_by_key($features[3]),
);

Because i am getting below error.

Fatal error: Cannot redeclare group_by_key() (previously declared in E:\xampp\htdocs\joomla\do\modules\mod_xp_comparison\tmpl\default.php:31) in E:\xampp\htdocs\joomla\do\modules\mod_xp_comparison\tmpl\default.php on line 40

You should try it this way:

if (!function_exists('group_by_key')) {
    function group_by_key($array) {
        $result = array();

        foreach ($array as $sub) {
            foreach ($sub as $k => $v) {
                $result[$k][] = $v;
            }
        }
        return $result;
    }
}

$features_list = array(
    $features_list1 = group_by_key($features[0]),
    $features_list2 = group_by_key($features[1]),
    $features_list3 = group_by_key($features[2]),
    $features_list4 = group_by_key($features[3]),
);

The reason of the above is that you cannot include (or declare) the same function twice. So if it is already defined in a Global scope in your default.php for example then it's just causing a conflict. Thus if you are not sure, then you have to use that function inside an if (!function_exists('any_function_name')) { ...// function ... } condition statement.

If you want to follow standard Joomla practices, instead of placing custom functions in the layout, create a helper class (helper.php):

defined('_JEXEC') or die;

class ModXpComparisonHelper
{
    public static function group_by_key($array)
    {
        $result = array();

        foreach ($array as $sub)
        {
            foreach ($sub as $k => $v)
            {
                $result[$k][] = $v;
            }
        }

        return $result;
    }
}

Include the helper in main module file (mod_xp_comparison.php):

JLoader::register('ModXpComparisonHelper', __DIR__ . '/helper.php');

And then call the function when needed:

$features_list = array(
    $features_list1 = ModXpComparisonHelper::group_by_key($features[0]),
    $features_list2 = ModXpComparisonHelper::group_by_key($features[1]),
    $features_list3 = ModXpComparisonHelper::group_by_key($features[2]),
    $features_list4 = ModXpComparisonHelper::group_by_key($features[3]),
);